json-schema-pvdgm 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +19 -0
  3. data/README.textile +354 -0
  4. data/lib/json-schema.rb +25 -0
  5. data/lib/json-schema/attributes/additionalitems.rb +23 -0
  6. data/lib/json-schema/attributes/additionalproperties.rb +67 -0
  7. data/lib/json-schema/attributes/allof.rb +37 -0
  8. data/lib/json-schema/attributes/anyof.rb +41 -0
  9. data/lib/json-schema/attributes/dependencies.rb +30 -0
  10. data/lib/json-schema/attributes/dependencies_v4.rb +20 -0
  11. data/lib/json-schema/attributes/disallow.rb +11 -0
  12. data/lib/json-schema/attributes/divisibleby.rb +16 -0
  13. data/lib/json-schema/attributes/enum.rb +24 -0
  14. data/lib/json-schema/attributes/extends.rb +49 -0
  15. data/lib/json-schema/attributes/format.rb +123 -0
  16. data/lib/json-schema/attributes/items.rb +25 -0
  17. data/lib/json-schema/attributes/maxdecimal.rb +15 -0
  18. data/lib/json-schema/attributes/maximum.rb +15 -0
  19. data/lib/json-schema/attributes/maximum_inclusive.rb +15 -0
  20. data/lib/json-schema/attributes/maxitems.rb +12 -0
  21. data/lib/json-schema/attributes/maxlength.rb +14 -0
  22. data/lib/json-schema/attributes/maxproperties.rb +12 -0
  23. data/lib/json-schema/attributes/minimum.rb +15 -0
  24. data/lib/json-schema/attributes/minimum_inclusive.rb +15 -0
  25. data/lib/json-schema/attributes/minitems.rb +12 -0
  26. data/lib/json-schema/attributes/minlength.rb +14 -0
  27. data/lib/json-schema/attributes/minproperties.rb +12 -0
  28. data/lib/json-schema/attributes/multipleof.rb +16 -0
  29. data/lib/json-schema/attributes/not.rb +28 -0
  30. data/lib/json-schema/attributes/oneof.rb +32 -0
  31. data/lib/json-schema/attributes/pattern.rb +15 -0
  32. data/lib/json-schema/attributes/patternproperties.rb +23 -0
  33. data/lib/json-schema/attributes/properties.rb +58 -0
  34. data/lib/json-schema/attributes/properties_optional.rb +23 -0
  35. data/lib/json-schema/attributes/properties_v4.rb +57 -0
  36. data/lib/json-schema/attributes/ref.rb +70 -0
  37. data/lib/json-schema/attributes/required.rb +23 -0
  38. data/lib/json-schema/attributes/type.rb +102 -0
  39. data/lib/json-schema/attributes/type_v4.rb +54 -0
  40. data/lib/json-schema/attributes/uniqueitems.rb +16 -0
  41. data/lib/json-schema/model_validator.rb +85 -0
  42. data/lib/json-schema/schema.rb +73 -0
  43. data/lib/json-schema/uri/file.rb +36 -0
  44. data/lib/json-schema/uri/uuid.rb +285 -0
  45. data/lib/json-schema/util/array_set.rb +14 -0
  46. data/lib/json-schema/util/hash.rb +8 -0
  47. data/lib/json-schema/validator.rb +672 -0
  48. data/lib/json-schema/validators/draft1.rb +32 -0
  49. data/lib/json-schema/validators/draft2.rb +33 -0
  50. data/lib/json-schema/validators/draft3.rb +38 -0
  51. data/lib/json-schema/validators/draft4.rb +45 -0
  52. data/resources/draft-01.json +155 -0
  53. data/resources/draft-02.json +166 -0
  54. data/resources/draft-03.json +174 -0
  55. data/resources/draft-04.json +150 -0
  56. data/test/data/all_of_ref_data.json +3 -0
  57. data/test/data/any_of_ref_data.json +7 -0
  58. data/test/data/bad_data_1.json +3 -0
  59. data/test/data/good_data_1.json +3 -0
  60. data/test/data/one_of_ref_links_data.json +5 -0
  61. data/test/schemas/all_of_ref_base_schema.json +6 -0
  62. data/test/schemas/all_of_ref_schema.json +7 -0
  63. data/test/schemas/any_of_ref_jane_schema.json +4 -0
  64. data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
  65. data/test/schemas/any_of_ref_john_schema.json +4 -0
  66. data/test/schemas/any_of_ref_schema.json +15 -0
  67. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
  68. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
  69. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
  70. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
  71. data/test/schemas/good_schema_1.json +10 -0
  72. data/test/schemas/good_schema_2.json +10 -0
  73. data/test/schemas/good_schema_extends1.json +10 -0
  74. data/test/schemas/good_schema_extends2.json +13 -0
  75. data/test/schemas/inner.schema.json +21 -0
  76. data/test/schemas/one_of_ref_links_schema.json +16 -0
  77. data/test/schemas/self_link_schema.json +17 -0
  78. data/test/schemas/up_link_schema.json +17 -0
  79. data/test/test_all_of_ref_schema.rb +11 -0
  80. data/test/test_any_of_ref_schema.rb +11 -0
  81. data/test/test_bad_schema_ref.rb +33 -0
  82. data/test/test_extended_schema.rb +68 -0
  83. data/test/test_extends_and_additionalProperties.rb +50 -0
  84. data/test/test_files_v3.rb +52 -0
  85. data/test/test_fragment_resolution.rb +31 -0
  86. data/test/test_full_validation.rb +209 -0
  87. data/test/test_jsonschema_draft1.rb +701 -0
  88. data/test/test_jsonschema_draft2.rb +773 -0
  89. data/test/test_jsonschema_draft3.rb +1236 -0
  90. data/test/test_jsonschema_draft4.rb +1356 -0
  91. data/test/test_model_validator.rb +52 -0
  92. data/test/test_one_of.rb +42 -0
  93. data/test/test_ruby_schema.rb +38 -0
  94. data/test/test_schema_type_attribute.rb +21 -0
  95. data/test/test_schema_validation.rb +85 -0
  96. metadata +180 -0
