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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
| var env,
| DRAFTS = [
| "draft-00",
| "draft-01",
| "draft-02",
| "draft-03"
| ],
| curDraftId;
|
| //calls ok(true) if no error is thrown
| function okNoError(func, msg) {
| try {
| func();
| ok(true, msg);
| } catch (e) {
| ok(false, msg + ': ' + JSON.stringify(e));
| }
| }
|
| //calls ok(true) if an error is thrown
| function okError(func, msg) {
| try {
| func();
| ok(false, msg);
| } catch (e) {
| ok(true, msg + ': ' + JSON.stringify(e));
| }
| }
|
| //
| //
| // Tests
| //
| //
|
| for (curDraftId = 0; curDraftId < DRAFTS.length; ++curDraftId) {
|
| module(DRAFTS[curDraftId]);
|
| (function (id) {
| test("Acquire Validator", function () {
| JSV = require('../lib/jsv').JSV;
| env = null;
|
| ok(JSV, "JSV is loaded");
|
| env = JSV.createEnvironment("json-schema-draft-03");
|
| env.setOption("defaultSchemaURI", "http://json-schema.org/" + id + "/hyper-schema#");
| env.setOption("latestJSONSchemaSchemaURI", "http://json-schema.org/" + id + "/schema#");
| env.setOption("latestJSONSchemaHyperSchemaURI", "http://json-schema.org/" + id + "/hyper-schema#");
| env.setOption("latestJSONSchemaLinksURI", "http://json-schema.org/" + id + "/links#");
|
| ok(env, "Environment created");
|
| });
| }(DRAFTS[curDraftId]));
|
| test("Primitive Validation", function () {
| equal(env.validate({}).errors.length, 0, "Object");
| equal(env.validate([]).errors.length, 0, "Array");
| equal(env.validate('').errors.length, 0, "String");
| equal(env.validate(00).errors.length, 0, "Number");
| equal(env.validate(false).errors.length, 0, "Boolean");
| equal(env.validate(null).errors.length, 0, "Null");
| });
|
| test("Type Validation", function () {
| //simple type
| equal(env.validate({}, { type : 'object' }).errors.length, 0, "Object");
| equal(env.validate([], { type : 'array' }).errors.length, 0, "Array");
| equal(env.validate('', { type : 'string' }).errors.length, 0, "String");
| equal(env.validate(00, { type : 'number' }).errors.length, 0, "Number");
| equal(env.validate(00, { type : 'integer' }).errors.length, 0, "Integer");
| equal(env.validate(false, { type : 'boolean' }).errors.length, 0, "Boolean");
| equal(env.validate(null, { type : 'null' }).errors.length, 0, "Null");
| equal(env.validate(true, { type : 'any' }).errors.length, 0, "Any");
|
| notEqual(env.validate(null, { type : 'object' }).errors.length, 0, "Object");
| notEqual(env.validate(null, { type : 'array' }).errors.length, 0, "Array");
| notEqual(env.validate(null, { type : 'string' }).errors.length, 0, "String");
| notEqual(env.validate(null, { type : 'number' }).errors.length, 0, "Number");
| notEqual(env.validate(0.1, { type : 'integer' }).errors.length, 0, "Integer");
| notEqual(env.validate(null, { type : 'boolean' }).errors.length, 0, "Boolean");
| notEqual(env.validate(false, { type : 'null' }).errors.length, 0, "Null");
|
| //union type
| equal(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Object");
| notEqual(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
|
| //schema union type
| equal(env.validate({}, { type : [{ type : 'string' }, { type : 'object' }] }).errors.length, 0, "Object");
| equal(env.validate(55, { type : [{ type : 'string' }, { type : 'object' }, 'number'] }).errors.length, 0, "Object");
| notEqual(env.validate([], { type : ['string', { type : 'object' }] }).errors.length, 0, "Array");
| });
|
| test("Properties Validation", function () {
| equal(env.validate({}, { type : 'object', properties : {} }).errors.length, 0);
| equal(env.validate({ a : 1 }, { type : 'object', properties : { a : {}} }).errors.length, 0);
| equal(env.validate({ a : 1 }, { type : 'object', properties : { a : { type : 'number' }} }).errors.length, 0);
| equal(env.validate({ a : { b : 'two' } }, { type : 'object', properties : { a : { type : 'object', properties : { b : { type : 'string' } } }} }).errors.length, 0);
|
| notEqual(env.validate({ a : 1 }, { type : 'object', properties : { a : { type : 'string' }} }).errors.length, 0);
| notEqual(env.validate({ a : { b : 'two' } }, { type : 'object', properties : { a : { type : 'object', properties : { b : { type : 'number' } } }} }).errors.length, 0);
| });
|
| if (curDraftId > 2) {
| test("PatternProperties Validation", function () {
| equal(env.validate(null, { patternProperties : {} }).errors.length, 0);
| equal(env.validate({}, { patternProperties : {} }).errors.length, 0);
| equal(env.validate({ a : 1 }, { patternProperties : { '[a-z]' : {}} }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, cc : '3' }, { patternProperties : { '^[a-z]$' : { type : 'number' }} }).errors.length, 0);
| equal(env.validate({ a : { b : 'two' } }, { patternProperties : { '[a-z]' : { patternProperties : { '[a-z]' : { type : 'string' } } }} }).errors.length, 0);
|
| notEqual(env.validate({ a : 1, b : 2, c : '3' }, { patternProperties : { '^[a-z]$' : { type : 'number' }} }).errors.length, 0);
| notEqual(env.validate({ a : { b : 'two' } }, { patternProperties : { '[a-z]' : { patternProperties : { '[a-z]' : { type : 'number' } } }} }).errors.length, 0);
| });
| }
|
| test("AdditionalProperties Validation", function () {
| //object tests
| equal(env.validate({ a : 1, b : 2, c : 3 }, {}).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : true }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : true }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : false }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : { type : 'number' } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'number' } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
|
| notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : false }).errors.length, 0);
| notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
|
| //array tests
| equal(env.validate([1, 2, 3], {}).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalProperties : true }).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalProperties : false }).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalProperties : { type : 'number' } }).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalProperties : { type : 'string' } }).errors.length, 0);
| equal(env.validate(['1', '2'], { items : { type : 'string' }, additionalProperties : false }).errors.length, 0);
| equal(env.validate(['1', '2'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
| equal(env.validate(['1', '2', 3], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
| equal(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
|
| if (curDraftId < 3) {
| notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
| notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
| }
| });
|
| test("Items Validation", function () {
| equal(env.validate([], { type : 'array', items : { type : 'string' } }).errors.length, 0);
| equal(env.validate(['foo'], { type : 'array', items : { type : 'string' } }).errors.length, 0);
| equal(env.validate(['foo', 2], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
|
| notEqual(env.validate([1], { type : 'array', items : { type : 'string' } }).errors.length, 0);
| notEqual(env.validate(['foo', 'two'], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
| });
|
| if (curDraftId > 2) {
| test("AdditionalItems Validation", function () {
| //array tests
| equal(env.validate([1, 2, 3], {}).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalItems : true }).errors.length, 0);
| equal(env.validate([1, 2, 3], { additionalItems : { type : 'number' } }).errors.length, 0);
| equal(env.validate(['1', '2'], { items : { type : 'string' }, additionalItems : false }).errors.length, 0);
| equal(env.validate(['1', '2'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : false }).errors.length, 0);
| equal(env.validate(['1', '2', 3], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
| equal(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
|
| notEqual(env.validate([1, 2, 3], { additionalItems : false }).errors.length, 0);
| notEqual(env.validate([1, 2, 3], { additionalItems : { type : 'string' } }).errors.length, 0);
| notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : false }).errors.length, 0);
| notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
| });
| }
|
| (function (curDraftId) {
| test("Optional Validation", function () {
| equal(env.validate({}, { properties : { a : { optional : true } } }).errors.length, 0);
| equal(env.validate({ a : false }, { properties : { a : { optional : true } } }).errors.length, 0);
| equal(env.validate({ a : false }, { properties : { a : { optional : false } } }).errors.length, 0);
|
| notEqual(env.validate({}, { properties : { a : { optional : false } } }).errors.length, 0);
| notEqual(env.validate({ b : true }, { properties : { a : { optional : false } } }).errors.length, 0);
| if (curDraftId < 3) {
| notEqual(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
| } else {
| equal(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
| }
| });
| }(curDraftId));
|
| if (curDraftId > 2) {
| test("Required Validation", function () {
| equal(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
| equal(env.validate({}, { properties : { a : { required : false } } }).errors.length, 0);
| equal(env.validate({ a : false }, { properties : { a : { required : false } } }).errors.length, 0);
| equal(env.validate({ a : false }, { properties : { a : { required : true } } }).errors.length, 0);
|
| notEqual(env.validate({}, { properties : { a : { required : true } } }).errors.length, 0);
| notEqual(env.validate({ b : true }, { properties : { a : { required : true } } }).errors.length, 0);
| });
| }
|
| test("Requires Validation", function () {
| equal(env.validate({ a : 1 }, { properties : { a : { requires : 'a' } } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { properties : { a : {}, b : { requires : 'a' } } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'a' } } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { properties : { b : { requires : { properties : { a : { type : 'number' } } } } } }).errors.length, 0);
|
| notEqual(env.validate({ b : 2 }, { properties : { b : { requires : 'a' } } }).errors.length, 0);
| notEqual(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'c' } } }).errors.length, 0);
| notEqual(env.validate({ b : 2 }, { properties : { b : { requires : { properties : { b : { type : 'string' } } } } } }).errors.length, 0);
| });
|
| if (curDraftId > 2) {
| test("Dependencies Validation", function () {
| equal(env.validate(null, { dependencies : {} }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { dependencies : {} }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { dependencies : { a : 'b' } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { dependencies : { c : 'd' } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2, c : 3 }, { dependencies : { a : ['b'], b : ['a', 'c'] } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { dependencies : { a : { properties : { b : { type : 'number', required : true } } } } }).errors.length, 0);
|
| notEqual(env.validate({ a : 1, b : 2 }, { dependencies : { a : 'c' } }).errors.length, 0);
| notEqual(env.validate({ a : 1, b : 2, c : 3 }, { dependencies : { a : ['b'], b : ['a', 'd'] } }).errors.length, 0);
| notEqual(env.validate({ a : 1, b : 2 }, { dependencies : { a : { properties : { b : { type : 'string', required : true } } } } }).errors.length, 0);
| });
| }
|
| test("Minimum/Maximum Validation", function () {
| equal(env.validate(0, {}).errors.length, 0);
| equal(env.validate(1, { minimum : 1, maximum : 10 }).errors.length, 0);
| equal(env.validate(5, { minimum : 1, maximum : 10 }).errors.length, 0);
| equal(env.validate(10, { minimum : 1, maximum : 10 }).errors.length, 0);
| equal(env.validate(1, { minimum : 1, maximum : 1 }).errors.length, 0);
|
| notEqual(env.validate(0, { minimum : 1, maximum : 10 }).errors.length, 0);
| notEqual(env.validate(11, { minimum : 1, maximum : 10 }).errors.length, 0);
| });
|
| test("MinimumCanEqual/MaximumCanEqual Validation", function () {
| //true
| notEqual(env.validate(0, { minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0); //illegal
| equal(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
| equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
| equal(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
| equal(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
|
| notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
| notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
|
| //false
| notEqual(env.validate(0, { minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0); //illegal
| equal(env.validate(1.0001, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| equal(env.validate(9.9999, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
|
| notEqual(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| notEqual(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| notEqual(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
| });
|
| if (curDraftId > 2) {
| test("ExclusiveMinimum/ExclusiveMaximum Validation", function () {
| //true
| notEqual(env.validate(0, { exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0); //illegal
| equal(env.validate(1, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
| equal(env.validate(5, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
| equal(env.validate(10, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
| equal(env.validate(1, { minimum : 1, maximum : 1, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
|
| notEqual(env.validate(0, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
| notEqual(env.validate(11, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
|
| //false
| notEqual(env.validate(0, { exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0); //illegal
| equal(env.validate(1.0001, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| equal(env.validate(5, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| equal(env.validate(9.9999, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
|
| notEqual(env.validate(1, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| notEqual(env.validate(10, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| notEqual(env.validate(1, { minimum : 1, maximum : 1, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| notEqual(env.validate(0, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| notEqual(env.validate(11, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
| });
| }
|
| test("MinItems/MaxItems Validation", function () {
| equal(env.validate([], {}).errors.length, 0);
| equal(env.validate([1], { minItems : 1, maxItems : 1 }).errors.length, 0);
| equal(env.validate([1], { minItems : 1, maxItems : 3 }).errors.length, 0);
| equal(env.validate([1, 2], { minItems : 1, maxItems : 3 }).errors.length, 0);
| equal(env.validate([1, 2, 3], { minItems : 1, maxItems : 3 }).errors.length, 0);
|
| notEqual(env.validate([], { minItems : 1, maxItems : 0 }).errors.length, 0);
| notEqual(env.validate([], { minItems : 1, maxItems : 3 }).errors.length, 0);
| notEqual(env.validate([1, 2, 3, 4], { minItems : 1, maxItems : 3 }).errors.length, 0);
| });
|
| if (curDraftId > 1) {
| test("UniqueItems Validation", function () {
| equal(env.validate([], {}).errors.length, 0);
| equal(env.validate([], { uniqueItems : true }).errors.length, 0);
| equal(env.validate([null], { uniqueItems : true }).errors.length, 0);
| equal(env.validate([true, false], { uniqueItems : true }).errors.length, 0);
| equal(env.validate([1, 2, 3], { uniqueItems : true }).errors.length, 0);
| equal(env.validate(['a', 'b'], { uniqueItems : true }).errors.length, 0);
| equal(env.validate([[], []], { uniqueItems : true }).errors.length, 0);
| equal(env.validate([{}, {}], { uniqueItems : true }).errors.length, 0);
|
| notEqual(env.validate([null, null], { uniqueItems : true }).errors.length, 0);
| notEqual(env.validate([false, false], { uniqueItems : true }).errors.length, 0);
| notEqual(env.validate([1, 2, 1], { uniqueItems : true }).errors.length, 0);
| notEqual(env.validate(['a', 'b', 'b'], { uniqueItems : true }).errors.length, 0);
| });
| }
|
| test("Pattern Validation", function () {
| equal(env.validate('', {}).errors.length, 0);
| equal(env.validate('', { pattern : '^$' }).errors.length, 0);
| equal(env.validate('today', { pattern : 'day' }).errors.length, 0);
|
| notEqual(env.validate('', { pattern : '^ $' }).errors.length, 0);
| notEqual(env.validate('today', { pattern : 'dam' }).errors.length, 0);
| notEqual(env.validate('aaaaa', { pattern : 'aa(a' }).errors.length, 0);
| });
|
| test("MinLength/MaxLength Validation", function () {
| equal(env.validate('', {}).errors.length, 0);
| equal(env.validate('1', { minLength : 1, maxLength : 1 }).errors.length, 0);
| equal(env.validate('1', { minLength : 1, maxLength : 3 }).errors.length, 0);
| equal(env.validate('12', { minLength : 1, maxLength : 3 }).errors.length, 0);
| equal(env.validate('123', { minLength : 1, maxLength : 3 }).errors.length, 0);
|
| notEqual(env.validate('', { minLength : 1, maxLength : 0 }).errors.length, 0);
| notEqual(env.validate('', { minLength : 1, maxLength : 3 }).errors.length, 0);
| notEqual(env.validate('1234', { minLength : 1, maxLength : 3 }).errors.length, 0);
| });
|
| test("Enum Validation", function () {
| equal(env.validate(null, {}).errors.length, 0);
| equal(env.validate(true, { 'enum' : [false, true] }).errors.length, 0);
| equal(env.validate(2, { 'enum' : [1, 2, 3] }).errors.length, 0);
| equal(env.validate('a', { 'enum' : ['a'] }).errors.length, 0);
| equal(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : true, 'required' : false } } }).errors.length, 0);
|
| notEqual(env.validate(true, { 'enum' : ['false', 'true'] }).errors.length, 0);
| notEqual(env.validate(4, { 'enum' : [1, 2, 3, '4'] }).errors.length, 0);
| notEqual(env.validate('', { 'enum' : [] }).errors.length, 0);
| notEqual(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : false, 'required' : true } } }).errors.length, 0);
| });
|
| test("Format Validation", function () {
| //TODO
| });
|
| test("MaxDecimal Validation", function () {
| equal(env.validate(0, {}).errors.length, 0);
| equal(env.validate(0, { maxDecimal : 0 }).errors.length, 0);
| equal(env.validate(0, { maxDecimal : 1 }).errors.length, 0);
| equal(env.validate(0.22, { maxDecimal : 2 }).errors.length, 0);
| equal(env.validate(0.33, { maxDecimal : 3 }).errors.length, 0);
|
| notEqual(env.validate(0.1, { maxDecimal : 0 }).errors.length, 0);
| notEqual(env.validate(0.111, { maxDecimal : 1 }).errors.length, 0);
| });
|
| if (curDraftId > 1) {
| test("DivisibleBy Validation", function () {
| equal(env.validate(0, {}).errors.length, 0);
| equal(env.validate(0, { divisibleBy : 1 }).errors.length, 0);
| equal(env.validate(10, { divisibleBy : 5 }).errors.length, 0);
| equal(env.validate(10, { divisibleBy : 10 }).errors.length, 0);
| equal(env.validate(0, { divisibleBy : 2.5 }).errors.length, 0);
| equal(env.validate(5, { divisibleBy : 2.5 }).errors.length, 0);
| equal(env.validate(7.5, { divisibleBy : 2.5 }).errors.length, 0);
| equal(env.validate(9.1, { divisibleBy : 1.3 }).errors.length, 0);
|
| notEqual(env.validate(0, { divisibleBy : 0 }).errors.length, 0);
| notEqual(env.validate(7, { divisibleBy : 5 }).errors.length, 0);
| notEqual(env.validate(4.5, { divisibleBy : 2 }).errors.length, 0);
| notEqual(env.validate(7.5, { divisibleBy : 1.8 }).errors.length, 0);
| });
| }
|
| test("Disallow Validation", function () {
| equal(env.validate({}, { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
| equal(env.validate([], { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'object'] }).errors.length, 0, "Array");
| equal(env.validate('', { disallow : ['null', 'boolean', 'number', 'integer', 'array', 'object'] }).errors.length, 0, "String");
| equal(env.validate(0.1, { disallow : ['null', 'boolean', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Number");
| equal(env.validate(00, { disallow : ['null', 'boolean', 'string', 'array', 'object'] }).errors.length, 0, "Integer");
| equal(env.validate(false, { disallow : ['null', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Boolean");
| equal(env.validate(null, { disallow : ['boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Null");
|
| notEqual(env.validate({}, { disallow : 'object' }).errors.length, 0, "Object");
| notEqual(env.validate([], { disallow : 'array' }).errors.length, 0, "Array");
| notEqual(env.validate('', { disallow : 'string' }).errors.length, 0, "String");
| notEqual(env.validate(00, { disallow : 'integer' }).errors.length, 0, "Number");
| notEqual(env.validate(0.1, { disallow : 'number' }).errors.length, 0, "Integer");
| notEqual(env.validate(false, { disallow : 'boolean' }).errors.length, 0, "Boolean");
| notEqual(env.validate(null, { disallow : 'null' }).errors.length, 0, "Null");
| notEqual(env.validate(null, { disallow : 'any' }).errors.length, 0, "Any");
| });
|
| test("Extends Validation", function () {
| equal(env.validate({}, { 'extends' : {} }).errors.length, 0);
| equal(env.validate({}, { 'extends' : { type : 'object' } }).errors.length, 0);
| equal(env.validate(1, { type : 'integer', 'extends' : { type : 'number' } }).errors.length, 0);
| equal(env.validate({ a : 1, b : 2 }, { properties : { a : { type : 'number' } }, additionalProperties : false, 'extends' : { properties : { b : { type : 'number' } } } }).errors.length, 0);
|
| notEqual(env.validate(1, { type : 'number', 'extends' : { type : 'string' } }).errors.length, 0);
|
| //TODO: More tests
| });
|
| test("JSON Schema Validation", function () {
| var schema = env.findSchema(env.getOption("latestJSONSchemaSchemaURI"));
| var hyperSchema = env.findSchema(env.getOption("latestJSONSchemaHyperSchemaURI"));
| var links = env.findSchema(env.getOption("latestJSONSchemaLinksURI"));
|
| equal(schema.validate(schema).errors.length, 0, "schema.validate(schema)");
| equal(hyperSchema.validate(schema).errors.length, 0, "hyperSchema.validate(schema)");
| equal(hyperSchema.validate(hyperSchema).errors.length, 0, "hyperSchema.validate(hyperSchema)");
| equal(hyperSchema.validate(links).errors.length, 0, "hyperSchema.validate(links)");
| });
|
| (function(curDraftId){
| test("Links Validation", function () {
| var schema, instance;
| //full
| equal(env.validate({ 'a' : {} }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
| notEqual(env.validate({ 'a' : 1 }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
|
| //describedby
| okNoError(function () {
| schema = env.createSchema({ "id" : "http://test.example.com/3", "properties" : { "test" : { "type" : "object" } }, "extends" : { "$ref" : "http://json-schema.org/draft-03/schema#" } }, null, "http://test.example.com/3");
| equal(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : {} }).errors.length, 0);
| notEqual(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : 0 }).errors.length, 0);
| }, "describedby schema");
|
| //self
| okNoError(function () {
| schema = env.createSchema({ "properties" : { "two" : { "id" : "http://test.example.com/2", "type" : "object" } } }, null, "http://not.example.com/2");
| equal(env.validate({}, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
| notEqual(env.validate(null, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
| }, "self schema");
|
| //links api
| okNoError(function () {
| schema = env.createSchema({ "links" : [ { "rel" : "bar", "href" : "http:" + (curDraftId < 3 ? "{-this}" : "{@}") + "#" } ] });
| instance = env.createInstance("foo");
| equal(schema.getLink("bar", instance), "http:foo#", "'bar' link and self reference");
| }, "links api schema");
|
| //invalid reference
| (env.getOption("enforceReferences") ? okError : okNoError)(function () {
| schema = env.createSchema({ "$ref" : "asdf:qwerty" }); //should throw error
| }, "invalid reference");
| });
| }(curDraftId));
|
| test("PathStart Validation", function () {
| var instance = env.createInstance({}, "http://test.example.com/4"),
| schema = env.createSchema({"pathStart" : "http://test.example.com"});
|
| equal(env.validate(instance, schema).errors.length, 0);
|
| instance = env.createInstance({}); //random URI
|
| notEqual(env.validate(instance, schema).errors.length, 0);
| });
|
| test("Register Schemas", function () {
| var schema = env.createSchema({'type' : 'string'}, null, 'http://test.example.com/1');
| equal(env.findSchema(env.getOption("latestJSONSchemaHyperSchemaURI")).validate(schema).errors.length, 0);
| equal(env.validate('', { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
| notEqual(env.validate({}, { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
| });
|
| test("Complex Examples", function () {
| //example 1
| var schema = env.createSchema({
| "id":"Common#",
| "type":"object",
| "properties":{
| "!":{"type":"string","enum":["Common"]}
| },
| "additionalProperties":false
| }, undefined, "Common#");
|
| var report = env.validate({
| "!" : "List",
| "list" : [
| {
| "!" : "Text",
| "common" : {"!":"NotCommon"}
| }
| ],
| "common" : {"!":"Common"}
| },
|
| {
| "properties":{
| "!":{"type":"string","enum":["List"]},
| "list":{
| "type":"array",
| "items":{
| "type":[
| {
| "type":"object",
| "properties":{
| "!":{"type":"string","enum":["Music"]},
| "common":{"$ref":"Common#"}
| }
| },
| {
| "type":"object",
| "properties":{
| "!":{"type":"string","enum":["Text"]},
| "common":{"$ref":"Common#"}
| }
| }
| ]
| }
| },
|
| "common":{"$ref":"Common#"}
| }
| }
| );
|
| notEqual(report.errors.length, 0, "example 1");
|
| //example 2
| schema = env.createSchema({
| "extends": {
| "type": "object",
| "properties": {
| "id": {
| "type": "string",
| "minLength": 1,
| "pattern": "^\\S.+\\S$"
| }
| }
| },
| "properties": {
| "role": {
| "extends": {
| "type": "string",
| "minLength": 1,
| "pattern": "^\\S.+\\S$"
| },
| "description": "some description"
| }
| }
| });
|
| report = env.validate({ "id" : "some id", "role" : "yunowork?"}, schema);
|
| equal(report.errors.length, 0, "example 2");
| });
|
| }
|
|