json-schema 2.5.1 → 2.5.2

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