@@ -0,0 +1,1236 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class JSONSchemaDraft3Test < Test::Unit::TestCase
5
+ def test_types
6
+ # Set up the default datatype
7
+ schema = {
8
+ "$schema" => "http://json-schema.org/draft-03/schema#",
9
+ "properties" => {
10
+ "a" => {}
11
+ }
12
+ }
13
+ data = {
14
+ "a" => nil
15
+ }
16
+
17
+ # Test integers
18
+ schema["properties"]["a"]["type"] = "integer"
19
+ data["a"] = 5
20
+ assert(JSON::Validator.validate(schema,data))
21
+
22
+ data["a"] = 5.2
23
+ assert(!JSON::Validator.validate(schema,data))
24
+
25
+ data['a'] = 'string'
26
+ assert(!JSON::Validator.validate(schema,data))
27
+
28
+ data['a'] = true
29
+ assert(!JSON::Validator.validate(schema,data))
30
+
31
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'integer'}, 3))
32
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'integer'}, "hello"))
33
+
34
+ # Test numbers
35
+ schema["properties"]["a"]["type"] = "number"
36
+ data["a"] = 5
37
+ assert(JSON::Validator.validate(schema,data))
38
+
39
+ data["a"] = 5.2
40
+ assert(JSON::Validator.validate(schema,data))
41
+
42
+ data['a'] = 'string'
43
+ assert(!JSON::Validator.validate(schema,data))
44
+
45
+ data['a'] = true
46
+ assert(!JSON::Validator.validate(schema,data))
47
+
48
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, 3))
49
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, 3.14159265358979))
50
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, "hello"))
51
+
52
+
53
+ # Test strings
54
+ schema["properties"]["a"]["type"] = "string"
55
+ data["a"] = 5
56
+ assert(!JSON::Validator.validate(schema,data))
57
+
58
+ data["a"] = 5.2
59
+ assert(!JSON::Validator.validate(schema,data))
60
+
61
+ data['a'] = 'string'
62
+ assert(JSON::Validator.validate(schema,data))
63
+
64
+ data['a'] = true
65
+ assert(!JSON::Validator.validate(schema,data))
66
+
67
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, 'hello'))
68
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, 3.14159265358979))
69
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, []))
70
+
71
+
72
+ # Test booleans
73
+ schema["properties"]["a"]["type"] = "boolean"
74
+ data["a"] = 5
75
+ assert(!JSON::Validator.validate(schema,data))
76
+
77
+ data["a"] = 5.2
78
+ assert(!JSON::Validator.validate(schema,data))
79
+
80
+ data['a'] = 'string'
81
+ assert(!JSON::Validator.validate(schema,data))
82
+
83
+ data['a'] = true
84
+ assert(JSON::Validator.validate(schema,data))
85
+
86
+ data['a'] = false
87
+ assert(JSON::Validator.validate(schema,data))
88
+
89
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, true))
90
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, false))
91
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, nil))
92
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, 3))
93
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, "hello"))
94
+
95
+
96
+ # Test object
97
+ schema["properties"]["a"]["type"] = "object"
98
+ data["a"] = {}
99
+ assert(JSON::Validator.validate(schema,data))
100
+
101
+ data["a"] = 5.2
102
+ assert(!JSON::Validator.validate(schema,data))
103
+
104
+ data['a'] = 'string'
105
+ assert(!JSON::Validator.validate(schema,data))
106
+
107
+ data['a'] = true
108
+ assert(!JSON::Validator.validate(schema,data))
109
+
110
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, {'a' => true}))
111
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, {}))
112
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, []))
113
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, 3))
114
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, "hello"))
115
+
116
+
117
+ # Test array
118
+ schema["properties"]["a"]["type"] = "array"
119
+ data["a"] = []
120
+ assert(JSON::Validator.validate(schema,data))
121
+
122
+ data["a"] = 5.2
123
+ assert(!JSON::Validator.validate(schema,data))
124
+
125
+ data['a'] = 'string'
126
+ assert(!JSON::Validator.validate(schema,data))
127
+
128
+ data['a'] = true
129
+ assert(!JSON::Validator.validate(schema,data))
130
+
131
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, ['a']))
132
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, []))
133
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, {}))
134
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, 3))
135
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, "hello"))
136
+
137
+
138
+ # Test null
139
+ schema["properties"]["a"]["type"] = "null"
140
+ data["a"] = nil
141
+ assert(JSON::Validator.validate(schema,data))
142
+
143
+ data["a"] = 5.2
144
+ assert(!JSON::Validator.validate(schema,data))
145
+
146
+ data['a'] = 'string'
147
+ assert(!JSON::Validator.validate(schema,data))
148
+
149
+ data['a'] = true
150
+ assert(!JSON::Validator.validate(schema,data))
151
+
152
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, nil))
153
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, false))
154
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, []))
155
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, "hello"))
156
+
157
+
158
+ # Test any
159
+ schema["properties"]["a"]["type"] = "any"
160
+ data["a"] = 5
161
+ assert(JSON::Validator.validate(schema,data))
162
+
163
+ data["a"] = 5.2
164
+ assert(JSON::Validator.validate(schema,data))
165
+
166
+ data['a'] = 'string'
167
+ assert(JSON::Validator.validate(schema,data))
168
+
169
+ data['a'] = true
170
+ assert(JSON::Validator.validate(schema,data))
171
+
172
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, true))
173
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, nil))
174
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, {}))
175
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, 3))
176
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, "hello"))
177
+
178
+
179
+ # Test a union type
180
+ schema["properties"]["a"]["type"] = ["integer","string"]
181
+ data["a"] = 5
182
+ assert(JSON::Validator.validate(schema,data))
183
+
184
+ data["a"] = 'boo'
185
+ assert(JSON::Validator.validate(schema,data))
186
+
187
+ data["a"] = false
188
+ assert(!JSON::Validator.validate(schema,data))
189
+
190
+ assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => ['string', 'null']}, "hello"))
191
+ assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => ['integer', 'object']}, "hello"))
192
+
193
+ # Test a union type with schemas
194
+ schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
195
+
196
+ data["a"] = "test"
197
+ assert(JSON::Validator.validate(schema,data))
198
+
199
+ data["a"] = 5
200
+ assert(!JSON::Validator.validate(schema,data))
201
+
202
+ data["a"] = {"b" => 5}
203
+ assert(JSON::Validator.validate(schema,data))
204
+
205
+ data["a"] = {"b" => "taco"}
206
+ assert(!JSON::Validator.validate(schema,data))
207
+
208
+ # Test an array of unioned-type objects that prevent additionalProperties
209
+ schema["properties"]["a"] = {
210
+ 'type' => 'array',
211
+ 'items' => {
212
+ 'type' => [
213
+ { 'type' => 'object', 'properties' => { "b" => { "type" => "integer" } } },
214
+ { 'type' => 'object', 'properties' => { "c" => { "type" => "string" } } }
215
+ ],
216
+ 'additionalProperties' => false
217
+ }
218
+ }
219
+
220
+ data["a"] = [{"b" => 5}, {"c" => "foo"}]
221
+ errors = JSON::Validator.fully_validate(schema, data)
222
+ assert(errors.empty?, errors.join("\n"))
223
+
224
+ # This should actually pass, because this matches the first schema in the union
225
+ data["a"] << {"c" => false}
226
+ assert(JSON::Validator.validate(schema,data))
227
+ end
228
+
229
+ def test_required
230
+ # Set up the default datatype
231
+ schema = {
232
+ "$schema" => "http://json-schema.org/draft-03/schema#",
233
+ "properties" => {
234
+ "a" => {"required" => true}
235
+ }
236
+ }
237
+ data = {}
238
+
239
+ assert(!JSON::Validator.validate(schema,data))
240
+ data['a'] = "Hello"
241
+ assert(JSON::Validator.validate(schema,data))
242
+
243
+ schema = {
244
+ "$schema" => "http://json-schema.org/draft-03/schema#",
245
+ "properties" => {
246
+ "a" => {"type" => "integer"}
247
+ }
248
+ }
249
+
250
+ data = {}
251
+ assert(JSON::Validator.validate(schema,data))
252
+
253
+ end
254
+
255
+
256
+
257
+ def test_minimum
258
+ # Set up the default datatype
259
+ schema = {
260
+ "$schema" => "http://json-schema.org/draft-03/schema#",
261
+ "properties" => {
262
+ "a" => {"minimum" => 5}
263
+ }
264
+ }
265
+
266
+ data = {
267
+ "a" => nil
268
+ }
269
+
270
+
271
+ # Test an integer
272
+ data["a"] = 5
273
+ assert(JSON::Validator.validate(schema,data))
274
+
275
+ data["a"] = 4
276
+ assert(!JSON::Validator.validate(schema,data))
277
+
278
+ # Test a float
279
+ data["a"] = 5.0
280
+ assert(JSON::Validator.validate(schema,data))
281
+
282
+ data["a"] = 4.9
283
+ assert(!JSON::Validator.validate(schema,data))
284
+
285
+ # Test a non-number
286
+ data["a"] = "a string"
287
+ assert(JSON::Validator.validate(schema,data))
288
+
289
+ # Test exclusiveMinimum
290
+ schema["properties"]["a"]["exclusiveMinimum"] = true
291
+
292
+ data["a"] = 6
293
+ assert(JSON::Validator.validate(schema,data))
294
+
295
+ data["a"] = 5
296
+ assert(!JSON::Validator.validate(schema,data))
297
+
298
+ # Test with float
299
+ data["a"] = 5.00000001
300
+ assert(JSON::Validator.validate(schema,data))
301
+
302
+ data["a"] = 5.0
303
+ assert(!JSON::Validator.validate(schema,data))
304
+ end
305
+
306
+
307
+
308
+ def test_maximum
309
+ # Set up the default datatype
310
+ schema = {
311
+ "$schema" => "http://json-schema.org/draft-03/schema#",
312
+ "properties" => {
313
+ "a" => {"maximum" => 5}
314
+ }
315
+ }
316
+
317
+ data = {
318
+ "a" => nil
319
+ }
320
+
321
+
322
+ # Test an integer
323
+ data["a"] = 5
324
+ assert(JSON::Validator.validate(schema,data))
325
+
326
+ data["a"] = 6
327
+ assert(!JSON::Validator.validate(schema,data))
328
+
329
+ # Test a float
330
+ data["a"] = 5.0
331
+ assert(JSON::Validator.validate(schema,data))
332
+
333
+ data["a"] = 5.1
334
+ assert(!JSON::Validator.validate(schema,data))
335
+
336
+ # Test a non-number
337
+ data["a"] = "a string"
338
+ assert(JSON::Validator.validate(schema,data))
339
+
340
+ # Test exclusiveMinimum
341
+ schema["properties"]["a"]["exclusiveMaximum"] = true
342
+
343
+ data["a"] = 4
344
+ assert(JSON::Validator.validate(schema,data))
345
+
346
+ data["a"] = 5
347
+ assert(!JSON::Validator.validate(schema,data))
348
+
349
+ # Test with float
350
+ data["a"] = 4.9999999
351
+ assert(JSON::Validator.validate(schema,data))
352
+
353
+ data["a"] = 5.0
354
+ assert(!JSON::Validator.validate(schema,data))
355
+ end
356
+
357
+
358
+ def test_min_items
359
+ # Set up the default datatype
360
+ schema = {
361
+ "$schema" => "http://json-schema.org/draft-03/schema#",
362
+ "properties" => {
363
+ "a" => {"minItems" => 1}
364
+ }
365
+ }
366
+
367
+ data = {
368
+ "a" => nil
369
+ }
370
+
371
+ # Test with an array
372
+ data["a"] = ["boo"]
373
+ assert(JSON::Validator.validate(schema,data))
374
+
375
+ data["a"] = []
376
+ assert(!JSON::Validator.validate(schema,data))
377
+
378
+ # Test with a non-array
379
+ data["a"] = "boo"
380
+ assert(JSON::Validator.validate(schema,data))
381
+ end
382
+
383
+
384
+
385
+ def test_max_items
386
+ # Set up the default datatype
387
+ schema = {
388
+ "$schema" => "http://json-schema.org/draft-03/schema#",
389
+ "properties" => {
390
+ "a" => {"maxItems" => 1}
391
+ }
392
+ }
393
+
394
+ data = {
395
+ "a" => nil
396
+ }
397
+
398
+ # Test with an array
399
+ data["a"] = ["boo"]
400
+ assert(JSON::Validator.validate(schema,data))
401
+
402
+ data["a"] = ["boo","taco"]
403
+ assert(!JSON::Validator.validate(schema,data))
404
+
405
+ # Test with a non-array
406
+ data["a"] = "boo"
407
+ assert(JSON::Validator.validate(schema,data))
408
+ end
409
+
410
+
411
+
412
+ def test_unique_items
413
+ # Set up the default datatype
414
+ schema = {
415
+ "$schema" => "http://json-schema.org/draft-03/schema#",
416
+ "properties" => {
417
+ "a" => {"uniqueItems" => true}
418
+ }
419
+ }
420
+
421
+ data = {
422
+ "a" => nil
423
+ }
424
+
425
+ # Test with nulls
426
+ data["a"] = [nil,5]
427
+ assert(JSON::Validator.validate(schema,data))
428
+
429
+ data["a"] = [nil,nil]
430
+ assert(!JSON::Validator.validate(schema,data))
431
+
432
+ # Test with booleans
433
+ data["a"] = [true,4]
434
+ assert(JSON::Validator.validate(schema,data))
435
+
436
+ data["a"] = [true,false]
437
+ assert(JSON::Validator.validate(schema,data))
438
+
439
+ data["a"] = [true,true]
440
+ assert(!JSON::Validator.validate(schema,data))
441
+
442
+ # Test with numbers
443
+ data["a"] = [4,true]
444
+ assert(JSON::Validator.validate(schema,data))
445
+
446
+ data["a"] = [4,4.1]
447
+ assert(JSON::Validator.validate(schema,data))
448
+
449
+ data["a"] = [4,4]
450
+ assert(!JSON::Validator.validate(schema,data))
451
+
452
+ # Test with strings
453
+ data["a"] = ['a',true]
454
+ assert(JSON::Validator.validate(schema,data))
455
+
456
+ data["a"] = ['a','ab']
457
+ assert(JSON::Validator.validate(schema,data))
458
+
459
+ data["a"] = ['a','a']
460
+ assert(!JSON::Validator.validate(schema,data))
461
+
462
+ # Test with arrays
463
+ data["a"] = [[1],true]
464
+ assert(JSON::Validator.validate(schema,data))
465
+
466
+ data["a"] = [[1,2],[1,3]]
467
+ assert(JSON::Validator.validate(schema,data))
468
+
469
+ data["a"] = [[1,2,3],[1,2,3]]
470
+ assert(!JSON::Validator.validate(schema,data))
471
+
472
+ # Test with objects
473
+ data["a"] = [{"a" => 1},true]
474
+ assert(JSON::Validator.validate(schema,data))
475
+
476
+ data["a"] = [{"a" => 1},{"a" => 2}]
477
+ assert(JSON::Validator.validate(schema,data))
478
+
479
+ data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
480
+ assert(!JSON::Validator.validate(schema,data))
481
+ end
482
+
483
+ def test_strict_properties
484
+ schema = {
485
+ "$schema" => "http://json-schema.org/draft-03/schema#",
486
+ "properties" => {
487
+ "a" => {"type" => "string"},
488
+ "b" => {"type" => "string"}
489
+ }
490
+ }
491
+
492
+ data = {"a" => "a"}
493
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
494
+
495
+ data = {"b" => "b"}
496
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
497
+
498
+ data = {"a" => "a", "b" => "b"}
499
+ assert(JSON::Validator.validate(schema,data,:strict => true))
500
+
501
+ data = {"a" => "a", "b" => "b", "c" => "c"}
502
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
503
+ end
504
+
505
+ def test_strict_properties_additional_props
506
+ schema = {
507
+ "$schema" => "http://json-schema.org/draft-03/schema#",
508
+ "properties" => {
509
+ "a" => {"type" => "string"},
510
+ "b" => {"type" => "string"}
511
+ },
512
+ "additionalProperties" => {"type" => "integer"}
513
+ }
514
+
515
+ data = {"a" => "a"}
516
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
517
+
518
+ data = {"b" => "b"}
519
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
520
+
521
+ data = {"a" => "a", "b" => "b"}
522
+ assert(JSON::Validator.validate(schema,data,:strict => true))
523
+
524
+ data = {"a" => "a", "b" => "b", "c" => "c"}
525
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
526
+
527
+ data = {"a" => "a", "b" => "b", "c" => 3}
528
+ assert(JSON::Validator.validate(schema,data,:strict => true))
529
+ end
530
+
531
+ def test_strict_properties_pattern_props
532
+ schema = {
533
+ "$schema" => "http://json-schema.org/draft-03/schema#",
534
+ "properties" => {
535
+ "a" => {"type" => "string"},
536
+ "b" => {"type" => "string"}
537
+ },
538
+ "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
539
+ }
540
+
541
+ data = {"a" => "a"}
542
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
543
+
544
+ data = {"b" => "b"}
545
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
546
+
547
+ data = {"a" => "a", "b" => "b"}
548
+ assert(JSON::Validator.validate(schema,data,:strict => true))
549
+
550
+ data = {"a" => "a", "b" => "b", "c" => "c"}
551
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
552
+
553
+ data = {"a" => "a", "b" => "b", "c" => 3}
554
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
555
+
556
+ data = {"a" => "a", "b" => "b", "23 taco" => 3}
557
+ assert(JSON::Validator.validate(schema,data,:strict => true))
558
+
559
+ data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
560
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
561
+ end
562
+
563
+ def test_pattern
564
+ # Set up the default datatype
565
+ schema = {
566
+ "$schema" => "http://json-schema.org/draft-03/schema#",
567
+ "properties" => {
568
+ "a" => {"pattern" => "\\d+ taco"}
569
+ }
570
+ }
571
+
572
+ data = {
573
+ "a" => nil
574
+ }
575
+
576
+ # Test strings
577
+ data["a"] = "156 taco bell"
578
+ assert(JSON::Validator.validate(schema,data))
579
+
580
+ # Test a non-string
581
+ data["a"] = 5
582
+ assert(JSON::Validator.validate(schema,data))
583
+
584
+ data["a"] = "taco"
585
+ assert(!JSON::Validator.validate(schema,data))
586
+ end
587
+
588
+
589
+ def test_min_length
590
+ # Set up the default datatype
591
+ schema = {
592
+ "$schema" => "http://json-schema.org/draft-03/schema#",
593
+ "properties" => {
594
+ "a" => {"minLength" => 1}
595
+ }
596
+ }
597
+
598
+ data = {
599
+ "a" => nil
600
+ }
601
+
602
+ # Try out strings
603
+ data["a"] = "t"
604
+ assert(JSON::Validator.validate(schema,data))
605
+
606
+ data["a"] = ""
607
+ assert(!JSON::Validator.validate(schema,data))
608
+
609
+ # Try out non-string
610
+ data["a"] = 5
611
+ assert(JSON::Validator.validate(schema,data))
612
+ end
613
+
614
+
615
+ def test_max_length
616
+ # Set up the default datatype
617
+ schema = {
618
+ "$schema" => "http://json-schema.org/draft-03/schema#",
619
+ "properties" => {
620
+ "a" => {"maxLength" => 1}
621
+ }
622
+ }
623
+
624
+ data = {
625
+ "a" => nil
626
+ }
627
+
628
+ # Try out strings
629
+ data["a"] = "t"
630
+ assert(JSON::Validator.validate(schema,data))
631
+
632
+ data["a"] = "tt"
633
+ assert(!JSON::Validator.validate(schema,data))
634
+
635
+ # Try out non-string
636
+ data["a"] = 5
637
+ assert(JSON::Validator.validate(schema,data))
638
+ end
639
+
640
+
641
+ def test_enum
642
+ # Set up the default datatype
643
+ schema = {
644
+ "$schema" => "http://json-schema.org/draft-03/schema#",
645
+ "properties" => {
646
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
647
+ }
648
+ }
649
+
650
+ data = {
651
+ "a" => nil
652
+ }
653
+
654
+ # Make sure all of the above are valid...
655
+ data["a"] = 1
656
+ assert(JSON::Validator.validate(schema,data))
657
+
658
+ data["a"] = 'boo'
659
+ assert(JSON::Validator.validate(schema,data))
660
+
661
+ data["a"] = [1,2,3]
662
+ assert(JSON::Validator.validate(schema,data))
663
+
664
+ data["a"] = {"a" => "b"}
665
+ assert(JSON::Validator.validate(schema,data))
666
+
667
+ # Test something that doesn't exist
668
+ data["a"] = 'taco'
669
+ assert(!JSON::Validator.validate(schema,data))
670
+
671
+ # Try it without the key
672
+ data = {}
673
+ assert(JSON::Validator.validate(schema,data))
674
+ end
675
+
676
+
677
+ def test_divisible_by
678
+ # Set up the default datatype
679
+ schema = {
680
+ "$schema" => "http://json-schema.org/draft-03/schema#",
681
+ "properties" => {
682
+ "a" => {"divisibleBy" => 1.1}
683
+ }
684
+ }
685
+
686
+ data = {
687
+ "a" => nil
688
+ }
689
+
690
+ data["a"] = 3.3
691
+ assert(JSON::Validator.validate(schema,data))
692
+
693
+ data["a"] = 3.4
694
+ assert(!JSON::Validator.validate(schema,data))
695
+
696
+ schema["properties"]["a"]["divisibleBy"] = 2.0
697
+
698
+ data["a"] = 4.0
699
+ assert(JSON::Validator.validate(schema,data))
700
+
701
+ data["a"] = 'boo'
702
+ assert(JSON::Validator.validate(schema,data))
703
+
704
+ data["a"] = 5
705
+ schema["properties"]["a"]["divisibleBy"] = 0
706
+ assert(!JSON::Validator.validate(schema,data))
707
+ end
708
+
709
+
710
+
711
+ def test_disallow
712
+ # Set up the default datatype
713
+ schema = {
714
+ "$schema" => "http://json-schema.org/draft-03/schema#",
715
+ "properties" => {
716
+ "a" => {"disallow" => "integer"}
717
+ }
718
+ }
719
+
720
+ data = {
721
+ "a" => nil
722
+ }
723
+
724
+
725
+ data["a"] = 'string'
726
+ assert(JSON::Validator.validate(schema,data))
727
+
728
+ data["a"] = 5
729
+ assert(!JSON::Validator.validate(schema,data))
730
+
731
+
732
+ schema["properties"]["a"]["disallow"] = ["integer","string"]
733
+ data["a"] = 'string'
734
+ assert(!JSON::Validator.validate(schema,data))
735
+
736
+ data["a"] = 5
737
+ assert(!JSON::Validator.validate(schema,data))
738
+
739
+ data["a"] = false
740
+ assert(JSON::Validator.validate(schema,data))
741
+
742
+ end
743
+
744
+
745
+
746
+ def test_extends
747
+ schema = {
748
+ "$schema" => "http://json-schema.org/draft-03/schema#",
749
+ "properties" => {
750
+ "a" => { "type" => "integer"}
751
+ }
752
+ }
753
+
754
+ schema2 = {
755
+ "$schema" => "http://json-schema.org/draft-03/schema#",
756
+ "properties" => {
757
+ "a" => { "maximum" => 5 }
758
+ }
759
+ }
760
+
761
+ data = {
762
+ "a" => 10
763
+ }
764
+
765
+ assert(JSON::Validator.validate(schema,data))
766
+ assert(!JSON::Validator.validate(schema2,data))
767
+
768
+ schema["extends"] = schema2
769
+
770
+ assert(!JSON::Validator.validate(schema,data))
771
+ end
772
+
773
+ def test_pattern_properties
774
+ # Set up the default datatype
775
+ schema = {
776
+ "$schema" => "http://json-schema.org/draft-03/schema#",
777
+ "patternProperties" => {
778
+ "\\d+ taco" => {"type" => "integer"}
779
+ }
780
+ }
781
+
782
+ data = {
783
+ "a" => true,
784
+ "1 taco" => 1,
785
+ "20 tacos" => 20
786
+ }
787
+
788
+ assert(JSON::Validator.validate(schema,data))
789
+ data["20 tacos"] = "string!"
790
+ assert(!JSON::Validator.validate(schema,data))
791
+ end
792
+
793
+
794
+ def test_additional_properties
795
+ # Test no additional properties allowed
796
+ schema = {
797
+ "$schema" => "http://json-schema.org/draft-03/schema#",
798
+ "properties" => {
799
+ "a" => { "type" => "integer" }
800
+ },
801
+ "additionalProperties" => false
802
+ }
803
+
804
+ data = {
805
+ "a" => 10
806
+ }
807
+
808
+ assert(JSON::Validator.validate(schema,data))
809
+ data["b"] = 5
810
+ assert(!JSON::Validator.validate(schema,data))
811
+
812
+ # Test additional properties match a schema
813
+ schema["additionalProperties"] = { "type" => "string" }
814
+ data["b"] = "taco"
815
+ assert(JSON::Validator.validate(schema,data))
816
+ data["b"] = 5
817
+ assert(!JSON::Validator.validate(schema,data))
818
+
819
+ # Make sure this works with pattern properties set, too
820
+ schema = {
821
+ "$schema" => "http://json-schema.org/draft-03/schema#",
822
+ "patternProperties" => {
823
+ "\\d+ taco" => {"type" => "integer"}
824
+ },
825
+ "additionalProperties" => false
826
+ }
827
+
828
+ data = {
829
+ "5 tacos" => 5,
830
+ "20 tacos" => 20
831
+ }
832
+
833
+ assert(JSON::Validator.validate(schema,data))
834
+ data["b"] = 5
835
+ assert(!JSON::Validator.validate(schema,data))
836
+ end
837
+
838
+
839
+ def test_items
840
+ schema = {
841
+ "$schema" => "http://json-schema.org/draft-03/schema#",
842
+ "items" => { "type" => "integer" }
843
+ }
844
+
845
+ data = [1,2,4]
846
+ assert(JSON::Validator.validate(schema,data))
847
+ data = [1,2,"string"]
848
+ assert(!JSON::Validator.validate(schema,data))
849
+
850
+ schema = {
851
+ "$schema" => "http://json-schema.org/draft-03/schema#",
852
+ "items" => [
853
+ {"type" => "integer"},
854
+ {"type" => "string"}
855
+ ]
856
+ }
857
+
858
+ data = [1,"string"]
859
+ assert(JSON::Validator.validate(schema,data))
860
+ data = [1,"string",3]
861
+ assert(JSON::Validator.validate(schema,data))
862
+ data = ["string",1]
863
+ assert(!JSON::Validator.validate(schema,data))
864
+
865
+ schema = {
866
+ "$schema" => "http://json-schema.org/draft-03/schema#",
867
+ "items" => [
868
+ {"type" => "integer"},
869
+ {"type" => "string"}
870
+ ],
871
+ "additionalItems" => false
872
+ }
873
+
874
+ data = [1,"string"]
875
+ assert(JSON::Validator.validate(schema,data))
876
+ data = [1,"string",3]
877
+ assert(!JSON::Validator.validate(schema,data))
878
+
879
+ schema = {"$schema" => "http://json-schema.org/draft-03/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
880
+
881
+ data = [1,"string"]
882
+ assert(JSON::Validator.validate(schema,data))
883
+ data = [1,"string",3]
884
+ assert(JSON::Validator.validate(schema,data))
885
+ data = [1,"string","string"]
886
+ assert(!JSON::Validator.validate(schema,data))
887
+ end
888
+
889
+
890
+ def test_list_option
891
+ schema = {
892
+ "$schema" => "http://json-schema.org/draft-03/schema#",
893
+ "type" => "object",
894
+ "properties" => { "a" => {"type" => "integer", "required" => true} }
895
+ }
896
+
897
+ data = [{"a" => 1},{"a" => 2},{"a" => 3}]
898
+ assert(JSON::Validator.validate(schema,data,:list => true))
899
+ assert(!JSON::Validator.validate(schema,data))
900
+
901
+ data = {"a" => 1}
902
+ assert(!JSON::Validator.validate(schema,data,:list => true))
903
+
904
+ data = [{"a" => 1},{"b" => 2},{"a" => 3}]
905
+ assert(!JSON::Validator.validate(schema,data,:list => true))
906
+ end
907
+
908
+
909
+ def test_self_reference
910
+ schema = {
911
+ "$schema" => "http://json-schema.org/draft-03/schema#",
912
+ "type" => "object",
913
+ "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
914
+ }
915
+
916
+ data = {"a" => 5, "b" => {"b" => {"a" => 1}}}
917
+ assert(JSON::Validator.validate(schema,data))
918
+ data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
919
+ assert(!JSON::Validator.validate(schema,data))
920
+ end
921
+
922
+
923
+ def test_format_ipv4
924
+ schema = {
925
+ "$schema" => "http://json-schema.org/draft-03/schema#",
926
+ "type" => "object",
927
+ "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
928
+ }
929
+
930
+ data = {"a" => "1.1.1.1"}
931
+ assert(JSON::Validator.validate(schema,data))
932
+ data = {"a" => "1.1.1"}
933
+ assert(!JSON::Validator.validate(schema,data))
934
+ data = {"a" => "1.1.1.300"}
935
+ assert(!JSON::Validator.validate(schema,data))
936
+ data = {"a" => 5}
937
+ assert(!JSON::Validator.validate(schema,data))
938
+ data = {"a" => "1.1.1"}
939
+ assert(!JSON::Validator.validate(schema,data))
940
+ data = {"a" => "1.1.1.1b"}
941
+ assert(!JSON::Validator.validate(schema,data))
942
+ data = {"a" => "b1.1.1.1"}
943
+ end
944
+
945
+
946
+ def test_format_ipv6
947
+ schema = {
948
+ "$schema" => "http://json-schema.org/draft-03/schema#",
949
+ "type" => "object",
950
+ "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
951
+ }
952
+
953
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
954
+ assert(JSON::Validator.validate(schema,data))
955
+ data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
956
+ assert(JSON::Validator.validate(schema,data))
957
+ data = {"a" => "1111:2222:8888::eeee:ffff"}
958
+ assert(JSON::Validator.validate(schema,data))
959
+ data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
960
+ assert(!JSON::Validator.validate(schema,data))
961
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
962
+ assert(!JSON::Validator.validate(schema,data))
963
+ data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
964
+ assert(!JSON::Validator.validate(schema,data))
965
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
966
+ assert(!JSON::Validator.validate(schema,data))
967
+ end
968
+
969
+ def test_format_time
970
+ schema = {
971
+ "$schema" => "http://json-schema.org/draft-03/schema#",
972
+ "type" => "object",
973
+ "properties" => { "a" => {"type" => "string", "format" => "time"}}
974
+ }
975
+
976
+ data = {"a" => "12:00:00"}
977
+ assert(JSON::Validator.validate(schema,data))
978
+ data = {"a" => "12:00"}
979
+ assert(!JSON::Validator.validate(schema,data))
980
+ data = {"a" => "12:00:60"}
981
+ assert(!JSON::Validator.validate(schema,data))
982
+ data = {"a" => "12:60:00"}
983
+ assert(!JSON::Validator.validate(schema,data))
984
+ data = {"a" => "24:00:00"}
985
+ assert(!JSON::Validator.validate(schema,data))
986
+ data = {"a" => "0:00:00"}
987
+ assert(!JSON::Validator.validate(schema,data))
988
+ data = {"a" => "-12:00:00"}
989
+ assert(!JSON::Validator.validate(schema,data))
990
+ data = {"a" => "12:00:00b"}
991
+ assert(!JSON::Validator.validate(schema,data))
992
+ end
993
+
994
+
995
+ def test_format_date
996
+ schema = {
997
+ "$schema" => "http://json-schema.org/draft-03/schema#",
998
+ "type" => "object",
999
+ "properties" => { "a" => {"type" => "string", "format" => "date"}}
1000
+ }
1001
+
1002
+ data = {"a" => "2010-01-01"}
1003
+ assert(JSON::Validator.validate(schema,data))
1004
+ data = {"a" => "2010-01-32"}
1005
+ assert(!JSON::Validator.validate(schema,data))
1006
+ data = {"a" => "n2010-01-01"}
1007
+ assert(!JSON::Validator.validate(schema,data))
1008
+ data = {"a" => "2010-1-01"}
1009
+ assert(!JSON::Validator.validate(schema,data))
1010
+ data = {"a" => "2010-01-1"}
1011
+ assert(!JSON::Validator.validate(schema,data))
1012
+ data = {"a" => "2010-01-01n"}
1013
+ assert(!JSON::Validator.validate(schema,data))
1014
+ end
1015
+
1016
+ def test_format_datetime
1017
+ schema = {
1018
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1019
+ "type" => "object",
1020
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
1021
+ }
1022
+
1023
+ data = {"a" => "2010-01-01T12:00:00Z"}
1024
+ assert(JSON::Validator.validate(schema,data))
1025
+ data = {"a" => "2010-01-01T12:00:00.1Z"}
1026
+ assert(JSON::Validator.validate(schema,data))
1027
+ data = {"a" => "2010-01-01T12:00:00,1Z"}
1028
+ assert(JSON::Validator.validate(schema,data))
1029
+ data = {"a" => "2010-01-32T12:00:00Z"}
1030
+ assert(!JSON::Validator.validate(schema,data))
1031
+ data = {"a" => "2010-13-01T12:00:00Z"}
1032
+ assert(!JSON::Validator.validate(schema,data))
1033
+ data = {"a" => "2010-01-01T24:00:00Z"}
1034
+ assert(!JSON::Validator.validate(schema,data))
1035
+ data = {"a" => "2010-01-01T12:60:00Z"}
1036
+ assert(!JSON::Validator.validate(schema,data))
1037
+ data = {"a" => "2010-01-01T12:00:60Z"}
1038
+ assert(!JSON::Validator.validate(schema,data))
1039
+ data = {"a" => "2010-01-01T12:00:00z"}
1040
+ assert(!JSON::Validator.validate(schema,data))
1041
+ data = {"a" => "2010-01-0112:00:00Z"}
1042
+ assert(!JSON::Validator.validate(schema,data))
1043
+
1044
+ # test with a specific timezone
1045
+ data = {"a" => "2010-01-01T12:00:00+01"}
1046
+ assert(JSON::Validator.validate(schema,data))
1047
+ data = {"a" => "2010-01-01T12:00:00+01:00"}
1048
+ assert(JSON::Validator.validate(schema,data))
1049
+ data = {"a" => "2010-01-01T12:00:00+01:30"}
1050
+ assert(JSON::Validator.validate(schema,data))
1051
+ data = {"a" => "2010-01-01T12:00:00+0234"}
1052
+ assert(JSON::Validator.validate(schema,data))
1053
+ data = {"a" => "2010-01-01T12:00:00+01:"}
1054
+ assert(!JSON::Validator.validate(schema,data))
1055
+ data = {"a" => "2010-01-01T12:00:00+0"}
1056
+ assert(!JSON::Validator.validate(schema,data))
1057
+ # do not allow mixing Z and specific timezone
1058
+ data = {"a" => "2010-01-01T12:00:00Z+01"}
1059
+ assert(!JSON::Validator.validate(schema,data))
1060
+ data = {"a" => "2010-01-01T12:00:00+01Z"}
1061
+ assert(!JSON::Validator.validate(schema,data))
1062
+ data = {"a" => "2010-01-01T12:00:00+01:30Z"}
1063
+ assert(!JSON::Validator.validate(schema,data))
1064
+ data = {"a" => "2010-01-01T12:00:00+0Z"}
1065
+ assert(!JSON::Validator.validate(schema,data))
1066
+
1067
+ # test without any timezone
1068
+ data = {"a" => "2010-01-01T12:00:00"}
1069
+ assert(JSON::Validator.validate(schema,data))
1070
+ data = {"a" => "2010-01-01T12:00:00.12345"}
1071
+ assert(JSON::Validator.validate(schema,data))
1072
+ data = {"a" => "2010-01-01T12:00:00,12345"}
1073
+ assert(JSON::Validator.validate(schema,data))
1074
+ data = {"a" => "2010-01-01T12:00:00.12345"}
1075
+ assert(JSON::Validator.validate(schema,data))
1076
+ end
1077
+
1078
+
1079
+ def test_format_union
1080
+ data1 = {"a" => "boo"}
1081
+ data2 = {"a" => nil}
1082
+
1083
+ schema = {
1084
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1085
+ "type" => "object",
1086
+ "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
1087
+ }
1088
+ assert(!JSON::Validator.validate(schema,data1))
1089
+ assert(JSON::Validator.validate(schema,data2))
1090
+ end
1091
+
1092
+ def test_format_uri
1093
+ data1 = {"a" => "http://gitbuh.com"}
1094
+ data2 = {"a" => "::boo"}
1095
+
1096
+ schema = {
1097
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1098
+ "type" => "object",
1099
+ "properties" => { "a" => {"type" => "string", "format" => "uri"}}
1100
+ }
1101
+
1102
+ assert(JSON::Validator.validate(schema,data1))
1103
+ assert(!JSON::Validator.validate(schema,data2))
1104
+ end
1105
+
1106
+
1107
+
1108
+ def test_schema
1109
+ schema = {
1110
+ "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
1111
+ "type" => "object"
1112
+ }
1113
+
1114
+ data = {"a" => "taco"}
1115
+ assert(!JSON::Validator.validate(schema,data))
1116
+
1117
+ schema = {
1118
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1119
+ "type" => "object"
1120
+ }
1121
+ assert(JSON::Validator.validate(schema,data))
1122
+ end
1123
+
1124
+ def test_dependency
1125
+ schema = {
1126
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1127
+ "type" => "object",
1128
+ "properties" => {
1129
+ "a" => {"type" => "integer"},
1130
+ "b" => {"type" => "integer"}
1131
+ },
1132
+ "dependencies" => {
1133
+ "a" => "b"
1134
+ }
1135
+ }
1136
+
1137
+ data = {"a" => 1, "b" => 2}
1138
+ assert(JSON::Validator.validate(schema,data))
1139
+ data = {"a" => 1}
1140
+ assert(!JSON::Validator.validate(schema,data))
1141
+
1142
+ schema = {
1143
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1144
+ "type" => "object",
1145
+ "properties" => {
1146
+ "a" => {"type" => "integer"},
1147
+ "b" => {"type" => "integer"},
1148
+ "c" => {"type" => "integer"}
1149
+ },
1150
+ "dependencies" => {
1151
+ "a" => ["b","c"]
1152
+ }
1153
+ }
1154
+
1155
+ data = {"a" => 1, "c" => 2}
1156
+ assert(!JSON::Validator.validate(schema,data))
1157
+ data = {"a" => 1, "b" => 2, "c" => 3}
1158
+ assert(JSON::Validator.validate(schema,data))
1159
+ end
1160
+
1161
+ def test_default
1162
+ schema = {
1163
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1164
+ "type" => "object",
1165
+ "properties" => {
1166
+ "a" => {"type" => "integer", "default" => 42},
1167
+ "b" => {"type" => "integer"}
1168
+ }
1169
+ }
1170
+
1171
+ data = {"b" => 2}
1172
+ assert(JSON::Validator.validate(schema,data))
1173
+ assert_nil(data["a"])
1174
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1175
+ assert_equal(42, data["a"])
1176
+
1177
+ schema = {
1178
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1179
+ "type" => "object",
1180
+ "properties" => {
1181
+ "a" => {"type" => "integer", "default" => 42, "required" => true},
1182
+ "b" => {"type" => "integer"}
1183
+ }
1184
+ }
1185
+
1186
+ data = {"b" => 2}
1187
+ assert(!JSON::Validator.validate(schema,data))
1188
+ assert_nil(data["a"])
1189
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1190
+ assert_equal(42, data["a"])
1191
+
1192
+ schema = {
1193
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1194
+ "type" => "object",
1195
+ "properties" => {
1196
+ "a" => {"type" => "integer", "default" => 42, "required" => true},
1197
+ "b" => {"type" => "integer"}
1198
+ }
1199
+ }
1200
+
1201
+
1202
+ schema = {
1203
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1204
+ "type" => "object",
1205
+ "properties" => {
1206
+ "a" => {"type" => "integer", "default" => 42, "required" => true, "readonly" => true},
1207
+ "b" => {"type" => "integer"}
1208
+ }
1209
+ }
1210
+
1211
+ data = {"b" => 2}
1212
+ assert(!JSON::Validator.validate(schema,data))
1213
+ assert_nil(data["a"])
1214
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1215
+ assert_nil(data["a"])
1216
+
1217
+ schema = {
1218
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1219
+ "type" => "object",
1220
+ "properties" => {
1221
+ "a" => {"type" => "integer", "default" => "42"},
1222
+ "b" => {"type" => "integer"}
1223
+ }
1224
+ }
1225
+
1226
+ data = {"b" => 2}
1227
+ assert(JSON::Validator.validate(schema,data))
1228
+ assert_nil(data["a"])
1229
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1230
+ assert_equal("42",data["a"])
1231
+
1232
+ end
1233
+
1234
+
1235
+ end
1236
+