-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.test.js
347 lines (293 loc) · 11.1 KB
/
index.test.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const assert = require('assert');
const apply = require('./index.js');
const Ajv = require('ajv').default;
describe('load types', function () {
it('add the types to ajv with the apply function', function () {
const ajv = new Ajv();
apply(ajv);
assert.ok(ajv.formats.duration);
assert.ok(ajv.formats.iri);
assert.ok(ajv.formats['idn-email']);
assert.ok(ajv.formats['idn-hostname']);
assert.ok(ajv.formats['iri-reference']);
});
it('add the types to ajv as options to Ajv instances', function () {
const formats = require('./formats');
const ajv = new Ajv({ formats });
assert.ok(ajv.formats.duration);
assert.ok(ajv.formats.iri);
assert.ok(ajv.formats['idn-email']);
assert.ok(ajv.formats['idn-hostname']);
assert.ok(ajv.formats['iri-reference']);
});
it('accept valid IRIs', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'iri',
};
const validate = ajv.compile(schema);
// examples from https://tools.ietf.org/html/rfc2396#section-1.3
assert.ok(validate('http://www.ietf.org/rfc/rfc2396.txt'));
assert.ok(validate('https://пошта.укр/russian'));
assert.ok(validate('ldap://[2001:db8::7]/c=GB?objectClass?one'));
assert.ok(validate('mailto:John.Doe@example.com'));
assert.ok(validate('news:comp.infosystems.www.servers.unix'));
assert.ok(validate('tel:+1-816-555-1212'));
assert.ok(validate('telnet://192.0.2.16:80/'));
assert.ok(validate('urn:oasis:names:specification:docbook:dtd:xml:4.1.2'));
// https://github.com/luzlab/ajv-formats-draft2019/issues/11
assert.ok(validate('modbus+tcp://1.2.3.4/path'));
assert.ok(validate('mqtt://1.2.3.4/path'));
// https://github.com/luzlab/ajv-formats-draft2019/issues/16
assert.ok(validate('http://www.w3.org/2004/02/skos/core#Concept'));
});
it('reject invalid IRIs', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'iri',
};
var validate = ajv.compile(schema);
assert.ok(!validate('example.com')); // missing a scheme
assert.ok(!validate('invalidScheme://example.com')); // an invalid scheme
assert.ok(!validate('this:that'));
// These are IRI-References not IRI
assert.ok(!validate('#someelement'));
assert.ok(!validate('afile.svg#anelement'));
});
it('accept a valid duration', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'duration',
};
var validate = ajv.compile(schema);
assert.ok(validate('P1Y2M4DT20H44M12.67S'));
});
it('reject an invalid duration', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'duration',
};
var validate = ajv.compile(schema);
assert.ok(!validate('10 seconds'));
});
it('accept valid idn-emails', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'idn-email',
};
const validate = ajv.compile(schema);
// examples from https://en.wikipedia.org/wiki/International_email
assert.ok(validate('квіточка@пошта.укр'));
assert.ok(validate('Dörte@Sörensen.example.com'));
assert.ok(validate('John.Doe@example.com'));
assert.ok(validate('"John Doe"@example.com'));
});
it('reject invalid idn-emails', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'idn-email',
};
var validate = ajv.compile(schema);
assert.ok(!validate('johndoe'));
assert.ok(!validate('valid@somewhere.com?asdf'));
});
// skipped because of https://github.com/luzlab/ajv-formats-draft2019/issues/14
it.skip('accept valid uuids', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'uuid',
};
const validate = ajv.compile(schema);
// examples from https://www.uuidgenerator.net/version4
assert.ok(validate('90e89155-4c0d-4942-a804-a610ccb76b1b'));
assert.ok(validate('55e9b1aa-cff4-43fe-8c66-bdd190720360'));
// examples from https://www.uuidgenerator.net/version1
assert.ok(validate('e1a4973e-395b-11ea-a137-2e728ce88125'));
});
// skipped because of https://github.com/luzlab/ajv-formats-draft2019/issues/14
it.skip('reject invalid uuids', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'uuid',
};
var validate = ajv.compile(schema);
assert.ok(!validate('90e89155-4c0d-4942-a804-a610ccb76b1b1'));
assert.ok(!validate('55e9b1aa-cff4-43fe-8c66-bdg190720360'));
});
it('accept valid international domains', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'idn-hostname',
};
const validate = ajv.compile(schema);
assert.ok(validate('google.com'));
assert.ok(validate('123.example.com.'));
// example from https://en.wikipedia.org/wiki/Internationalized_domain_name#Example_of_IDNA_encoding
assert.ok(validate('ジェーピーニック.jp'));
assert.ok(validate('ουτοπία.δπθ.gr'));
// https://tools.ietf.org/html/rfc5890#section-2.3.2.3
// -- An "internationalized domain name" (IDN) is a domain name that contains
// at least ONE A-label or U-label
assert.ok(validate('localhost'));
// from AJV test suite
// valid hostname - maximum length hostname (255 octets) with trailing dot
assert.ok(
validate(
'abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy.example.com.',
),
);
// valid hostname - maximum length hostname (255 octets)
assert.ok(
validate(
'abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy.example.com',
),
);
// valid hostname - maximum length label (63 chars)
assert.ok(
validate(
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.example.com',
),
);
// example from https://unicode.org/faq/idn.html#11
assert.ok(validate('öbb.at'));
});
it('reject invalid international domains', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'idn-hostname',
};
var validate = ajv.compile(schema);
// bad tld
// from ajv test suite
// invalid hostname - label too long (64 chars)
assert.ok(
!validate(
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl.example.com',
),
);
// invalid hostname - hostname too long (256 octets)
assert.ok(
!validate(
'abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.example.com',
),
);
// invalid hostname - hostname too long (256 octets)
assert.ok(
!validate(
'abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.example.com.',
),
);
// a URL, not a hostname
assert.ok(!validate('http://google.com'));
});
it('accept valid IRI-reference', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'iri-reference',
};
const validate = ajv.compile(schema);
assert.ok(validate('https://tools.ietf.org/html/rfc3986#section-4.2'));
// examples from https://dev.w3.org/SVG/profiles/1.2T/publish/diff/linking.html#IRIforms
assert.ok(validate('#someelement'));
assert.ok(validate('afile.svg#anelement'));
assert.ok(validate('afile.svg'));
assert.ok(validate('somecontainer#fragment'));
// from http://homepage.divms.uiowa.edu/~rus/Courses/WebPro/uri.pdf
assert.ok(
validate(
'http://example.org/absolute/URI/with/absolute/path/to/resource.txt',
),
);
assert.ok(
validate(
'//example.org/scheme-relative/URI/with/absolute/path/to/resource.txt',
),
);
assert.ok(validate('/relative/URI/with/absolute/path/to/resource.txt'));
assert.ok(validate('relative/path/to/resource.txt'));
assert.ok(validate('../../../resource.txt'));
assert.ok(validate('./resource.txt#frag01'));
assert.ok(validate('resource.txt'));
assert.ok(validate('#frag01'));
// https://tools.ietf.org/html/rfc3986#section-4.2
assert.ok(validate('//network/test'));
assert.ok(validate('./this:that'));
assert.ok(validate('./path')); // relative-path reference
assert.ok(validate('/path')); // absolute-path reference
// https://github.com/luzlab/ajv-formats-draft2019/issues/9
assert.ok(validate('mailto:valid@email.format'));
});
it('reject invalid IRI-reference', function () {
const ajv = new Ajv();
apply(ajv);
const schema = {
type: 'string',
format: 'iri-reference',
};
var validate = ajv.compile(schema);
// https://tools.ietf.org/html/rfc3986#section-4.2
assert.ok(!validate('this:that'));
// https://github.com/luzlab/ajv-formats-draft2019/issues/9
assert.ok(!validate('mailto:invalid.format'));
});
it('idn should not include the duration format', function () {
const draft07 = require('./idn');
assert.ok(!draft07.duration);
});
it('draft07 should include the correct formats', function () {
const idn = require('./idn');
assert.ok(idn['idn-hostname']);
assert.ok(idn['idn-email']);
assert.ok(idn['iri']);
assert.ok(idn['iri-reference']);
});
it('add the idn types to ajv as options to Ajv instances', function () {
const formats = require('./idn');
const ajv = new Ajv({ formats });
assert.ok(!ajv.formats.duration);
assert.ok(ajv.formats.iri);
assert.ok(ajv.formats['idn-email']);
assert.ok(ajv.formats['idn-hostname']);
assert.ok(ajv.formats['iri-reference']);
});
it('it should be possible to cherry pick formats to install', function () {
const { duration, iri } = require('./formats');
const ajv = new Ajv({ formats: { duration, iri } });
assert.ok(ajv.formats.duration);
assert.ok(ajv.formats.iri);
assert.ok(!ajv.formats['idn-email']);
assert.ok(!ajv.formats['idn-hostname']);
assert.ok(!ajv.formats['iri-reference']);
});
it('it should be possible to specify formats to install', function () {
const ajv = new Ajv();
apply(ajv, { formats: ['idn-email', 'iri'] });
assert.ok(!ajv.formats.duration);
assert.ok(ajv.formats.iri);
assert.ok(ajv.formats['idn-email']);
assert.ok(!ajv.formats['idn-hostname']);
assert.ok(!ajv.formats['iri-reference']);
});
});