json-schema 0.2.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,832 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class JSONSchemaDraft2Test < Test::Unit::TestCase
5
+ def test_types
6
+ # Set up the default datatype
7
+ schema = {
8
+ "properties" => {
9
+ "a" => {}
10
+ }
11
+ }
12
+ data = {
13
+ "a" => nil
14
+ }
15
+
16
+ # Test integers
17
+ schema["properties"]["a"]["type"] = "integer"
18
+ data["a"] = 5
19
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
20
+
21
+ data["a"] = 5.2
22
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
23
+
24
+ data['a'] = 'string'
25
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
26
+
27
+ data['a'] = true
28
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
29
+
30
+
31
+ # Test numbers
32
+ schema["properties"]["a"]["type"] = "number"
33
+ data["a"] = 5
34
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
35
+
36
+ data["a"] = 5.2
37
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
38
+
39
+ data['a'] = 'string'
40
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
41
+
42
+ data['a'] = true
43
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
44
+
45
+
46
+ # Test strings
47
+ schema["properties"]["a"]["type"] = "string"
48
+ data["a"] = 5
49
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
50
+
51
+ data["a"] = 5.2
52
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
53
+
54
+ data['a'] = 'string'
55
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
56
+
57
+ data['a'] = true
58
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
59
+
60
+
61
+ # Test booleans
62
+ schema["properties"]["a"]["type"] = "boolean"
63
+ data["a"] = 5
64
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
65
+
66
+ data["a"] = 5.2
67
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
68
+
69
+ data['a'] = 'string'
70
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
71
+
72
+ data['a'] = true
73
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
74
+
75
+ data['a'] = false
76
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
77
+
78
+
79
+ # Test object
80
+ schema["properties"]["a"]["type"] = "object"
81
+ data["a"] = {}
82
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
83
+
84
+ data["a"] = 5.2
85
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
86
+
87
+ data['a'] = 'string'
88
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
89
+
90
+ data['a'] = true
91
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
92
+
93
+
94
+ # Test array
95
+ schema["properties"]["a"]["type"] = "array"
96
+ data["a"] = []
97
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
98
+
99
+ data["a"] = 5.2
100
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
101
+
102
+ data['a'] = 'string'
103
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
104
+
105
+ data['a'] = true
106
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
107
+
108
+
109
+ # Test null
110
+ schema["properties"]["a"]["type"] = "null"
111
+ data["a"] = nil
112
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
113
+
114
+ data["a"] = 5.2
115
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
116
+
117
+ data['a'] = 'string'
118
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
119
+
120
+ data['a'] = true
121
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
122
+
123
+
124
+ # Test any
125
+ schema["properties"]["a"]["type"] = "any"
126
+ data["a"] = 5
127
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
128
+
129
+ data["a"] = 5.2
130
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
131
+
132
+ data['a'] = 'string'
133
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
134
+
135
+ data['a'] = true
136
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
137
+
138
+
139
+ # Test a union type
140
+ schema["properties"]["a"]["type"] = ["integer","string"]
141
+ data["a"] = 5
142
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
143
+
144
+ data["a"] = 'boo'
145
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
146
+
147
+ data["a"] = false
148
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
149
+
150
+ # Test a union type with schemas
151
+ schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
152
+
153
+ data["a"] = "test"
154
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
155
+
156
+ data["a"] = 5
157
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
158
+
159
+ data["a"] = {"b" => 5}
160
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
161
+
162
+ data["a"] = {"b" => "taco"}
163
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
164
+ end
165
+
166
+
167
+
168
+ def test_optional
169
+ # Set up the default datatype
170
+ schema = {
171
+ "properties" => {
172
+ "a" => {"type" => "string"}
173
+ }
174
+ }
175
+ data = {}
176
+
177
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
178
+ data['a'] = "Hello"
179
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
180
+
181
+ schema = {
182
+ "properties" => {
183
+ "a" => {"type" => "integer", "optional" => "true"}
184
+ }
185
+ }
186
+
187
+ data = {}
188
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
189
+
190
+ end
191
+
192
+
193
+
194
+ def test_minimum
195
+ # Set up the default datatype
196
+ schema = {
197
+ "properties" => {
198
+ "a" => {"minimum" => 5}
199
+ }
200
+ }
201
+
202
+ data = {
203
+ "a" => nil
204
+ }
205
+
206
+
207
+ # Test an integer
208
+ data["a"] = 5
209
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
210
+
211
+ data["a"] = 4
212
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
213
+
214
+ # Test a float
215
+ data["a"] = 5.0
216
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
217
+
218
+ data["a"] = 4.9
219
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
220
+
221
+ # Test a non-number
222
+ data["a"] = "a string"
223
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
224
+
225
+ # Test exclusiveMinimum
226
+ schema["properties"]["a"]["minimumCanEqual"] = false
227
+
228
+ data["a"] = 6
229
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
230
+
231
+ data["a"] = 5
232
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
233
+
234
+ # Test with float
235
+ data["a"] = 5.00000001
236
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
237
+
238
+ data["a"] = 5.0
239
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
240
+ end
241
+
242
+
243
+
244
+ def test_maximum
245
+ # Set up the default datatype
246
+ schema = {
247
+ "properties" => {
248
+ "a" => {"maximum" => 5}
249
+ }
250
+ }
251
+
252
+ data = {
253
+ "a" => nil
254
+ }
255
+
256
+
257
+ # Test an integer
258
+ data["a"] = 5
259
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
260
+
261
+ data["a"] = 6
262
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
263
+
264
+ # Test a float
265
+ data["a"] = 5.0
266
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
267
+
268
+ data["a"] = 5.1
269
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
270
+
271
+ # Test a non-number
272
+ data["a"] = "a string"
273
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
274
+
275
+ # Test exclusiveMinimum
276
+ schema["properties"]["a"]["maximumCanEqual"] = false
277
+
278
+ data["a"] = 4
279
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
280
+
281
+ data["a"] = 5
282
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
283
+
284
+ # Test with float
285
+ data["a"] = 4.9999999
286
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
287
+
288
+ data["a"] = 5.0
289
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
290
+ end
291
+
292
+
293
+ def test_min_items
294
+ # Set up the default datatype
295
+ schema = {
296
+ "properties" => {
297
+ "a" => {"minItems" => 1}
298
+ }
299
+ }
300
+
301
+ data = {
302
+ "a" => nil
303
+ }
304
+
305
+ # Test with an array
306
+ data["a"] = ["boo"]
307
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
308
+
309
+ data["a"] = []
310
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
311
+
312
+ # Test with a non-array
313
+ data["a"] = "boo"
314
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
315
+ end
316
+
317
+
318
+
319
+ def test_max_items
320
+ # Set up the default datatype
321
+ schema = {
322
+ "properties" => {
323
+ "a" => {"maxItems" => 1}
324
+ }
325
+ }
326
+
327
+ data = {
328
+ "a" => nil
329
+ }
330
+
331
+ # Test with an array
332
+ data["a"] = ["boo"]
333
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
334
+
335
+ data["a"] = ["boo","taco"]
336
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
337
+
338
+ # Test with a non-array
339
+ data["a"] = "boo"
340
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
341
+ end
342
+
343
+
344
+
345
+ def test_unique_items
346
+ # Set up the default datatype
347
+ schema = {
348
+ "properties" => {
349
+ "a" => {"uniqueItems" => true}
350
+ }
351
+ }
352
+
353
+ data = {
354
+ "a" => nil
355
+ }
356
+
357
+ # Test with nulls
358
+ data["a"] = [nil,5]
359
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
360
+
361
+ data["a"] = [nil,nil]
362
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
363
+
364
+ # Test with booleans
365
+ data["a"] = [true,4]
366
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
367
+
368
+ data["a"] = [true,false]
369
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
370
+
371
+ data["a"] = [true,true]
372
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
373
+
374
+ # Test with numbers
375
+ data["a"] = [4,true]
376
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
377
+
378
+ data["a"] = [4,4.1]
379
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
380
+
381
+ data["a"] = [4,4]
382
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
383
+
384
+ # Test with strings
385
+ data["a"] = ['a',true]
386
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
387
+
388
+ data["a"] = ['a','ab']
389
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
390
+
391
+ data["a"] = ['a','a']
392
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
393
+
394
+ # Test with arrays
395
+ data["a"] = [[1],true]
396
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
397
+
398
+ data["a"] = [[1,2],[1,3]]
399
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
400
+
401
+ data["a"] = [[1,2,3],[1,2,3]]
402
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
403
+
404
+ # Test with objects
405
+ data["a"] = [{"a" => 1},true]
406
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
407
+
408
+ data["a"] = [{"a" => 1},{"a" => 2}]
409
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
410
+
411
+ data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
412
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
413
+ end
414
+
415
+
416
+ def test_pattern
417
+ # Set up the default datatype
418
+ schema = {
419
+ "properties" => {
420
+ "a" => {"pattern" => "\\d+ taco"}
421
+ }
422
+ }
423
+
424
+ data = {
425
+ "a" => nil
426
+ }
427
+
428
+ # Test strings
429
+ data["a"] = "156 taco bell"
430
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
431
+
432
+ # Test a non-string
433
+ data["a"] = 5
434
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
435
+
436
+ data["a"] = "taco"
437
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
438
+ end
439
+
440
+
441
+ def test_min_length
442
+ # Set up the default datatype
443
+ schema = {
444
+ "properties" => {
445
+ "a" => {"minLength" => 1}
446
+ }
447
+ }
448
+
449
+ data = {
450
+ "a" => nil
451
+ }
452
+
453
+ # Try out strings
454
+ data["a"] = "t"
455
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
456
+
457
+ data["a"] = ""
458
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
459
+
460
+ # Try out non-string
461
+ data["a"] = 5
462
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
463
+ end
464
+
465
+
466
+ def test_max_length
467
+ # Set up the default datatype
468
+ schema = {
469
+ "properties" => {
470
+ "a" => {"maxLength" => 1}
471
+ }
472
+ }
473
+
474
+ data = {
475
+ "a" => nil
476
+ }
477
+
478
+ # Try out strings
479
+ data["a"] = "t"
480
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
481
+
482
+ data["a"] = "tt"
483
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
484
+
485
+ # Try out non-string
486
+ data["a"] = 5
487
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
488
+ end
489
+
490
+
491
+ def test_enum
492
+ # Set up the default datatype
493
+ schema = {
494
+ "properties" => {
495
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
496
+ }
497
+ }
498
+
499
+ data = {
500
+ "a" => nil
501
+ }
502
+
503
+ # Make sure all of the above are valid...
504
+ data["a"] = 1
505
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
506
+
507
+ data["a"] = 'boo'
508
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
509
+
510
+ data["a"] = [1,2,3]
511
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
512
+
513
+ data["a"] = {"a" => "b"}
514
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
515
+
516
+ # Test something that doesn't exist
517
+ data["a"] = 'taco'
518
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
519
+
520
+ # Try it without the key
521
+ data = {}
522
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
523
+ end
524
+
525
+
526
+ def test_divisible_by
527
+ # Set up the default datatype
528
+ schema = {
529
+ "properties" => {
530
+ "a" => {"divisibleBy" => 1.1}
531
+ }
532
+ }
533
+
534
+ data = {
535
+ "a" => nil
536
+ }
537
+
538
+ data["a"] = 3.3
539
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
540
+
541
+ data["a"] = 3.4
542
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
543
+
544
+ schema["properties"]["a"]["divisibleBy"] = 2.0
545
+
546
+ data["a"] = 4.0
547
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
548
+
549
+ data["a"] = 'boo'
550
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
551
+
552
+ data["a"] = 5
553
+ schema["properties"]["a"]["divisibleBy"] = 0
554
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
555
+ end
556
+
557
+
558
+
559
+ def test_disallow
560
+ # Set up the default datatype
561
+ schema = {
562
+ "properties" => {
563
+ "a" => {"disallow" => "integer"}
564
+ }
565
+ }
566
+
567
+ data = {
568
+ "a" => nil
569
+ }
570
+
571
+
572
+ data["a"] = 'string'
573
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
574
+
575
+ data["a"] = 5
576
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
577
+
578
+
579
+ schema["properties"]["a"]["disallow"] = ["integer","string"]
580
+ data["a"] = 'string'
581
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
582
+
583
+ data["a"] = 5
584
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
585
+
586
+ data["a"] = false
587
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
588
+
589
+ end
590
+
591
+
592
+
593
+ def test_additional_properties
594
+ # Test no additional properties allowed
595
+ schema = {
596
+ "properties" => {
597
+ "a" => { "type" => "integer" }
598
+ },
599
+ "additionalProperties" => false
600
+ }
601
+
602
+ data = {
603
+ "a" => 10
604
+ }
605
+
606
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
607
+ data["b"] = 5
608
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
609
+
610
+ # Test additional properties match a schema
611
+ schema["additionalProperties"] = { "type" => "string" }
612
+ data["b"] = "taco"
613
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
614
+ data["b"] = 5
615
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
616
+ end
617
+
618
+
619
+ def test_items
620
+ schema = {
621
+ "items" => { "type" => "integer" }
622
+ }
623
+
624
+ data = [1,2,4]
625
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
626
+ data = [1,2,"string"]
627
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
628
+
629
+ schema = {
630
+ "items" => [
631
+ {"type" => "integer"},
632
+ {"type" => "string"}
633
+ ]
634
+ }
635
+
636
+ data = [1,"string"]
637
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
638
+ data = [1,"string",3]
639
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
640
+ data = ["string",1]
641
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
642
+
643
+ end
644
+
645
+
646
+ def test_format_ipv4
647
+ schema = {
648
+ "type" => "object",
649
+ "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
650
+ }
651
+
652
+ data = {"a" => "1.1.1.1"}
653
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
654
+ data = {"a" => "1.1.1"}
655
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
656
+ data = {"a" => "1.1.1.300"}
657
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
658
+ data = {"a" => 5}
659
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
660
+ data = {"a" => "1.1.1"}
661
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
662
+ data = {"a" => "1.1.1.1b"}
663
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
664
+ data = {"a" => "b1.1.1.1"}
665
+ end
666
+
667
+
668
+ def test_format_ipv6
669
+ schema = {
670
+ "type" => "object",
671
+ "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
672
+ }
673
+
674
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
675
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
676
+ data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
677
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
678
+ data = {"a" => "1111:2222:8888::eeee:ffff"}
679
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
680
+ data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
681
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
682
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
683
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
684
+ data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
685
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
686
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
687
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
688
+ end
689
+
690
+ def test_format_time
691
+ schema = {
692
+ "type" => "object",
693
+ "properties" => { "a" => {"type" => "string", "format" => "time"}}
694
+ }
695
+
696
+ data = {"a" => "12:00:00"}
697
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
698
+ data = {"a" => "12:00"}
699
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
700
+ data = {"a" => "12:00:60"}
701
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
702
+ data = {"a" => "12:60:00"}
703
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
704
+ data = {"a" => "24:00:00"}
705
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
706
+ data = {"a" => "0:00:00"}
707
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
708
+ data = {"a" => "-12:00:00"}
709
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
710
+ data = {"a" => "12:00:00b"}
711
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
712
+ end
713
+
714
+
715
+ def test_format_date
716
+ schema = {
717
+ "type" => "object",
718
+ "properties" => { "a" => {"type" => "string", "format" => "date"}}
719
+ }
720
+
721
+ data = {"a" => "2010-01-01"}
722
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
723
+ data = {"a" => "2010-01-32"}
724
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
725
+ data = {"a" => "n2010-01-01"}
726
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
727
+ data = {"a" => "2010-1-01"}
728
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
729
+ data = {"a" => "2010-01-1"}
730
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
731
+ data = {"a" => "2010-01-01n"}
732
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
733
+ end
734
+
735
+ def test_format_datetime
736
+ schema = {
737
+ "type" => "object",
738
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
739
+ }
740
+
741
+ data = {"a" => "2010-01-01T12:00:00Z"}
742
+ assert(JSON::Validator.validate(schema,data,:version => :draft2))
743
+ data = {"a" => "2010-01-32T12:00:00Z"}
744
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
745
+ data = {"a" => "2010-13-01T12:00:00Z"}
746
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
747
+ data = {"a" => "2010-01-01T24:00:00Z"}
748
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
749
+ data = {"a" => "2010-01-01T12:60:00Z"}
750
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
751
+ data = {"a" => "2010-01-01T12:00:60Z"}
752
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
753
+ data = {"a" => "2010-01-01T12:00:00"}
754
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
755
+ data = {"a" => "2010-01-01T12:00:00z"}
756
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
757
+ data = {"a" => "2010-01-0112:00:00Z"}
758
+ assert(!JSON::Validator.validate(schema,data,:version => :draft2))
759
+ end
760
+
761
+
762
+ def test_format_strings
763
+ data1 = {"a" => "boo"}
764
+ data2 = {"a" => 5}
765
+
766
+ schema = {
767
+ "type" => "object",
768
+ "properties" => { "a" => {"format" => "regex"}}
769
+ }
770
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
771
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
772
+
773
+ schema = {
774
+ "type" => "object",
775
+ "properties" => { "a" => {"format" => "color"}}
776
+ }
777
+ assert(JSON::Validator.validate(schema,data1))
778
+ assert(!JSON::Validator.validate(schema,data2))
779
+
780
+ schema = {
781
+ "type" => "object",
782
+ "properties" => { "a" => {"format" => "style"}}
783
+ }
784
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
785
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
786
+
787
+ schema = {
788
+ "type" => "object",
789
+ "properties" => { "a" => {"format" => "phone"}}
790
+ }
791
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
792
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
793
+
794
+ schema = {
795
+ "type" => "object",
796
+ "properties" => { "a" => {"format" => "uri"}}
797
+ }
798
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
799
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
800
+
801
+ schema = {
802
+ "type" => "object",
803
+ "properties" => { "a" => {"format" => "email"}}
804
+ }
805
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
806
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
807
+
808
+ schema = {
809
+ "type" => "object",
810
+ "properties" => { "a" => {"format" => "host-name"}}
811
+ }
812
+ assert(JSON::Validator.validate(schema,data1,:version => :draft2))
813
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft2))
814
+ end
815
+
816
+
817
+ def test_format_numeric
818
+ data1 = {"a" => "boo"}
819
+ data2 = {"a" => 5}
820
+ data3 = {"a" => 5.4}
821
+
822
+ schema = {
823
+ "type" => "object",
824
+ "properties" => { "a" => {"format" => "utc-millisec"}}
825
+ }
826
+ assert(!JSON::Validator.validate(schema,data1,:version => :draft2))
827
+ assert(JSON::Validator.validate(schema,data2,:version => :draft2))
828
+ assert(JSON::Validator.validate(schema,data3,:version => :draft2))
829
+ end
830
+
831
+ end
832
+