-
Notifications
You must be signed in to change notification settings - Fork 8
/
iri-reference.js
43 lines (37 loc) · 956 Bytes
/
iri-reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { parse } = require('uri-js');
const addressParser = require('smtp-address-parser').parse;
const schemes = require('schemes');
function validate(address) {
try {
addressParser(address);
return true;
} catch (err) {
return false;
}
}
module.exports = (value) => {
const iri = parse(value);
// All valid IRIs are valid IRI-references
if (iri.scheme === 'mailto' && iri.to.every(validate)) {
return true;
}
if (
iri.reference === 'absolute' &&
iri.path !== undefined &&
schemes.allByName[iri.scheme]
) {
return true;
}
// Check for valid IRI-reference
// If there is a scheme, it must be valid
if (iri.scheme && !schemes.allByName[iri.scheme]) {
return false;
}
// Check there's a path and for a proper type of reference
return (
iri.path !== undefined &&
(iri.reference === 'relative' ||
iri.reference === 'same-document' ||
iri.reference === 'uri')
);
};