json-schema 1.2.1 → 2.0.0

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.
@@ -28,8 +28,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
28
28
  data['a'] = true
29
29
  assert(!JSON::Validator.validate(schema,data))
30
30
 
31
- assert(JSON::Validator.validate({'type' => 'integer'}, 3))
32
- assert(!JSON::Validator.validate({'type' => 'integer'}, "hello"))
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
33
 
34
34
  # Test numbers
35
35
  schema["properties"]["a"]["type"] = "number"
@@ -45,9 +45,9 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
45
45
  data['a'] = true
46
46
  assert(!JSON::Validator.validate(schema,data))
47
47
 
48
- assert(JSON::Validator.validate({'type' => 'number'}, 3))
49
- assert(JSON::Validator.validate({'type' => 'number'}, 3.14159265358979))
50
- assert(!JSON::Validator.validate({'type' => 'number'}, "hello"))
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
51
 
52
52
 
53
53
  # Test strings
@@ -64,9 +64,9 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
64
64
  data['a'] = true
65
65
  assert(!JSON::Validator.validate(schema,data))
66
66
 
67
- assert(JSON::Validator.validate({'type' => 'string'}, 'hello'))
68
- assert(!JSON::Validator.validate({'type' => 'string'}, 3.14159265358979))
69
- assert(!JSON::Validator.validate({'type' => 'string'}, []))
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
70
 
71
71
 
72
72
  # Test booleans
@@ -86,11 +86,11 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
86
86
  data['a'] = false
87
87
  assert(JSON::Validator.validate(schema,data))
88
88
 
89
- assert(JSON::Validator.validate({'type' => 'boolean'}, true))
90
- assert(JSON::Validator.validate({'type' => 'boolean'}, false))
91
- assert(!JSON::Validator.validate({'type' => 'boolean'}, nil))
92
- assert(!JSON::Validator.validate({'type' => 'boolean'}, 3))
93
- assert(!JSON::Validator.validate({'type' => 'boolean'}, "hello"))
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
94
 
95
95
 
96
96
  # Test object
@@ -107,11 +107,11 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
107
107
  data['a'] = true
108
108
  assert(!JSON::Validator.validate(schema,data))
109
109
 
110
- assert(JSON::Validator.validate({'type' => 'object'}, {'a' => true}))
111
- assert(JSON::Validator.validate({'type' => 'object'}, {}))
112
- assert(!JSON::Validator.validate({'type' => 'object'}, []))
113
- assert(!JSON::Validator.validate({'type' => 'object'}, 3))
114
- assert(!JSON::Validator.validate({'type' => 'object'}, "hello"))
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
115
 
116
116
 
117
117
  # Test array
@@ -128,11 +128,11 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
128
128
  data['a'] = true
129
129
  assert(!JSON::Validator.validate(schema,data))
130
130
 
131
- assert(JSON::Validator.validate({'type' => 'array'}, ['a']))
132
- assert(JSON::Validator.validate({'type' => 'array'}, []))
133
- assert(!JSON::Validator.validate({'type' => 'array'}, {}))
134
- assert(!JSON::Validator.validate({'type' => 'array'}, 3))
135
- assert(!JSON::Validator.validate({'type' => 'array'}, "hello"))
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
136
 
137
137
 
138
138
  # Test null
@@ -149,10 +149,10 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
149
149
  data['a'] = true
150
150
  assert(!JSON::Validator.validate(schema,data))
151
151
 
152
- assert(JSON::Validator.validate({'type' => 'null'}, nil))
153
- assert(!JSON::Validator.validate({'type' => 'null'}, false))
154
- assert(!JSON::Validator.validate({'type' => 'null'}, []))
155
- assert(!JSON::Validator.validate({'type' => 'null'}, "hello"))
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
156
 
157
157
 
158
158
  # Test any
@@ -169,11 +169,11 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
169
169
  data['a'] = true
170
170
  assert(JSON::Validator.validate(schema,data))
171
171
 
172
- assert(JSON::Validator.validate({'type' => 'any'}, true))
173
- assert(JSON::Validator.validate({'type' => 'any'}, nil))
174
- assert(JSON::Validator.validate({'type' => 'any'}, {}))
175
- assert(JSON::Validator.validate({'type' => 'any'}, 3))
176
- assert(JSON::Validator.validate({'type' => 'any'}, "hello"))
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
177
 
178
178
 
179
179
  # Test a union type
@@ -187,8 +187,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
187
187
  data["a"] = false
188
188
  assert(!JSON::Validator.validate(schema,data))
189
189
 
190
- assert(JSON::Validator.validate({'type' => ['string', 'null']}, "hello"))
191
- assert(!JSON::Validator.validate({'type' => ['integer', 'object']}, "hello"))
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
192
 
193
193
  # Test a union type with schemas
194
194
  schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
