-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (109 loc) · 3.15 KB
/
index.js
File metadata and controls
127 lines (109 loc) · 3.15 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
/* eslint-disable no-control-regex */
/**
* Validation rules.
* @private
*/
const rules = {
NCHAR: /^[\u002D\u002E\u005F\w]+$/,
NQCHAR: /^[\u0021\u0023-\u005B\u005D-\u007E]+$/,
NQSCHAR: /^[\u0020-\u0021\u0023-\u005B\u005D-\u007E]+$/,
UNICODECHARNOCRLF: /^[\u0009\u0020-\u007E\u0080-\uD7FF\uE000-\uFFFD]+$/,
UNICODECHARNOCRLF_EXTENDED: /^[\u{10000}-\u{10FFFF}]+$/u,
URI: /^[a-zA-Z][a-zA-Z0-9+.-]+:/,
VSCHAR: /^[\u0020-\u007E]+$/
};
/* eslint-enable no-control-regex */
/**
* Ensure the value is a string.
* @private
* @param s {any} the value to be checked
* @throws {TypeError} if the value is not a string
*/
const assertString = (s) => {
const t = typeof s;
if (t !== 'string') {
throw new TypeError('Value must be a string');
}
};
/**
* Minimal, RFC 6749, compliant unicode validator.
* @see https://datatracker.ietf.org/doc/html/rfc6749#appendix-A
*/
const isFormat = {
/**
* Validate if a value matches a unicode character.
*
* @see https://tools.ietf.org/html/rfc6749#appendix-A
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
nchar: function(value) {
assertString(value);
return rules.NCHAR.test(value);
},
/**
* Validate if a value matches a unicode character, including exclamation marks.
*
* @see https://tools.ietf.org/html/rfc6749#appendix-A
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
nqchar: function(value) {
assertString(value);
return rules.NQCHAR.test(value);
},
/**
* Validate if a value matches a unicode character, including exclamation marks and spaces.
*
* @see https://tools.ietf.org/html/rfc6749#appendix-A
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
nqschar: function(value) {
assertString(value);
return rules.NQSCHAR.test(value);
},
/**
* Validate if a value matches a unicode character excluding the carriage
* return and linefeed characters.
*
* @see https://tools.ietf.org/html/rfc6749#appendix-A
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
uchar: function(value) {
assertString(value);
// manually test \u10000-\u10FFFF
if (rules.UNICODECHARNOCRLF.test(value)) {
return true;
}
return rules.UNICODECHARNOCRLF_EXTENDED.test(value);
},
/**
* Validate if a value matches generic URIs.
*
* @see http://tools.ietf.org/html/rfc3986#section-3
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
uri: function(value) {
assertString(value);
return rules.URI.test(value);
},
/**
* Validate if a value matches against the printable set of unicode characters.
*
* @see https://tools.ietf.org/html/rfc6749#appendix-A
* @value {string} the value to be validated
* @return {boolean} true, if valid, otherwise false
*/
vschar: function(value) {
assertString(value);
return rules.VSCHAR.test(value);
}
};
/**
* Export validation functions.
*/
module.exports = isFormat;