json-schema-openc-fork 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.md +19 -0
  3. data/README.textile +452 -0
  4. data/lib/json-schema.rb +19 -0
  5. data/lib/json-schema/attribute.rb +43 -0
  6. data/lib/json-schema/attributes/additionalitems.rb +28 -0
  7. data/lib/json-schema/attributes/additionalproperties.rb +58 -0
  8. data/lib/json-schema/attributes/allof.rb +39 -0
  9. data/lib/json-schema/attributes/anyof.rb +47 -0
  10. data/lib/json-schema/attributes/dependencies.rb +44 -0
  11. data/lib/json-schema/attributes/disallow.rb +12 -0
  12. data/lib/json-schema/attributes/divisibleby.rb +22 -0
  13. data/lib/json-schema/attributes/enum.rb +24 -0
  14. data/lib/json-schema/attributes/extends.rb +50 -0
  15. data/lib/json-schema/attributes/format.rb +14 -0
  16. data/lib/json-schema/attributes/formats/custom.rb +21 -0
  17. data/lib/json-schema/attributes/formats/date.rb +24 -0
  18. data/lib/json-schema/attributes/formats/date_time.rb +36 -0
  19. data/lib/json-schema/attributes/formats/date_time_v4.rb +15 -0
  20. data/lib/json-schema/attributes/formats/ip.rb +41 -0
  21. data/lib/json-schema/attributes/formats/time.rb +22 -0
  22. data/lib/json-schema/attributes/formats/uri.rb +20 -0
  23. data/lib/json-schema/attributes/items.rb +26 -0
  24. data/lib/json-schema/attributes/limit.rb +179 -0
  25. data/lib/json-schema/attributes/maxdecimal.rb +18 -0
  26. data/lib/json-schema/attributes/multipleof.rb +11 -0
  27. data/lib/json-schema/attributes/not.rb +30 -0
  28. data/lib/json-schema/attributes/oneof.rb +56 -0
  29. data/lib/json-schema/attributes/pattern.rb +18 -0
  30. data/lib/json-schema/attributes/patternproperties.rb +22 -0
  31. data/lib/json-schema/attributes/properties.rb +74 -0
  32. data/lib/json-schema/attributes/properties_optional.rb +26 -0
  33. data/lib/json-schema/attributes/ref.rb +74 -0
  34. data/lib/json-schema/attributes/required.rb +28 -0
  35. data/lib/json-schema/attributes/type.rb +83 -0
  36. data/lib/json-schema/attributes/type_v4.rb +29 -0
  37. data/lib/json-schema/attributes/uniqueitems.rb +16 -0
  38. data/lib/json-schema/errors/custom_format_error.rb +6 -0
  39. data/lib/json-schema/errors/json_parse_error.rb +6 -0
  40. data/lib/json-schema/errors/schema_error.rb +6 -0
  41. data/lib/json-schema/errors/validation_error.rb +46 -0
  42. data/lib/json-schema/schema.rb +63 -0
  43. data/lib/json-schema/schema/reader.rb +113 -0
  44. data/lib/json-schema/schema/validator.rb +36 -0
  45. data/lib/json-schema/util/array_set.rb +14 -0
  46. data/lib/json-schema/util/uri.rb +16 -0
  47. data/lib/json-schema/util/uuid.rb +285 -0
  48. data/lib/json-schema/validator.rb +592 -0
  49. data/lib/json-schema/validators/draft1.rb +45 -0
  50. data/lib/json-schema/validators/draft2.rb +46 -0
  51. data/lib/json-schema/validators/draft3.rb +50 -0
  52. data/lib/json-schema/validators/draft4.rb +56 -0
  53. data/lib/json-schema/validators/hyper-draft4.rb +14 -0
  54. data/resources/draft-01.json +155 -0
  55. data/resources/draft-02.json +166 -0
  56. data/resources/draft-03.json +174 -0
  57. data/resources/draft-04.json +150 -0
  58. data/test/data/all_of_ref_data.json +3 -0
  59. data/test/data/any_of_ref_data.json +7 -0
  60. data/test/data/bad_data_1.json +3 -0
  61. data/test/data/good_data_1.json +3 -0
  62. data/test/data/one_of_ref_links_data.json +5 -0
  63. data/test/schemas/address_microformat.json +18 -0
  64. data/test/schemas/all_of_ref_base_schema.json +6 -0
  65. data/test/schemas/all_of_ref_schema.json +7 -0
  66. data/test/schemas/any_of_ref_jane_schema.json +4 -0
  67. data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
  68. data/test/schemas/any_of_ref_john_schema.json +4 -0
  69. data/test/schemas/any_of_ref_schema.json +15 -0
  70. data/test/schemas/definition_schema.json +15 -0
  71. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
  72. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
  73. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
  74. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
  75. data/test/schemas/good_schema_1.json +10 -0
  76. data/test/schemas/good_schema_2.json +10 -0
  77. data/test/schemas/good_schema_extends1.json +10 -0
  78. data/test/schemas/good_schema_extends2.json +13 -0
  79. data/test/schemas/inner.schema.json +21 -0
  80. data/test/schemas/one_of_ref_links_schema.json +16 -0
  81. data/test/schemas/ref john with spaces schema.json +11 -0
  82. data/test/schemas/relative_definition_schema.json +8 -0
  83. data/test/schemas/self_link_schema.json +17 -0
  84. data/test/schemas/up_link_schema.json +17 -0
  85. data/test/test_all_of_ref_schema.rb +35 -0
  86. data/test/test_any_of_ref_schema.rb +35 -0
  87. data/test/test_bad_schema_ref.rb +39 -0
  88. data/test/test_common_test_suite.rb +66 -0
  89. data/test/test_custom_format.rb +116 -0
  90. data/test/test_definition.rb +15 -0
  91. data/test/test_extended_schema.rb +62 -0
  92. data/test/test_extends_and_additionalProperties.rb +52 -0
  93. data/test/test_files_v3.rb +43 -0
  94. data/test/test_fragment_resolution.rb +30 -0
  95. data/test/test_fragment_validation_with_ref.rb +34 -0
  96. data/test/test_full_validation.rb +208 -0
  97. data/test/test_helper.rb +47 -0
  98. data/test/test_initialize_data.rb +118 -0
  99. data/test/test_jsonschema_draft1.rb +171 -0
  100. data/test/test_jsonschema_draft2.rb +142 -0
  101. data/test/test_jsonschema_draft3.rb +502 -0
  102. data/test/test_jsonschema_draft4.rb +704 -0
  103. data/test/test_list_option.rb +21 -0
  104. data/test/test_merge_missing_values.rb +45 -0
  105. data/test/test_minitems.rb +16 -0
  106. data/test/test_one_of.rb +85 -0
  107. data/test/test_ruby_schema.rb +59 -0
  108. data/test/test_schema_loader.rb +74 -0
  109. data/test/test_schema_type_attribute.rb +20 -0
  110. data/test/test_schema_validation.rb +185 -0
  111. data/test/test_stringify.rb +48 -0
  112. data/test/test_uri_related.rb +67 -0
  113. data/test/test_validator.rb +53 -0
  114. metadata +284 -0