@@ -462,85 +462,6 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
462
462
  assert(!JSON::Validator.validate(schema,data))
463
463
  end
464
464
 
465
- def test_strict_properties
466
- schema = {
467
- "$schema" => "http://json-schema.org/draft-03/schema#",
468
- "properties" => {
469
- "a" => {"type" => "string"},
470
- "b" => {"type" => "string"}
471
- }
472
- }
473
-
474
- data = {"a" => "a"}
475
- assert(!JSON::Validator.validate(schema,data,:strict => true))
476
-
477
- data = {"b" => "b"}
478
- assert(!JSON::Validator.validate(schema,data,:strict => true))
479
-
480
- data = {"a" => "a", "b" => "b"}
481
- assert(JSON::Validator.validate(schema,data,:strict => true))
482
-
483
- data = {"a" => "a", "b" => "b", "c" => "c"}
484
- assert(!JSON::Validator.validate(schema,data,:strict => true))
485
- end
486
-
487
- def test_strict_properties_additional_props
488
- schema = {
489
- "$schema" => "http://json-schema.org/draft-03/schema#",
490
- "properties" => {
491
- "a" => {"type" => "string"},
492
- "b" => {"type" => "string"}
493
- },
494
- "additionalProperties" => {"type" => "integer"}
495
- }
496
-
497
- data = {"a" => "a"}
498
- assert(!JSON::Validator.validate(schema,data,:strict => true))
499
-
500
- data = {"b" => "b"}
501
- assert(!JSON::Validator.validate(schema,data,:strict => true))
502
-
503
- data = {"a" => "a", "b" => "b"}
504
- assert(JSON::Validator.validate(schema,data,:strict => true))
505
-
506
- data = {"a" => "a", "b" => "b", "c" => "c"}
507
- assert(!JSON::Validator.validate(schema,data,:strict => true))
508
-
509
- data = {"a" => "a", "b" => "b", "c" => 3}
510
- assert(JSON::Validator.validate(schema,data,:strict => true))
511
- end
512
-
513
- def test_strict_properties_pattern_props
514
- schema = {
515
- "$schema" => "http://json-schema.org/draft-03/schema#",
516
- "properties" => {
517
- "a" => {"type" => "string"},
518
- "b" => {"type" => "string"}
519
- },
520
- "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
521
- }
522
-
523
- data = {"a" => "a"}
524
- assert(!JSON::Validator.validate(schema,data,:strict => true))
525
-
526
- data = {"b" => "b"}
527
- assert(!JSON::Validator.validate(schema,data,:strict => true))
528
-
529
- data = {"a" => "a", "b" => "b"}
530
- assert(JSON::Validator.validate(schema,data,:strict => true))
531
-
532
- data = {"a" => "a", "b" => "b", "c" => "c"}
533
- assert(!JSON::Validator.validate(schema,data,:strict => true))
534
-
535
- data = {"a" => "a", "b" => "b", "c" => 3}
536
- assert(!JSON::Validator.validate(schema,data,:strict => true))
537
-
538
- data = {"a" => "a", "b" => "b", "23 taco" => 3}
539
- assert(JSON::Validator.validate(schema,data,:strict => true))
540
-
541
- data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
542
- assert(!JSON::Validator.validate(schema,data,:strict => true))
543
- end
544
465
 
545
466
  def test_pattern
546
467
  # Set up the default datatype
@@ -1032,6 +953,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1032
953
  data2 = {"a" => nil}
1033
954
 
1034
955
  schema = {
956
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1035
957
  "type" => "object",
1036
958
  "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
1037
959
  }
@@ -1059,6 +981,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1059
981
 
1060
982
  def test_dependency
