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