Skip to content

Commit b4e344d

Browse files
committed
tests validator is tested for all unicode ranges
1 parent 0cf3b11 commit b4e344d

File tree

2 files changed

+137
-5
lines changed

2 files changed

+137
-5
lines changed

lib/validator/is.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66

77
var rules = {
8-
NCHAR: /^[\u002D|\u002E|\u005F|\w]+$/,
9-
NQCHAR: /^[\u0021|\u0023-\u005B|\u005D-\u007E]+$/,
10-
NQSCHAR: /^[\u0020-\u0021|\u0023-\u005B|\u005D-\u007E]+$/,
11-
UNICODECHARNOCRLF: /^[\u0009|\u0020-\u007E|\u0080-\uD7FF|\uE000-\uFFFD|\u10000-\u10FFFF]+$/,
8+
NCHAR: /^[\u002D\u002E\u005F\w]+$/,
9+
NQCHAR: /^[\u0021\u0023-\u005B\u005D-\u007E]+$/,
10+
NQSCHAR: /^[\u0020-\u0021\u0023-\u005B\u005D-\u007E]+$/,
11+
// note: \u10000-\u10FFFF is displayed by surrogate pairs
12+
UNICODECHARNOCRLF: /^[\u0009\u0020-\u007E\u0080-\uD7FF\uE000-\uFFFD]+$/,
13+
UNICODECHARNOCRLF_EXTENDED: /[\u{10000}-\u{10FFFF}]/u,
1214
URI: /^[a-zA-Z][a-zA-Z0-9+.-]+:/,
1315
VSCHAR: /^[\u0020-\u007E]+$/
1416
};
@@ -57,7 +59,12 @@ module.exports = {
5759
*/
5860

5961
uchar: function(value) {
60-
return rules.UNICODECHARNOCRLF.test(value);
62+
// manually test \u10000-\u10FFFF
63+
if (rules.UNICODECHARNOCRLF.test(value)) {
64+
return true;
65+
}
66+
67+
return .test(value);
6168
},
6269

6370
/**

test/unit/validator/is_test.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
var is = require('../../../lib/validator/is');
2+
require('should');
3+
4+
function runRanges (ranges, fn, expected) {
5+
ranges.forEach(function (range) {
6+
var lower = range[0];
7+
var upper = range[1];
8+
9+
for (var i = lower; i <= upper; i++) {
10+
var unicodeChar = String.fromCodePoint(i);
11+
var nchar = fn(unicodeChar);
12+
nchar.should.eql(expected, i + ' ' + unicodeChar);
13+
}
14+
});
15+
}
16+
17+
describe('Validator', function () {
18+
describe('is', function () {
19+
it('validates if a value matches a unicode character (nchar)', function () {
20+
var validRanges = [
21+
[45, 46], // \u002D \u002E
22+
[48, 57], // 0-9
23+
[65, 90], // A-Z
24+
[95, 95], // \u005F
25+
[97, 122] // a-z
26+
];
27+
28+
runRanges(validRanges, is.nchar, true);
29+
30+
var invalidRanges = [
31+
[0, 44],
32+
[47, 47],
33+
[58, 64],
34+
[91, 94],
35+
[96, 96],
36+
[123, 1023]
37+
];
38+
39+
runRanges(invalidRanges, is.nchar, false);
40+
});
41+
it('validates if a value matches a unicode character, including exclamation marks (nqchar)', function () {
42+
var validRanges = [
43+
[33, 33], // \u0021
44+
[35, 91], // \u0023-\u005B
45+
[93, 126] // \u005D-\u007E
46+
];
47+
48+
runRanges(validRanges, is.nqchar, true);
49+
50+
var invalidRanges = [
51+
[0, 32],
52+
[34, 34],
53+
[92, 92],
54+
[127, 1023]
55+
];
56+
57+
runRanges(invalidRanges, is.nqchar, false);
58+
});
59+
it('validates if a value matches a unicode character, including exclamation marks and spaces (nqschar)', function () {
60+
var validRanges = [
61+
[32, 33], // \u0020-\u0021
62+
[35, 91], // \u0023-\u005B
63+
[93, 126] // \u005D-\u007E
64+
];
65+
66+
runRanges(validRanges, is.nqschar, true);
67+
68+
var invalidRanges = [
69+
[0, 31],
70+
[34, 34],
71+
[92, 92],
72+
[127, 1023]
73+
];
74+
75+
runRanges(invalidRanges, is.nqschar, false);
76+
});
77+
it('validates if a value matches a unicode character excluding the carriage return and linefeed characters (uchar)', function () {
78+
this.timeout(10000);
79+
var validRanges = [
80+
[9, 9], // \u0009
81+
[32, 126], // \u0020-\u007E,
82+
[128, 55295], // \u0080-\uD7FF
83+
[57344, 65533], // \uE000-\uFFFD
84+
[65536, 1114111] // \u10000-\u10FFFF
85+
];
86+
87+
runRanges(validRanges, is.uchar, true);
88+
89+
var invalidRanges = [
90+
[0, 8],
91+
[10, 31],
92+
[127, 127],
93+
[55296, 57343],
94+
[65534, 65535]
95+
];
96+
97+
runRanges(invalidRanges, is.uchar, false);
98+
});
99+
it('validates if a value matches generic URIs (uri)', function () {
100+
['aa:', 'http:', 'https:'].forEach(function (uri) {
101+
is.uri(uri).should.equal(true);
102+
is.uri(uri.toUpperCase()).should.equal(true);
103+
});
104+
105+
['a', 'a:', 'http'].forEach(function (uri) {
106+
is.uri(uri).should.equal(false);
107+
is.uri(uri.toUpperCase()).should.equal(false);
108+
});
109+
});
110+
it('validates if a value matches against the printable set of unicode characters (vschar)', function () {
111+
var validRanges = [
112+
[32, 126] // \u0020-\u007E
113+
];
114+
115+
runRanges(validRanges, is.vschar, true);
116+
117+
var invalidRanges = [
118+
[0, 31],
119+
[127, 1023]
120+
];
121+
122+
runRanges(invalidRanges, is.vschar, false);
123+
});
124+
});
125+
});

0 commit comments

Comments
 (0)