@@ -0,0 +1,704 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../test_helper', __FILE__)
3
+
4
+ class JSONSchemaDraft4Test < Minitest::Test
5
+ def schema_version
6
+ :draft4
7
+ end
8
+
9
+ def exclusive_minimum
10
+ { 'exclusiveMinimum' => true }
11
+ end
12
+
13
+ def exclusive_maximum
14
+ { 'exclusiveMaximum' => true }
15
+ end
16
+
17
+ def ipv4_format
18
+ 'ipv4'
19
+ end
20
+
21
+ include ArrayValidation::ItemsTests
22
+ include ArrayValidation::AdditionalItemsTests
23
+ include ArrayValidation::UniqueItemsTests
24
+
25
+ include NumberValidation::MinMaxTests
26
+ include NumberValidation::MultipleOfTests
27
+
28
+ include ObjectValidation::AdditionalPropertiesTests
29
+ include ObjectValidation::PatternPropertiesTests
30
+
31
+ include StringValidation::ValueTests
32
+ include StringValidation::FormatTests
33
+
34
+ include TypeValidation::SimpleTypeTests
35
+
36
+ def test_required
37
+ # Set up the default datatype
38
+ schema = {
39
+ "$schema" => "http://json-schema.org/draft-04/schema#",
40
+ "required" => ["a"],
41
+ "properties" => {
42
+ "a" => {}
43
+ }
44
+ }
45
+ data = {}
46
+
47
+ refute_valid schema, data
48
+ data['a'] = "Hello"
49
+ assert_valid schema, data
50
+
51
+ schema = {
52
+ "$schema" => "http://json-schema.org/draft-04/schema#",
53
+ "properties" => {
54
+ "a" => {"type" => "integer"}
55
+ }
56
+ }
57
+
58
+ data = {}
59
+ assert_valid schema, data
60
+ end
61
+
62
+ def test_min_properties
63
+ schema = { 'minProperties' => 2 }
64
+
65
+ assert_valid schema, {'a' => 1, 'b' => 2}
66
+ assert_valid schema, {'a' => 1, 'b' => 2, 'c' => 3}
67
+
68
+ refute_valid schema, {'a' => 1}
69
+ refute_valid schema, {}
70
+ end
71
+
72
+ def test_max_properties
73
+ schema = { 'maxProperties' => 2 }
74
+
75
+ assert_valid schema, {'a' => 1, 'b' => 2}
76
+ assert_valid schema, {'a' => 1}
77
+ assert_valid schema, {}
78
+
79
+ refute_valid schema, {'a' => 1, 'b' => 2, 'c' => 3}
80
+ end
81
+
82
+ def test_strict_properties
83
+ schema = {
84
+ "$schema" => "http://json-schema.org/draft-04/schema#",
85
+ "properties" => {
86
+ "a" => {"type" => "string"},
87
+ "b" => {"type" => "string"}
88
+ }
89
+ }
90
+
91
+ data = {"a" => "a"}
92
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
93
+
94
+ data = {"b" => "b"}
95
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
96
+
97
+ data = {"a" => "a", "b" => "b"}
98
+ assert(JSON::Validator.validate(schema,data,:strict => true))
99
+
100
+ data = {"a" => "a", "b" => "b", "c" => "c"}
101
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
102
+ end
103
+
104
+ def test_strict_properties_additional_props
105
+ schema = {
106
+ "$schema" => "http://json-schema.org/draft-04/schema#",
107
+ "properties" => {
108
+ "a" => {"type" => "string"},
109
+ "b" => {"type" => "string"}
110
+ },
111
+ "additionalProperties" => {"type" => "integer"}
112
+ }
113
+
114
+ data = {"a" => "a"}
115
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
116
+
117
+ data = {"b" => "b"}
118
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
119
+
120
+ data = {"a" => "a", "b" => "b"}
121
+ assert(JSON::Validator.validate(schema,data,:strict => true))
122
+
123
+ data = {"a" => "a", "b" => "b", "c" => "c"}
124
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
125
+
126
+ data = {"a" => "a", "b" => "b", "c" => 3}
127
+ assert(JSON::Validator.validate(schema,data,:strict => true))
128
+ end
129
+
130
+ def test_strict_properties_pattern_props
131
+ schema = {
132
+ "$schema" => "http://json-schema.org/draft-03/schema#",
133
+ "properties" => {
134
+ "a" => {"type" => "string"},
135
+ "b" => {"type" => "string"}
136
+ },
137
+ "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
138
+ }
139
+
140
+ data = {"a" => "a"}
141
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
142
+
143
+ data = {"b" => "b"}
144
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
145
+
146
+ data = {"a" => "a", "b" => "b"}
147
+ assert(JSON::Validator.validate(schema,data,:strict => true))
148
+
149
+ data = {"a" => "a", "b" => "b", "c" => "c"}
150
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
151
+
152
+ data = {"a" => "a", "b" => "b", "c" => 3}
153
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
154
+
155
+ data = {"a" => "a", "b" => "b", "23 taco" => 3}
156
+ assert(JSON::Validator.validate(schema,data,:strict => true))
157
+
158
+ data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
159
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
160
+ end
161
+
162
+ def test_enum
163
+ # Set up the default datatype
164
+ schema = {
165
+ "$schema" => "http://json-schema.org/draft-04/schema#",
166
+ "properties" => {
167
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
168
+ }
169
+ }
170
+
171
+ data = {
172
+ "a" => nil
173
+ }
174
+
175
+ # Make sure all of the above are valid...
176
+ data["a"] = 1
177
+ assert_valid schema, data
178
+
179
+ data["a"] = 'boo'
180
+ assert_valid schema, data
181
+
182
+ data["a"] = [1,2,3]
183
+ assert_valid schema, data
184
+
185
+ data["a"] = {"a" => "b"}
186
+ assert_valid schema, data
187
+
188
+ # Test something that doesn't exist
189
+ data["a"] = 'taco'
190
+ refute_valid schema, data
191
+
192
+ # Try it without the key
193
+ data = {}
194
+ assert_valid schema, data
195
+ end
196
+
197
+ def test_enum_with_schema_validation
198
+ schema = {
199
+ "$schema" => "http://json-schema.org/draft-04/schema#",
200
+ "properties" => {
201
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
202
+ }
203
+ }
204
+
205
+ data = {
206
+ "a" => nil
207
+ }
208
+
209
+ # Make sure all of the above are valid...
210
+ data["a"] = 1
211
+ assert(JSON::Validator.validate(schema,data,:validate_schema => true))
212
+ end
213
+
214
+ def test_list_option
215
+ schema = {
216
+ "$schema" => "http://json-schema.org/draft-04/schema#",
217
+ "type" => "object",
218
+ "required" => ["a"],
219
+ "properties" => { "a" => {"type" => "integer"} }
220
+ }
221
+
222
+ data = [{"a" => 1},{"a" => 2},{"a" => 3}]
223
+ assert(JSON::Validator.validate(schema,data,:list => true))
224
+ refute_valid schema, data
225
+
226
+ data = {"a" => 1}
227
+ assert(!JSON::Validator.validate(schema,data,:list => true))
228
+
229
+ data = [{"a" => 1},{"b" => 2},{"a" => 3}]
230
+ assert(!JSON::Validator.validate(schema,data,:list => true))
231
+ end
232
+
233
+ def test_default_with_strict_and_anyof
234
+ schema = {
235
+ "anyOf" => [
236
+ {
237
+ "type" => "object",
238
+ "properties" => {
239
+ "foo" => {
240
+ "enum" => ["view", "search"],
241
+ "default" => "view"
242
+ }
243
+ }
244
+ },
245
+ {
246
+ "type" => "object",
247
+ "properties" => {
248
+ "bar" => {
249
+ "type" => "string"
250
+ }
251
+ }
252
+ }
253
+ ]
254
+ }
255
+
256
+ data = {
257
+ "bar" => "baz"
258
+ }
259
+
260
+ assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
261
+ end
262
+
263
+ def test_default_with_anyof
264
+ schema = {
265
+ "anyOf" => [
266
+ {
267
+ "type" => "object",
268
+ "properties" => {
269
+ "foo" => {
270
+ "enum" => ["view", "search"],
271
+ "default" => "view"
272
+ }
273
+ }
274
+ },
275
+ {
276
+ "type" => "object",
277
+ "properties" => {
278
+ "bar" => {
279
+ "type" => "string"
280
+ }
281
+ }
282
+ }
283
+ ]
284
+ }
285
+
286
+ data = {}
287
+
288
+ assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
289
+ assert(data['foo'] == 'view')
290
+ end
291
+
292
+ def test_default_with_strict_and_oneof
293
+ schema = {
294
+ "oneOf" => [
295
+ {
296
+ "type" => "object",
297
+ "properties" => {
298
+ "bar" => {
299
+ "type" => "string"
300
+ }
301
+ }
302
+ },
303
+ {
304
+ "type" => "object",
305
+ "properties" => {
306
+ "foo" => {
307
+ "enum" => ["view", "search"],
308
+ "default" => "view"
309
+ }
310
+ }
311
+ }
312
+ ]
313
+ }
314
+
315
+ data = {
316
+ "bar" => "baz"
317
+ }
318
+
319
+ assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
320
+ assert(!data.key?('foo'))
321
+ end
322
+
323
+ def test_self_reference
324
+ schema = {
325
+ "$schema" => "http://json-schema.org/draft-04/schema#",
326
+ "type" => "object",
327
+ "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
328
+ }
329
+
330
+ assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}}
331
+ refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
332
+ end
333
+
334
+ def test_format_datetime
335
+ schema = {
336
+ "$schema" => "http://json-schema.org/draft-04/schema#",
337
+ "type" => "object",
338
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
339
+ }
340
+
341
+ assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
342
+ assert_valid schema, {"a" => "2010-01-01T12:00:00.1Z"}
343
+ refute_valid schema, {"a" => "2010-01-01T12:00:00,1Z"}
344
+ refute_valid schema, {"a" => "2010-01-01T12:00:00+0000"}
345
+ assert_valid schema, {"a" => "2010-01-01T12:00:00+00:00"}
346
+ refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
347
+ refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
348
+ assert_valid schema, {"a" => "2010-01-01T24:00:00Z"}
349
+ refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
350
+ assert_valid schema, {"a" => "2010-01-01T12:00:60Z"}
351
+ assert_valid schema, {"a" => "2010-01-01T12:00:00z"}
352
+ refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
353
+ end
354
+
355
+ def test_format_uri
356
+ data1 = {"a" => "http://gitbuh.com"}
357
+ data2 = {"a" => "::boo"}
358
+ data3 = {"a" => "http://ja.wikipedia.org/wiki/メインページ"}
359
+
360
+ schema = {
361
+ "$schema" => "http://json-schema.org/draft-04/schema#",
362
+ "type" => "object",
363
+ "properties" => { "a" => {"type" => "string", "format" => "uri"}}
364
+ }
365
+
366
+ assert(JSON::Validator.validate(schema,data1))
367
+ assert(!JSON::Validator.validate(schema,data2))
368
+ assert(JSON::Validator.validate(schema,data3))
369
+ end
370
+
371
+ def test_schema
372
+ schema = {
373
+ "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
374
+ "type" => "object"
375
+ }
376
+
377
+ data = {"a" => "taco"}
378
+ assert(!JSON::Validator.validate(schema,data))
379
+
380
+ schema = {
381
+ "$schema" => "http://json-schema.org/draft-04/schema#",
382
+ "type" => "object"
383
+ }
384
+ assert_valid schema, data
385
+ end
386
+
387
+ def test_dependency
388
+ schema = {
389
+ "$schema" => "http://json-schema.org/draft-04/schema#",
390
+ "type" => "object",
391
+ "properties" => {
392
+ "a" => {"type" => "integer"},
393
+ "b" => {"type" => "integer"}
394
+ },
395
+ "dependencies" => {
396
+ "a" => ["b"]
397
+ }
398
+ }
399
+
400
+ data = {"a" => 1, "b" => 2}
401
+ assert_valid schema, data
402
+ data = {"a" => 1}
403
+ refute_valid schema, data
404
+
405
+ schema = {
406
+ "$schema" => "http://json-schema.org/draft-04/schema#",
407
+ "type" => "object",
408
+ "properties" => {
409
+ "a" => {"type" => "integer"},
410
+ "b" => {"type" => "integer"},
411
+ "c" => {"type" => "integer"}
412
+ },
413
+ "dependencies" => {
414
+ "a" => ["b","c"]
415
+ }
416
+ }
417
+
418
+ data = {"a" => 1, "c" => 2}
419
+ refute_valid schema, data
420
+ data = {"a" => 1, "b" => 2, "c" => 3}
421
+ assert_valid schema, data
422
+ end
423
+
424
+ def test_schema_dependency
425
+ schema = {
426
+ "type"=> "object",
427
+ "properties"=> {
428
+ "name"=> { "type"=> "string" },
429
+ "credit_card"=> { "type"=> "number" }
430
+ },
431
+ "required"=> ["name"],
432
+ "dependencies"=> {
433
+ "credit_card"=> {
434
+ "properties"=> {
435
+ "billing_address"=> { "type"=> "string" }
436
+ },
437
+ "required"=> ["billing_address"]
438
+ }
439
+ }
440
+ }
441
+ data = {
442
+ "name" => "John Doe",
443
+ "credit_card" => 5555555555555555
444
+ }
445
+ assert(!JSON::Validator.validate(schema,data), 'test schema dependency with invalid data')
446
+ data['billing_address'] = "Somewhere over the rainbow"
447
+ assert(JSON::Validator.validate(schema,data), 'test schema dependency with valid data')
448
+ end
449
+
450
+ def test_default
451
+ schema = {
452
+ "$schema" => "http://json-schema.org/draft-04/schema#",
453
+ "type" => "object",
454
+ "properties" => {
455
+ "a" => {"type" => "integer", "default" => 42},
456
+ "b" => {"type" => "integer"}
457
+ }
458
+ }
459
+
460
+ data = {:b => 2}
461
+ assert_valid schema, data
462
+ assert_nil(data["a"])
463
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
464
+ assert_equal(42, data["a"])
465
+ assert_equal(2, data[:b])
466
+
467
+ schema = {
468
+ "$schema" => "http://json-schema.org/draft-04/schema#",
469
+ "type" => "object",
470
+ "required" => ["a"],
471
+ "properties" => {
472
+ "a" => {"type" => "integer", "default" => 42},
473
+ "b" => {"type" => "integer"}
474
+ }
475
+ }
476
+
477
+ data = {:b => 2}
478
+ refute_valid schema, data
479
+ assert_nil(data["a"])
480
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
481
+ assert_equal(42, data["a"])
482
+ assert_equal(2, data[:b])
483
+
484
+ schema = {
485
+ "$schema" => "http://json-schema.org/draft-04/schema#",
486
+ "type" => "object",
487
+ "required" => ["a"],
488
+ "properties" => {
489
+ "a" => {"type" => "integer", "default" => 42, "readonly" => true},
490
+ "b" => {"type" => "integer"}
491
+ }
492
+ }
493
+
494
+ data = {:b => 2}
495
+ refute_valid schema, data
496
+ assert_nil(data["a"])
497
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
498
+ assert_nil(data["a"])
499
+ assert_equal(2, data[:b])
500
+
501
+ schema = {
502
+ "$schema" => "http://json-schema.org/draft-04/schema#",
503
+ "type" => "object",
504
+ "properties" => {
505
+ "a" => {"type" => "integer", "default" => "42"},
506
+ "b" => {"type" => "integer"}
507
+ }
508
+ }
509
+
510
+ data = {:b => 2}
511
+ assert_valid schema, data
512
+ assert_nil(data["a"])
513
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
514
+ assert_equal("42",data["a"])
515
+ assert_equal(2, data[:b])
516
+
517
+ end
518
+
519
+
520
+ def test_all_of
521
+ schema = {
522
+ "$schema" => "http://json-schema.org/draft-04/schema#",
523
+ "allOf" => [
524
+ {
525
+ "properties" => {"a" => {"type" => "string"}},
526
+ "required" => ["a"]
527
+ },
528
+ {
529
+ "properties" => {"b" => {"type" => "integer"}}
530
+ }
531
+ ]
532
+ }
533
+
534
+ data = {"a" => "hello", "b" => 5}
535
+ assert_valid schema, data
536
+
537
+ data = {"a" => "hello"}
538
+ assert_valid schema, data
539
+
540
+ data = {"a" => "hello", "b" => "taco"}
541
+ refute_valid schema, data
542
+
543
+ data = {"b" => 5}
544
+ refute_valid schema, data
545
+ end
546
+
547
+
548
+ def test_any_of
549
+ schema = {
550
+ "$schema" => "http://json-schema.org/draft-04/schema#",
551
+ "anyOf" => [
552
+ {
553
+ "properties" => {"a" => {"type" => "string"}},
554
+ "required" => ["a"]
555
+ },
556
+ {
557
+ "properties" => {"b" => {"type" => "integer"}}
558
+ }
559
+ ]
560
+ }
561
+
562
+ data = {"a" => "hello", "b" => 5}
563
+ assert_valid schema, data
564
+
565
+ data = {"a" => "hello"}
566
+ assert_valid schema, data
567
+
568
+ data = {"a" => "hello", "b" => "taco"}
569
+ assert_valid schema, data
570
+
571
+ data = {"b" => 5}
572
+ assert_valid schema, data
573
+
574
+ data = {"a" => 5, "b" => "taco"}
575
+ refute_valid schema, data
576
+ end
577
+
578
+
579
+ def test_one_of
580
+ schema = {
581
+ "$schema" => "http://json-schema.org/draft-04/schema#",
582
+ "oneOf" => [
583
+ {
584
+ "properties" => {"a" => {"type" => "string"}},
585
+ "required" => ["a"]
586
+ },
587
+ {
588
+ "properties" => {"b" => {"type" => "integer"}}
589
+ }
590
+ ]
591
+ }
592
+
593
+ data = {"a" => "hello", "b" => 5}
594
+ refute_valid schema, data
595
+
596
+ # This passes because b is not required, thus matches both schemas
597
+ data = {"a" => "hello"}
598
+ refute_valid schema, data
599
+
600
+ data = {"a" => "hello", "b" => "taco"}
601
+ assert_valid schema, data
602
+
603
+ data = {"b" => 5}
604
+ assert_valid schema, data
605
+
606
+ data = {"a" => 5, "b" => "taco"}
607
+ refute_valid schema, data
608
+ end
609
+
610
+
611
+ def test_not
612
+ # Start with a simple not
613
+ schema = {
614
+ "$schema" => "http://json-schema.org/draft-04/schema#",
615
+ "properties" => {
616
+ "a" => {"not" => { "type" => ["string", "boolean"]}}
617
+ }
618
+ }
619
+
620
+ data = {"a" => 1}
621
+ assert_valid schema, data
622
+
623
+ data = {"a" => "hi!"}
624
+ refute_valid schema, data
625
+
626
+ data = {"a" => true}
627
+ refute_valid schema, data
628
+
629
+ # Sub-schema not
630
+ schema = {
631
+ "$schema" => "http://json-schema.org/draft-04/schema#",
632
+ "properties" => {
633
+ "a" => {"not" => {"anyOf" => [
634
+ {
635
+ "type" => ["string","boolean"]
636
+ },
637
+ {
638
+ "type" => "object",
639
+ "properties" => {
640
+ "b" => {"type" => "boolean"}
641
+ }
642
+ }
643
+ ]}
644
+ }
645
+ }
646
+ }
647
+
648
+ data = {"a" => 1}
649
+ assert_valid schema, data
650
+
651
+ data = {"a" => "hi!"}
652
+ refute_valid schema, data
653
+
654
+ data = {"a" => true}
655
+ refute_valid schema, data
656
+
657
+ data = {"a" => {"b" => true}}
658
+ refute_valid schema, data
659
+
660
+ data = {"a" => {"b" => 5}}
661
+ assert_valid schema, data
662
+ end
663
+
664
+ def test_not_fully_validate
665
+ # Start with a simple not
666
+ schema = {
667
+ "$schema" => "http://json-schema.org/draft-04/schema#",
668
+ "properties" => {
669
+ "a" => {"not" => { "type" => ["string", "boolean"]}}
670
+ }
671
+ }
672
+
673
+ data = {"a" => 1}
674
+ errors = JSON::Validator.fully_validate(schema,data)
675
+ assert_equal(0, errors.length)
676
+
677
+ data = {"a" => "taco"}
678
+ errors = JSON::Validator.fully_validate(schema,data)
679
+ assert_equal(1, errors.length)
680
+ end
681
+
682
+ def test_definitions
683
+ schema = {
684
+ "$schema" => "http://json-schema.org/draft-04/schema#",
685
+ "type" => "array",
686
+ "items" => { "$ref" => "#/definitions/positiveInteger"},
687
+ "definitions" => {
688
+ "positiveInteger" => {
689
+ "type" => "integer",
690
+ "minimum" => 0,
691
+ "exclusiveMinimum" => true
692
+ }
693
+ }
694
+ }
695
+
696
+ data = [1,2,3]
697
+ assert_valid schema, data
698
+
699
+ data = [-1,2,3]
700
+ refute_valid schema, data
701
+ end
702
+ end
703
+
704
+