1061
983
  schema = {
984
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1062
985
  "type" => "object",
1063
986
  "properties" => {
1064
987
  "a" => {"type" => "integer"},
@@ -1075,6 +998,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1075
998
  assert(!JSON::Validator.validate(schema,data))
1076
999
 
1077
1000
  schema = {
1001
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1078
1002
  "type" => "object",
1079
1003
  "properties" => {
1080
1004
  "a" => {"type" => "integer"},
@@ -1094,6 +1018,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1094
1018
 
1095
1019
  def test_default
1096
1020
  schema = {
1021
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1097
1022
  "type" => "object",
1098
1023
  "properties" => {
1099
1024
  "a" => {"type" => "integer", "default" => 42},
@@ -1108,6 +1033,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1108
1033
  assert_equal(42, data["a"])
1109
1034
 
1110
1035
  schema = {
1036
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1111
1037
  "type" => "object",
1112
1038
  "properties" => {
1113
1039
  "a" => {"type" => "integer", "default" => 42, "required" => true},
@@ -1122,6 +1048,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1122
1048
  assert_equal(42, data["a"])
1123
1049
 
1124
1050
  schema = {
1051
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1125
1052
  "type" => "object",
1126
1053
  "properties" => {
1127
1054
  "a" => {"type" => "integer", "default" => 42, "required" => true},
@@ -1131,6 +1058,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1131
1058
 
1132
1059
 
1133
1060
  schema = {
1061
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1134
1062
  "type" => "object",
1135
1063
  "properties" => {
1136
1064
  "a" => {"type" => "integer", "default" => 42, "required" => true, "readonly" => true},
@@ -1145,6 +1073,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1145
1073
  assert_nil(data["a"])
1146
1074
 
1147
1075
  schema = {
1076
+ "$schema" => "http://json-schema.org/draft-03/schema#",
1148
1077
  "type" => "object",
1149
1078
  "properties" => {
1150
1079
  "a" => {"type" => "integer", "default" => "42"},
@@ -0,0 +1,1227 @@
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
+
419
+
420
+ def test_unique_items
421
+ # Set up the default datatype
422
+ schema = {
423
+ "$schema" => "http://json-schema.org/draft-04/schema#",
424
+ "properties" => {
425
+ "a" => {"uniqueItems" => true}
426
+ }
427
+ }
428
+
429
+ data = {
430
+ "a" => nil
431
+ }
432
+
433
+ # Test with nulls
434
+ data["a"] = [nil,5]
435
+ assert(JSON::Validator.validate(schema,data))
436
+
437
+ data["a"] = [nil,nil]
438
+ assert(!JSON::Validator.validate(schema,data))
439
+
440
+ # Test with booleans
441
+ data["a"] = [true,4]
442
+ assert(JSON::Validator.validate(schema,data))
443
+
444
+ data["a"] = [true,false]
445
+ assert(JSON::Validator.validate(schema,data))
446
+
447
+ data["a"] = [true,true]
448
+ assert(!JSON::Validator.validate(schema,data))
449
+
450
+ # Test with numbers
451
+ data["a"] = [4,true]
452
+ assert(JSON::Validator.validate(schema,data))
453
+
454
+ data["a"] = [4,4.1]
455
+ assert(JSON::Validator.validate(schema,data))
456
+
457
+ data["a"] = [4,4]
458
+ assert(!JSON::Validator.validate(schema,data))
459
+
460
+ # Test with strings
461
+ data["a"] = ['a',true]
462
+ assert(JSON::Validator.validate(schema,data))
463
+
464
+ data["a"] = ['a','ab']
465
+ assert(JSON::Validator.validate(schema,data))
466
+
467
+ data["a"] = ['a','a']
468
+ assert(!JSON::Validator.validate(schema,data))
469
+
470
+ # Test with arrays
471
+ data["a"] = [[1],true]
472
+ assert(JSON::Validator.validate(schema,data))
473
+
474
+ data["a"] = [[1,2],[1,3]]
475
+ assert(JSON::Validator.validate(schema,data))
476
+
477
+ data["a"] = [[1,2,3],[1,2,3]]
478
+ assert(!JSON::Validator.validate(schema,data))
479
+
480
+ # Test with objects
481
+ data["a"] = [{"a" => 1},true]
482
+ assert(JSON::Validator.validate(schema,data))
483
+
484
+ data["a"] = [{"a" => 1},{"a" => 2}]
485
+ assert(JSON::Validator.validate(schema,data))
486
+
487
+ data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
488
+ assert(!JSON::Validator.validate(schema,data))
489
+ end
490
+
491
+
492
+ def test_pattern
493
+ # Set up the default datatype
494
+ schema = {
495
+ "$schema" => "http://json-schema.org/draft-04/schema#",
496
+ "properties" => {
497
+ "a" => {"pattern" => "\\d+ taco"}
498
+ }
499
+ }
500
+
501
+ data = {
502
+ "a" => nil
503
+ }
504
+
505
+ # Test strings
506
+ data["a"] = "156 taco bell"
507
+ assert(JSON::Validator.validate(schema,data))
508
+
509
+ # Test a non-string
510
+ data["a"] = 5
511
+ assert(JSON::Validator.validate(schema,data))
512
+
513
+ data["a"] = "taco"
514
+ assert(!JSON::Validator.validate(schema,data))
515
+ end
516
+
517
+
518
+ def test_min_length
519
+ # Set up the default datatype
520
+ schema = {
521
+ "$schema" => "http://json-schema.org/draft-04/schema#",
522
+ "properties" => {
523
+ "a" => {"minLength" => 1}
524
+ }
525
+ }
526
+
527
+ data = {
528
+ "a" => nil
529
+ }
530
+
531
+ # Try out strings
532
+ data["a"] = "t"
533
+ assert(JSON::Validator.validate(schema,data))
534
+
535
+ data["a"] = ""
536
+ assert(!JSON::Validator.validate(schema,data))
537
+
538
+ # Try out non-string
539
+ data["a"] = 5
540
+ assert(JSON::Validator.validate(schema,data))
541
+ end
542
+
543
+
544
+ def test_max_length
545
+ # Set up the default datatype
546
+ schema = {
547
+ "$schema" => "http://json-schema.org/draft-04/schema#",
548
+ "properties" => {
549
+ "a" => {"maxLength" => 1}
550
+ }
551
+ }
552
+
553
+ data = {
554
+ "a" => nil
555
+ }
556
+
557
+ # Try out strings
558
+ data["a"] = "t"
559
+ assert(JSON::Validator.validate(schema,data))
560
+
561
+ data["a"] = "tt"
562
+ assert(!JSON::Validator.validate(schema,data))
563
+
564
+ # Try out non-string
565
+ data["a"] = 5
566
+ assert(JSON::Validator.validate(schema,data))
567
+ end
568
+
569
+
570
+ def test_enum
571
+ # Set up the default datatype
572
+ schema = {
573
+ "$schema" => "http://json-schema.org/draft-04/schema#",
574
+ "properties" => {
575
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
576
+ }
577
+ }
578
+
579
+ data = {
580
+ "a" => nil
581
+ }
582
+
583
+ # Make sure all of the above are valid...
584
+ data["a"] = 1
585
+ assert(JSON::Validator.validate(schema,data))
586
+
587
+ data["a"] = 'boo'
588
+ assert(JSON::Validator.validate(schema,data))
589
+
590
+ data["a"] = [1,2,3]
591
+ assert(JSON::Validator.validate(schema,data))
592
+
593
+ data["a"] = {"a" => "b"}
594
+ assert(JSON::Validator.validate(schema,data))
595
+
596
+ # Test something that doesn't exist
597
+ data["a"] = 'taco'
598
+ assert(!JSON::Validator.validate(schema,data))
599
+
600
+ # Try it without the key
601
+ data = {}
602
+ assert(JSON::Validator.validate(schema,data))
603
+ end
604
+
605
+
606
+ def test_multiple_of
607
+ # Set up the default datatype
608
+ schema = {
609
+ "$schema" => "http://json-schema.org/draft-04/schema#",
610
+ "properties" => {
611
+ "a" => {"multipleOf" => 1.1}
612
+ }
613
+ }
614
+
615
+ data = {
616
+ "a" => nil
617
+ }
618
+
619
+ data["a"] = 3.3
620
+ assert(JSON::Validator.validate(schema,data))
621
+
622
+ data["a"] = 3.4
623
+ assert(!JSON::Validator.validate(schema,data))
624
+
625
+ schema["properties"]["a"]["multipleOf"] = 2.0
626
+
627
+ data["a"] = 4.0
628
+ assert(JSON::Validator.validate(schema,data))
629
+
630
+ data["a"] = 'boo'
631
+ assert(JSON::Validator.validate(schema,data))
632
+
633
+ data["a"] = 5
634
+ schema["properties"]["a"]["multipleOf"] = 0
635
+ assert(!JSON::Validator.validate(schema,data))
636
+ end
637
+
638
+
639
+ def test_pattern_properties
640
+ # Set up the default datatype
641
+ schema = {
642
+ "$schema" => "http://json-schema.org/draft-04/schema#",
643
+ "patternProperties" => {
644
+ "\\d+ taco" => {"type" => "integer"}
645
+ }
646
+ }
647
+
648
+ data = {
649
+ "a" => true,
650
+ "1 taco" => 1,
651
+ "20 tacos" => 20
652
+ }
653
+
654
+ assert(JSON::Validator.validate(schema,data))
655
+ data["20 tacos"] = "string!"
656
+ assert(!JSON::Validator.validate(schema,data))
657
+ end
658
+
659
+
660
+ def test_additional_properties
661
+ # Test no additional properties allowed
662
+ schema = {
663
+ "$schema" => "http://json-schema.org/draft-04/schema#",
664
+ "properties" => {
665
+ "a" => { "type" => "integer" }
666
+ },
667
+ "additionalProperties" => false
668
+ }
669
+
670
+ data = {
671
+ "a" => 10
672
+ }
673
+
674
+ assert(JSON::Validator.validate(schema,data))
675
+ data["b"] = 5
676
+ assert(!JSON::Validator.validate(schema,data))
677
+
678
+ # Test additional properties match a schema
679
+ schema["additionalProperties"] = { "type" => "string" }
680
+ data["b"] = "taco"
681
+ assert(JSON::Validator.validate(schema,data))
682
+ data["b"] = 5
683
+ assert(!JSON::Validator.validate(schema,data))
684
+
685
+ # Make sure this works with pattern properties set, too
686
+ schema = {
687
+ "$schema" => "http://json-schema.org/draft-04/schema#",
688
+ "patternProperties" => {
689
+ "\\d+ taco" => {"type" => "integer"}
690
+ },
691
+ "additionalProperties" => false
692
+ }
693
+
694
+ data = {
695
+ "5 tacos" => 5,
696
+ "20 tacos" => 20
697
+ }
698
+
699
+ assert(JSON::Validator.validate(schema,data))
700
+ data["b"] = 5
701
+ assert(!JSON::Validator.validate(schema,data))
702
+ end
703
+
704
+
705
+ def test_items
706
+ schema = {
707
+ "$schema" => "http://json-schema.org/draft-04/schema#",
708
+ "items" => { "type" => "integer" }
709
+ }
710
+
711
+ data = [1,2,4]
712
+ assert(JSON::Validator.validate(schema,data))
713
+ data = [1,2,"string"]
714
+ assert(!JSON::Validator.validate(schema,data))
715
+
716
+ schema = {
717
+ "$schema" => "http://json-schema.org/draft-04/schema#",
718
+ "items" => [
719
+ {"type" => "integer"},
720
+ {"type" => "string"}
721
+ ]
722
+ }
723
+
724
+ data = [1,"string"]
725
+ assert(JSON::Validator.validate(schema,data))
726
+ data = [1,"string",3]
727
+ assert(JSON::Validator.validate(schema,data))
728
+ data = ["string",1]
729
+ assert(!JSON::Validator.validate(schema,data))
730
+
731
+ schema = {
732
+ "$schema" => "http://json-schema.org/draft-04/schema#",
733
+ "items" => [
734
+ {"type" => "integer"},
735
+ {"type" => "string"}
736
+ ],
737
+ "additionalItems" => false
738
+ }
739
+
740
+ data = [1,"string"]
741
+ assert(JSON::Validator.validate(schema,data))
742
+ data = [1,"string",3]
743
+ assert(!JSON::Validator.validate(schema,data))
744
+
745
+ schema = {"$schema" => "http://json-schema.org/draft-04/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
746
+
747
+ data = [1,"string"]
748
+ assert(JSON::Validator.validate(schema,data))
749
+ data = [1,"string",3]
750
+ assert(JSON::Validator.validate(schema,data))
751
+ data = [1,"string","string"]
752
+ assert(!JSON::Validator.validate(schema,data))
753
+ end
754
+
755
+
756
+ def test_list_option
757
+ schema = {
758
+ "$schema" => "http://json-schema.org/draft-04/schema#",
759
+ "type" => "object",
760
+ "required" => ["a"],
761
+ "properties" => { "a" => {"type" => "integer"} }
762
+ }
763
+
764
+ data = [{"a" => 1},{"a" => 2},{"a" => 3}]
765
+ assert(JSON::Validator.validate(schema,data,:list => true))
766
+ assert(!JSON::Validator.validate(schema,data))
767
+
768
+ data = {"a" => 1}
769
+ assert(!JSON::Validator.validate(schema,data,:list => true))
770
+
771
+ data = [{"a" => 1},{"b" => 2},{"a" => 3}]
772
+ assert(!JSON::Validator.validate(schema,data,:list => true))
773
+ end
774
+
775
+
776
+ def test_self_reference
777
+ schema = {
778
+ "$schema" => "http://json-schema.org/draft-04/schema#",
779
+ "type" => "object",
780
+ "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
781
+ }
782
+
783
+ data = {"a" => 5, "b" => {"b" => {"a" => 1}}}
784
+ assert(JSON::Validator.validate(schema,data))
785
+ data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
786
+ assert(!JSON::Validator.validate(schema,data))
787
+ end
788
+
789
+
790
+ def test_format_ipv4
791
+ schema = {
792
+ "$schema" => "http://json-schema.org/draft-04/schema#",
793
+ "type" => "object",
794
+ "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
795
+ }
796
+
797
+ data = {"a" => "1.1.1.1"}
798
+ assert(JSON::Validator.validate(schema,data))
799
+ data = {"a" => "1.1.1"}
800
+ assert(!JSON::Validator.validate(schema,data))
801
+ data = {"a" => "1.1.1.300"}
802
+ assert(!JSON::Validator.validate(schema,data))
803
+ data = {"a" => 5}
804
+ assert(!JSON::Validator.validate(schema,data))
805
+ data = {"a" => "1.1.1"}
806
+ assert(!JSON::Validator.validate(schema,data))
807
+ data = {"a" => "1.1.1.1b"}
808
+ assert(!JSON::Validator.validate(schema,data))
809
+ data = {"a" => "b1.1.1.1"}
810
+ end
811
+
812
+
813
+ def test_format_ipv6
814
+ schema = {
815
+ "$schema" => "http://json-schema.org/draft-04/schema#",
816
+ "type" => "object",
817
+ "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
818
+ }
819
+
820
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
821
+ assert(JSON::Validator.validate(schema,data))
822
+ data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
823
+ assert(JSON::Validator.validate(schema,data))
824
+ data = {"a" => "1111:2222:8888::eeee:ffff"}
825
+ assert(JSON::Validator.validate(schema,data))
826
+ data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
827
+ assert(!JSON::Validator.validate(schema,data))
828
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
829
+ assert(!JSON::Validator.validate(schema,data))
830
+ data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
831
+ assert(!JSON::Validator.validate(schema,data))
832
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
833
+ assert(!JSON::Validator.validate(schema,data))
834
+ end
835
+
836
+ def test_format_time
837
+ schema = {
838
+ "$schema" => "http://json-schema.org/draft-04/schema#",
839
+ "type" => "object",
840
+ "properties" => { "a" => {"type" => "string", "format" => "time"}}
841
+ }
842
+
843
+ data = {"a" => "12:00:00"}
844
+ assert(JSON::Validator.validate(schema,data))
845
+ data = {"a" => "12:00"}
846
+ assert(!JSON::Validator.validate(schema,data))
847
+ data = {"a" => "12:00:60"}
848
+ assert(!JSON::Validator.validate(schema,data))
849
+ data = {"a" => "12:60:00"}
850
+ assert(!JSON::Validator.validate(schema,data))
851
+ data = {"a" => "24:00:00"}
852
+ assert(!JSON::Validator.validate(schema,data))
853
+ data = {"a" => "0:00:00"}
854
+ assert(!JSON::Validator.validate(schema,data))
855
+ data = {"a" => "-12:00:00"}
856
+ assert(!JSON::Validator.validate(schema,data))
857
+ data = {"a" => "12:00:00b"}
858
+ assert(!JSON::Validator.validate(schema,data))
859
+ end
860
+
861
+
862
+ def test_format_date
863
+ schema = {
864
+ "$schema" => "http://json-schema.org/draft-04/schema#",
865
+ "type" => "object",
866
+ "properties" => { "a" => {"type" => "string", "format" => "date"}}
867
+ }
868
+
869
+ data = {"a" => "2010-01-01"}
870
+ assert(JSON::Validator.validate(schema,data))
871
+ data = {"a" => "2010-01-32"}
872
+ assert(!JSON::Validator.validate(schema,data))
873
+ data = {"a" => "n2010-01-01"}
874
+ assert(!JSON::Validator.validate(schema,data))
875
+ data = {"a" => "2010-1-01"}
876
+ assert(!JSON::Validator.validate(schema,data))
877
+ data = {"a" => "2010-01-1"}
878
+ assert(!JSON::Validator.validate(schema,data))
879
+ data = {"a" => "2010-01-01n"}
880
+ assert(!JSON::Validator.validate(schema,data))
881
+ end
882
+
883
+ def test_format_datetime
884
+ schema = {
885
+ "$schema" => "http://json-schema.org/draft-04/schema#",
886
+ "type" => "object",
887
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
888
+ }
889
+
890
+ data = {"a" => "2010-01-01T12:00:00Z"}
891
+ assert(JSON::Validator.validate(schema,data))
892
+ data = {"a" => "2010-01-01T12:00:00.1Z"}
893
+ assert(JSON::Validator.validate(schema,data))
894
+ data = {"a" => "2010-01-01T12:00:00,1Z"}
895
+ assert(JSON::Validator.validate(schema,data))
896
+ data = {"a" => "2010-01-32T12:00:00Z"}
897
+ assert(!JSON::Validator.validate(schema,data))
898
+ data = {"a" => "2010-13-01T12:00:00Z"}
899
+ assert(!JSON::Validator.validate(schema,data))
900
+ data = {"a" => "2010-01-01T24:00:00Z"}
901
+ assert(!JSON::Validator.validate(schema,data))
902
+ data = {"a" => "2010-01-01T12:60:00Z"}
903
+ assert(!JSON::Validator.validate(schema,data))
904
+ data = {"a" => "2010-01-01T12:00:60Z"}
905
+ assert(!JSON::Validator.validate(schema,data))
906
+ data = {"a" => "2010-01-01T12:00:00"}
907
+ assert(!JSON::Validator.validate(schema,data))
908
+ data = {"a" => "2010-01-01T12:00:00z"}
909
+ assert(!JSON::Validator.validate(schema,data))
910
+ data = {"a" => "2010-01-0112:00:00Z"}
911
+ assert(!JSON::Validator.validate(schema,data))
912
+ end
913
+
914
+
915
+ def test_format_union
916
+ data1 = {"a" => "boo"}
917
+ data2 = {"a" => nil}
918
+
919
+ schema = {
920
+ "$schema" => "http://json-schema.org/draft-04/schema#",
921
+ "type" => "object",
922
+ "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
923
+ }
924
+ assert(!JSON::Validator.validate(schema,data1,:version => :draft3))
925
+ assert(JSON::Validator.validate(schema,data2,:version => :draft3))
926
+ end
927
+
928
+
929
+
930
+ def test_schema
931
+ schema = {
932
+ "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
933
+ "type" => "object"
934
+ }
935
+
936
+ data = {"a" => "taco"}
937
+ assert(!JSON::Validator.validate(schema,data))
938
+
939
+ schema = {
940
+ "$schema" => "http://json-schema.org/draft-04/schema#",
941
+ "type" => "object"
942
+ }
943
+ assert(JSON::Validator.validate(schema,data))
944
+ end
945
+
946
+ def test_dependency
947
+ schema = {
948
+ "$schema" => "http://json-schema.org/draft-04/schema#",
949
+ "type" => "object",
950
+ "properties" => {
951
+ "a" => {"type" => "integer"},
952
+ "b" => {"type" => "integer"}
953
+ },
954
+ "dependencies" => {
955
+ "a" => ["b"]
956
+ }
957
+ }
958
+
959
+ data = {"a" => 1, "b" => 2}
960
+ assert(JSON::Validator.validate(schema,data))
961
+ data = {"a" => 1}
962
+ assert(!JSON::Validator.validate(schema,data))
963
+
964
+ schema = {
965
+ "$schema" => "http://json-schema.org/draft-04/schema#",
966
+ "type" => "object",
967
+ "properties" => {
968
+ "a" => {"type" => "integer"},
969
+ "b" => {"type" => "integer"},
970
+ "c" => {"type" => "integer"}
971
+ },
972
+ "dependencies" => {
973
+ "a" => ["b","c"]
974
+ }
975
+ }
976
+
977
+ data = {"a" => 1, "c" => 2}
978
+ assert(!JSON::Validator.validate(schema,data))
979
+ data = {"a" => 1, "b" => 2, "c" => 3}
980
+ assert(JSON::Validator.validate(schema,data))
981
+ end
982
+
983
+ def test_default
984
+ schema = {
985
+ "$schema" => "http://json-schema.org/draft-04/schema#",
986
+ "type" => "object",
987
+ "properties" => {
988
+ "a" => {"type" => "integer", "default" => 42},
989
+ "b" => {"type" => "integer"}
990
+ }
991
+ }
992
+
993
+ data = {"b" => 2}
994
+ assert(JSON::Validator.validate(schema,data))
995
+ assert_nil(data["a"])
996
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
997
+ assert_equal(42, data["a"])
998
+
999
+ schema = {
1000
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1001
+ "type" => "object",
1002
+ "required" => ["a"],
1003
+ "properties" => {
1004
+ "a" => {"type" => "integer", "default" => 42},
1005
+ "b" => {"type" => "integer"}
1006
+ }
1007
+ }
1008
+
1009
+ data = {"b" => 2}
1010
+ assert(!JSON::Validator.validate(schema,data))
1011
+ assert_nil(data["a"])
1012
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1013
+ assert_equal(42, data["a"])
1014
+
1015
+ schema = {
1016
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1017
+ "type" => "object",
1018
+ "required" => ["a"],
1019
+ "properties" => {
1020
+ "a" => {"type" => "integer", "default" => 42},
1021
+ "b" => {"type" => "integer"}
1022
+ }
1023
+ }
1024
+
1025
+
1026
+ schema = {
1027
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1028
+ "type" => "object",
1029
+ "required" => ["a"],
1030
+ "properties" => {
1031
+ "a" => {"type" => "integer", "default" => 42, "readonly" => true},
1032
+ "b" => {"type" => "integer"}
1033
+ }
1034
+ }
1035
+
1036
+ data = {"b" => 2}
1037
+ assert(!JSON::Validator.validate(schema,data))
1038
+ assert_nil(data["a"])
1039
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1040
+ assert_nil(data["a"])
1041
+
1042
+ schema = {
1043
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1044
+ "type" => "object",
1045
+ "properties" => {
1046
+ "a" => {"type" => "integer", "default" => "42"},
1047
+ "b" => {"type" => "integer"}
1048
+ }
1049
+ }
1050
+
1051
+ data = {"b" => 2}
1052
+ assert(JSON::Validator.validate(schema,data))
1053
+ assert_nil(data["a"])
1054
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1055
+ assert_equal("42",data["a"])
1056
+
1057
+ end
1058
+
1059
+
1060
+ def test_all_of
1061
+ schema = {
1062
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1063
+ "allOf" => [
1064
+ {
1065
+ "properties" => {"a" => {"type" => "string"}},
1066
+ "required" => ["a"]
1067
+ },
1068
+ {
1069
+ "properties" => {"b" => {"type" => "integer"}}
1070
+ }
1071
+ ]
1072
+ }
1073
+
1074
+ data = {"a" => "hello", "b" => 5}
1075
+ assert(JSON::Validator.validate(schema,data))
1076
+
1077
+ data = {"a" => "hello"}
1078
+ assert(JSON::Validator.validate(schema,data))
1079
+
1080
+ data = {"a" => "hello", "b" => "taco"}
1081
+ assert(!JSON::Validator.validate(schema,data))
1082
+
1083
+ data = {"b" => 5}
1084
+ assert(!JSON::Validator.validate(schema,data))
1085
+ end
1086
+
1087
+
1088
+ def test_any_of
1089
+ schema = {
1090
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1091
+ "anyOf" => [
1092
+ {
1093
+ "properties" => {"a" => {"type" => "string"}},
1094
+ "required" => ["a"]
1095
+ },
1096
+ {
1097
+ "properties" => {"b" => {"type" => "integer"}}
1098
+ }
1099
+ ]
1100
+ }
1101
+
1102
+ data = {"a" => "hello", "b" => 5}
1103
+ assert(JSON::Validator.validate(schema,data))
1104
+
1105
+ data = {"a" => "hello"}
1106
+ assert(JSON::Validator.validate(schema,data))
1107
+
1108
+ data = {"a" => "hello", "b" => "taco"}
1109
+ assert(JSON::Validator.validate(schema,data))
1110
+
1111
+ data = {"b" => 5}
1112
+ assert(JSON::Validator.validate(schema,data))
1113
+
1114
+ data = {"a" => 5, "b" => "taco"}
1115
+ assert(!JSON::Validator.validate(schema,data))
1116
+ end
1117
+
1118
+
1119
+ def test_one_of
1120
+ schema = {
1121
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1122
+ "oneOf" => [
1123
+ {
1124
+ "properties" => {"a" => {"type" => "string"}},
1125
+ "required" => ["a"]
1126
+ },
1127
+ {
1128
+ "properties" => {"b" => {"type" => "integer"}}
1129
+ }
1130
+ ]
1131
+ }
1132
+
1133
+ data = {"a" => "hello", "b" => 5}
1134
+ assert(!JSON::Validator.validate(schema,data))
1135
+
1136
+ # This passes because b is not required, thus matches both schemas
1137
+ data = {"a" => "hello"}
1138
+ assert(!JSON::Validator.validate(schema,data))
1139
+
1140
+ data = {"a" => "hello", "b" => "taco"}
1141
+ assert(JSON::Validator.validate(schema,data))
1142
+
1143
+ data = {"b" => 5}
1144
+ assert(JSON::Validator.validate(schema,data))
1145
+
1146
+ data = {"a" => 5, "b" => "taco"}
1147
+ assert(!JSON::Validator.validate(schema,data))
1148
+ end
1149
+
1150
+
1151
+ def test_not
1152
+ # Start with a simple not
1153
+ schema = {
1154
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1155
+ "properties" => {
1156
+ "a" => {"not" => { "type" => ["string", "boolean"]}}
1157
+ }
1158
+ }
1159
+
1160
+ data = {"a" => 1}
1161
+ assert(JSON::Validator.validate(schema,data))
1162
+
1163
+ data = {"a" => "hi!"}
1164
+ assert(!JSON::Validator.validate(schema,data))
1165
+
1166
+ data = {"a" => true}
1167
+ assert(!JSON::Validator.validate(schema,data))
1168
+
1169
+ # Sub-schema not
1170
+ schema = {
1171
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1172
+ "properties" => {
1173
+ "a" => {"not" => {"anyOf" => [
1174
+ {
1175
+ "type" => ["string","boolean"]
1176
+ },
1177
+ {
1178
+ "type" => "object",
1179
+ "properties" => {
1180
+ "b" => {"type" => "boolean"}
1181
+ }
1182
+ }
1183
+ ]}
1184
+ }
1185
+ }
1186
+ }
1187
+
1188
+ data = {"a" => 1}
1189
+ assert(JSON::Validator.validate(schema,data))
1190
+
1191
+ data = {"a" => "hi!"}
1192
+ assert(!JSON::Validator.validate(schema,data))
1193
+
1194
+ data = {"a" => true}
1195
+ assert(!JSON::Validator.validate(schema,data))
1196
+
1197
+ data = {"a" => {"b" => true}}
1198
+ assert(!JSON::Validator.validate(schema,data))
1199
+
1200
+ data = {"a" => {"b" => 5}}
1201
+ assert(JSON::Validator.validate(schema,data))
1202
+ end
1203
+
1204
+
1205
+ def test_definitions
1206
+ schema = {
1207
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1208
+ "type" => "array",
1209
+ "items" => { "$ref" => "#/definitions/positiveInteger"},
1210
+ "definitions" => {
1211
+ "positiveInteger" => {
1212
+ "type" => "integer",
1213
+ "minimum" => 0,
1214
+ "exclusiveMinimum" => true
1215
+ }
1216
+ }
1217
+ }
1218
+
1219
+ data = [1,2,3]
1220
+ assert(JSON::Validator.validate(schema,data))
1221
+
1222
+ data = [-1,2,3]
1223
+ assert(!JSON::Validator.validate(schema,data))
1224
+ end
1225
+ end
1226
+
1227
+