-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (91 loc) · 2.73 KB
/
index.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
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
'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 */
/**
* 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) {
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) {
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) {
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) {
// 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) {
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) {
return rules.VSCHAR.test(value);
}
};
/**
* Export validation functions.
*/
module.exports = isFormat;