json-schema 2.4.1 → 2.5.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.
- checksums.yaml +6 -14
- data/README.textile +58 -7
- data/lib/json-schema.rb +3 -1
- data/lib/json-schema/attributes/additionalitems.rb +14 -11
- data/lib/json-schema/attributes/additionalproperties.rb +33 -43
- data/lib/json-schema/attributes/anyof.rb +4 -0
- data/lib/json-schema/attributes/dependencies.rb +31 -19
- data/lib/json-schema/attributes/disallow.rb +2 -3
- data/lib/json-schema/attributes/divisibleby.rb +11 -7
- data/lib/json-schema/attributes/enum.rb +14 -16
- data/lib/json-schema/attributes/format.rb +4 -7
- data/lib/json-schema/attributes/formats/date_time_v4.rb +5 -8
- data/lib/json-schema/attributes/formats/ip.rb +41 -0
- data/lib/json-schema/attributes/formats/uri.rb +10 -8
- data/lib/json-schema/attributes/items.rb +15 -16
- data/lib/json-schema/attributes/limit.rb +179 -0
- data/lib/json-schema/attributes/maxdecimal.rb +7 -6
- data/lib/json-schema/attributes/multipleof.rb +4 -11
- data/lib/json-schema/attributes/not.rb +1 -1
- data/lib/json-schema/attributes/oneof.rb +15 -6
- data/lib/json-schema/attributes/pattern.rb +7 -6
- data/lib/json-schema/attributes/patternproperties.rb +9 -12
- data/lib/json-schema/attributes/properties.rb +55 -39
- data/lib/json-schema/attributes/properties_optional.rb +13 -12
- data/lib/json-schema/attributes/ref.rb +4 -4
- data/lib/json-schema/attributes/required.rb +16 -13
- data/lib/json-schema/attributes/type.rb +13 -18
- data/lib/json-schema/attributes/type_v4.rb +11 -18
- data/lib/json-schema/attributes/uniqueitems.rb +5 -7
- data/lib/json-schema/schema.rb +8 -8
- data/lib/json-schema/schema/#validator.rb# +37 -0
- data/lib/json-schema/schema/reader.rb +113 -0
- data/lib/json-schema/util/uri.rb +16 -0
- data/lib/json-schema/validator.rb +123 -128
- data/lib/json-schema/validators/draft1.rb +1 -1
- data/lib/json-schema/validators/draft2.rb +1 -1
- data/lib/json-schema/validators/draft3.rb +1 -1
- data/lib/json-schema/validators/draft4.rb +1 -1
- data/lib/json-schema/validators/hyper-draft4.rb +1 -1
- data/test/schemas/address_microformat.json +18 -0
- data/test/schemas/definition_schema.json +15 -0
- data/test/schemas/ref john with spaces schema.json +11 -0
- data/test/schemas/relative_definition_schema.json +8 -0
- data/test/test_all_of_ref_schema.rb +12 -15
- data/test/test_any_of_ref_schema.rb +7 -9
- data/test/test_bad_schema_ref.rb +18 -12
- data/test/test_common_test_suite.rb +45 -29
- data/test/test_custom_format.rb +2 -3
- data/test/test_definition.rb +15 -0
- data/test/test_extended_schema.rb +25 -31
- data/test/test_extends_and_additionalProperties.rb +23 -21
- data/test/test_files_v3.rb +14 -23
- data/test/test_fragment_resolution.rb +6 -7
- data/test/test_fragment_validation_with_ref.rb +2 -8
- data/test/test_full_validation.rb +2 -3
- data/test/test_helper.rb +46 -1
- data/test/test_initialize_data.rb +118 -0
- data/test/test_jsonschema_draft1.rb +48 -600
- data/test/test_jsonschema_draft2.rb +48 -699
- data/test/test_jsonschema_draft3.rb +91 -861
- data/test/test_jsonschema_draft4.rb +173 -812
- data/test/test_list_option.rb +6 -7
- data/test/{test_merge_misisng_values.rb → test_merge_missing_values.rb} +2 -3
- data/test/test_minitems.rb +2 -4
- data/test/test_one_of.rb +9 -19
- data/test/test_ruby_schema.rb +5 -14
- data/test/test_schema_loader.rb +74 -0
- data/test/test_schema_type_attribute.rb +2 -3
- data/test/test_schema_validation.rb +4 -5
- data/test/test_stringify.rb +2 -3
- data/test/test_uri_related.rb +67 -0
- data/test/test_validator.rb +53 -0
- metadata +129 -51
- data/lib/json-schema/attributes/dependencies_v4.rb +0 -27
- data/lib/json-schema/attributes/formats/ip4.rb +0 -20
- data/lib/json-schema/attributes/formats/ip6.rb +0 -20
- data/lib/json-schema/attributes/maximum.rb +0 -17
- data/lib/json-schema/attributes/maximum_inclusive.rb +0 -17
- data/lib/json-schema/attributes/maxitems.rb +0 -14
- data/lib/json-schema/attributes/maxlength.rb +0 -16
- data/lib/json-schema/attributes/maxproperties.rb +0 -14
- data/lib/json-schema/attributes/minimum.rb +0 -17
- data/lib/json-schema/attributes/minimum_inclusive.rb +0 -17
- data/lib/json-schema/attributes/minitems.rb +0 -14
- data/lib/json-schema/attributes/minlength.rb +0 -16
- data/lib/json-schema/attributes/minproperties.rb +0 -14
- data/lib/json-schema/attributes/properties_v4.rb +0 -58
- data/lib/json-schema/uri/file.rb +0 -36
@@ -1,195 +1,37 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require '
|
3
|
-
require File.dirname(__FILE__) + '/../lib/json-schema'
|
2
|
+
require File.expand_path('../test_helper', __FILE__)
|
4
3
|
|
5
|
-
class JSONSchemaDraft4Test < Test
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
10
|
-
"properties" => {
|
11
|
-
"a" => {}
|
12
|
-
}
|
13
|
-
}
|
14
|
-
data = {
|
15
|
-
"a" => nil
|
16
|
-
}
|
17
|
-
|
18
|
-
# Test integers
|
19
|
-
schema["properties"]["a"]["type"] = "integer"
|
20
|
-
data["a"] = 5
|
21
|
-
assert(JSON::Validator.validate(schema,data))
|
22
|
-
|
23
|
-
data["a"] = 5.2
|
24
|
-
assert(!JSON::Validator.validate(schema,data))
|
25
|
-
|
26
|
-
data['a'] = 'string'
|
27
|
-
assert(!JSON::Validator.validate(schema,data))
|
28
|
-
|
29
|
-
data['a'] = true
|
30
|
-
assert(!JSON::Validator.validate(schema,data))
|
31
|
-
|
32
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'integer'}, 3))
|
33
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'integer'}, "hello"))
|
34
|
-
|
35
|
-
# Test numbers
|
36
|
-
schema["properties"]["a"]["type"] = "number"
|
37
|
-
data["a"] = 5
|
38
|
-
assert(JSON::Validator.validate(schema,data))
|
39
|
-
|
40
|
-
data["a"] = 5.2
|
41
|
-
assert(JSON::Validator.validate(schema,data))
|
42
|
-
|
43
|
-
data['a'] = 'string'
|
44
|
-
assert(!JSON::Validator.validate(schema,data))
|
45
|
-
|
46
|
-
data['a'] = true
|
47
|
-
assert(!JSON::Validator.validate(schema,data))
|
48
|
-
|
49
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, 3))
|
50
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, 3.14159265358979))
|
51
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, "hello"))
|
52
|
-
|
53
|
-
|
54
|
-
# Test strings
|
55
|
-
schema["properties"]["a"]["type"] = "string"
|
56
|
-
data["a"] = 5
|
57
|
-
assert(!JSON::Validator.validate(schema,data))
|
58
|
-
|
59
|
-
data["a"] = 5.2
|
60
|
-
assert(!JSON::Validator.validate(schema,data))
|
61
|
-
|
62
|
-
data['a'] = 'string'
|
63
|
-
assert(JSON::Validator.validate(schema,data))
|
64
|
-
|
65
|
-
data['a'] = true
|
66
|
-
assert(!JSON::Validator.validate(schema,data))
|
67
|
-
|
68
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, 'hello'))
|
69
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, 3.14159265358979))
|
70
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, []))
|
71
|
-
|
72
|
-
|
73
|
-
# Test booleans
|
74
|
-
schema["properties"]["a"]["type"] = "boolean"
|
75
|
-
data["a"] = 5
|
76
|
-
assert(!JSON::Validator.validate(schema,data))
|
77
|
-
|
78
|
-
data["a"] = 5.2
|
79
|
-
assert(!JSON::Validator.validate(schema,data))
|
80
|
-
|
81
|
-
data['a'] = 'string'
|
82
|
-
assert(!JSON::Validator.validate(schema,data))
|
83
|
-
|
84
|
-
data['a'] = true
|
85
|
-
assert(JSON::Validator.validate(schema,data))
|
86
|
-
|
87
|
-
data['a'] = false
|
88
|
-
assert(JSON::Validator.validate(schema,data))
|
89
|
-
|
90
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, true))
|
91
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, false))
|
92
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, nil))
|
93
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, 3))
|
94
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, "hello"))
|
95
|
-
|
96
|
-
# Test object
|
97
|
-
schema["properties"]["a"]["type"] = "object"
|
98
|
-
data["a"] = {}
|
99
|
-
assert(JSON::Validator.validate(schema,data))
|
100
|
-
|
101
|
-
data["a"] = 5.2
|
102
|
-
assert(!JSON::Validator.validate(schema,data))
|
103
|
-
|
104
|
-
data['a'] = 'string'
|
105
|
-
assert(!JSON::Validator.validate(schema,data))
|
106
|
-
|
107
|
-
data['a'] = true
|
108
|
-
assert(!JSON::Validator.validate(schema,data))
|
109
|
-
|
110
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, {'a' => true}))
|
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'}, []))
|
113
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, 3))
|
114
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, "hello"))
|
115
|
-
|
116
|
-
|
117
|
-
# Test array
|
118
|
-
schema["properties"]["a"]["type"] = "array"
|
119
|
-
data["a"] = []
|
120
|
-
assert(JSON::Validator.validate(schema,data))
|
121
|
-
|
122
|
-
data["a"] = 5.2
|
123
|
-
assert(!JSON::Validator.validate(schema,data))
|
124
|
-
|
125
|
-
data['a'] = 'string'
|
126
|
-
assert(!JSON::Validator.validate(schema,data))
|
127
|
-
|
128
|
-
data['a'] = true
|
129
|
-
assert(!JSON::Validator.validate(schema,data))
|
130
|
-
|
131
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, ['a']))
|
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'}, {}))
|
134
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, 3))
|
135
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, "hello"))
|
136
|
-
|
137
|
-
|
138
|
-
# Test null
|
139
|
-
schema["properties"]["a"]["type"] = "null"
|
140
|
-
data["a"] = nil
|
141
|
-
assert(JSON::Validator.validate(schema,data))
|
142
|
-
|
143
|
-
data["a"] = 5.2
|
144
|
-
assert(!JSON::Validator.validate(schema,data))
|
145
|
-
|
146
|
-
data['a'] = 'string'
|
147
|
-
assert(!JSON::Validator.validate(schema,data))
|
148
|
-
|
149
|
-
data['a'] = true
|
150
|
-
assert(!JSON::Validator.validate(schema,data))
|
151
|
-
|
152
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, nil))
|
153
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, false))
|
154
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, []))
|
155
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, "hello"))
|
156
|
-
|
157
|
-
|
158
|
-
# Test any
|
159
|
-
schema["properties"]["a"]["type"] = "any"
|
160
|
-
data["a"] = 5
|
161
|
-
assert(JSON::Validator.validate(schema,data))
|
162
|
-
|
163
|
-
data["a"] = 5.2
|
164
|
-
assert(JSON::Validator.validate(schema,data))
|
4
|
+
class JSONSchemaDraft4Test < Minitest::Test
|
5
|
+
def schema_version
|
6
|
+
:draft4
|
7
|
+
end
|
165
8
|
|
166
|
-
|
167
|
-
|
9
|
+
def exclusive_minimum
|
10
|
+
{ 'exclusiveMinimum' => true }
|
11
|
+
end
|
168
12
|
|
169
|
-
|
170
|
-
|
13
|
+
def exclusive_maximum
|
14
|
+
{ 'exclusiveMaximum' => true }
|
15
|
+
end
|
171
16
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, 3))
|
176
|
-
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, "hello"))
|
17
|
+
def ipv4_format
|
18
|
+
'ipv4'
|
19
|
+
end
|
177
20
|
|
21
|
+
include ArrayValidation::ItemsTests
|
22
|
+
include ArrayValidation::AdditionalItemsTests
|
23
|
+
include ArrayValidation::UniqueItemsTests
|
178
24
|
|
179
|
-
|
180
|
-
|
181
|
-
data["a"] = 5
|
182
|
-
assert(JSON::Validator.validate(schema,data))
|
25
|
+
include NumberValidation::MinMaxTests
|
26
|
+
include NumberValidation::MultipleOfTests
|
183
27
|
|
184
|
-
|
185
|
-
|
28
|
+
include ObjectValidation::AdditionalPropertiesTests
|
29
|
+
include ObjectValidation::PatternPropertiesTests
|
186
30
|
|
187
|
-
|
188
|
-
|
31
|
+
include StringValidation::ValueTests
|
32
|
+
include StringValidation::FormatTests
|
189
33
|
|
190
|
-
|
191
|
-
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => ['integer', 'object']}, "hello"))
|
192
|
-
end
|
34
|
+
include TypeValidation::SimpleTypeTests
|
193
35
|
|
194
36
|
def test_required
|
195
37
|
# Set up the default datatype
|
@@ -202,9 +44,9 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
202
44
|
}
|
203
45
|
data = {}
|
204
46
|
|
205
|
-
|
47
|
+
refute_valid schema, data
|
206
48
|
data['a'] = "Hello"
|
207
|
-
|
49
|
+
assert_valid schema, data
|
208
50
|
|
209
51
|
schema = {
|
210
52
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
@@ -214,207 +56,30 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
214
56
|
}
|
215
57
|
|
216
58
|
data = {}
|
217
|
-
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
def test_minimum
|
224
|
-
# Set up the default datatype
|
225
|
-
schema = {
|
226
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
227
|
-
"properties" => {
|
228
|
-
"a" => {"minimum" => 5}
|
229
|
-
}
|
230
|
-
}
|
231
|
-
|
232
|
-
data = {
|
233
|
-
"a" => nil
|
234
|
-
}
|
235
|
-
|
236
|
-
|
237
|
-
# Test an integer
|
238
|
-
data["a"] = 5
|
239
|
-
assert(JSON::Validator.validate(schema,data))
|
240
|
-
|
241
|
-
data["a"] = 4
|
242
|
-
assert(!JSON::Validator.validate(schema,data))
|
243
|
-
|
244
|
-
# Test a float
|
245
|
-
data["a"] = 5.0
|
246
|
-
assert(JSON::Validator.validate(schema,data))
|
247
|
-
|
248
|
-
data["a"] = 4.9
|
249
|
-
assert(!JSON::Validator.validate(schema,data))
|
250
|
-
|
251
|
-
# Test a non-number
|
252
|
-
data["a"] = "a string"
|
253
|
-
assert(JSON::Validator.validate(schema,data))
|
254
|
-
|
255
|
-
# Test exclusiveMinimum
|
256
|
-
schema["properties"]["a"]["exclusiveMinimum"] = true
|
257
|
-
|
258
|
-
data["a"] = 6
|
259
|
-
assert(JSON::Validator.validate(schema,data))
|
260
|
-
|
261
|
-
data["a"] = 5
|
262
|
-
assert(!JSON::Validator.validate(schema,data))
|
263
|
-
|
264
|
-
# Test with float
|
265
|
-
data["a"] = 5.00000001
|
266
|
-
assert(JSON::Validator.validate(schema,data))
|
267
|
-
|
268
|
-
data["a"] = 5.0
|
269
|
-
assert(!JSON::Validator.validate(schema,data))
|
270
|
-
end
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
def test_maximum
|
275
|
-
# Set up the default datatype
|
276
|
-
schema = {
|
277
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
278
|
-
"properties" => {
|
279
|
-
"a" => {"maximum" => 5}
|
280
|
-
}
|
281
|
-
}
|
282
|
-
|
283
|
-
data = {
|
284
|
-
"a" => nil
|
285
|
-
}
|
286
|
-
|
287
|
-
|
288
|
-
# Test an integer
|
289
|
-
data["a"] = 5
|
290
|
-
assert(JSON::Validator.validate(schema,data))
|
291
|
-
|
292
|
-
data["a"] = 6
|
293
|
-
assert(!JSON::Validator.validate(schema,data))
|
294
|
-
|
295
|
-
# Test a float
|
296
|
-
data["a"] = 5.0
|
297
|
-
assert(JSON::Validator.validate(schema,data))
|
298
|
-
|
299
|
-
data["a"] = 5.1
|
300
|
-
assert(!JSON::Validator.validate(schema,data))
|
301
|
-
|
302
|
-
# Test a non-number
|
303
|
-
data["a"] = "a string"
|
304
|
-
assert(JSON::Validator.validate(schema,data))
|
305
|
-
|
306
|
-
# Test exclusiveMinimum
|
307
|
-
schema["properties"]["a"]["exclusiveMaximum"] = true
|
308
|
-
|
309
|
-
data["a"] = 4
|
310
|
-
assert(JSON::Validator.validate(schema,data))
|
311
|
-
|
312
|
-
data["a"] = 5
|
313
|
-
assert(!JSON::Validator.validate(schema,data))
|
314
|
-
|
315
|
-
# Test with float
|
316
|
-
data["a"] = 4.9999999
|
317
|
-
assert(JSON::Validator.validate(schema,data))
|
318
|
-
|
319
|
-
data["a"] = 5.0
|
320
|
-
assert(!JSON::Validator.validate(schema,data))
|
321
|
-
end
|
322
|
-
|
323
|
-
|
324
|
-
def test_min_items
|
325
|
-
# Set up the default datatype
|
326
|
-
schema = {
|
327
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
328
|
-
"properties" => {
|
329
|
-
"a" => {"minItems" => 1}
|
330
|
-
}
|
331
|
-
}
|
332
|
-
|
333
|
-
data = {
|
334
|
-
"a" => nil
|
335
|
-
}
|
336
|
-
|
337
|
-
# Test with an array
|
338
|
-
data["a"] = ["boo"]
|
339
|
-
assert(JSON::Validator.validate(schema,data))
|
340
|
-
|
341
|
-
data["a"] = []
|
342
|
-
assert(!JSON::Validator.validate(schema,data))
|
343
|
-
|
344
|
-
# Test with a non-array
|
345
|
-
data["a"] = "boo"
|
346
|
-
assert(JSON::Validator.validate(schema,data))
|
59
|
+
assert_valid schema, data
|
347
60
|
end
|
348
61
|
|
349
|
-
|
350
|
-
|
351
|
-
def test_max_items
|
352
|
-
# Set up the default datatype
|
353
|
-
schema = {
|
354
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
355
|
-
"properties" => {
|
356
|
-
"a" => {"maxItems" => 1}
|
357
|
-
}
|
358
|
-
}
|
359
|
-
|
360
|
-
data = {
|
361
|
-
"a" => nil
|
362
|
-
}
|
363
|
-
|
364
|
-
# Test with an array
|
365
|
-
data["a"] = ["boo"]
|
366
|
-
assert(JSON::Validator.validate(schema,data))
|
367
|
-
|
368
|
-
data["a"] = ["boo","taco"]
|
369
|
-
assert(!JSON::Validator.validate(schema,data))
|
370
|
-
|
371
|
-
# Test with a non-array
|
372
|
-
data["a"] = "boo"
|
373
|
-
assert(JSON::Validator.validate(schema,data))
|
374
|
-
end
|
375
|
-
|
376
|
-
|
377
62
|
def test_min_properties
|
378
|
-
|
379
|
-
schema = {
|
380
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
381
|
-
"minProperties" => 2,
|
382
|
-
"properties" => {
|
383
|
-
}
|
384
|
-
}
|
385
|
-
|
386
|
-
data = {"a" => nil}
|
387
|
-
assert(!JSON::Validator.validate(schema,data))
|
63
|
+
schema = { 'minProperties' => 2 }
|
388
64
|
|
389
|
-
|
390
|
-
|
65
|
+
assert_valid schema, {'a' => 1, 'b' => 2}
|
66
|
+
assert_valid schema, {'a' => 1, 'b' => 2, 'c' => 3}
|
391
67
|
|
392
|
-
|
393
|
-
|
68
|
+
refute_valid schema, {'a' => 1}
|
69
|
+
refute_valid schema, {}
|
394
70
|
end
|
395
71
|
|
396
|
-
|
397
|
-
|
398
72
|
def test_max_properties
|
399
|
-
|
400
|
-
schema = {
|
401
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
402
|
-
"maxProperties" => 2,
|
403
|
-
"properties" => {
|
404
|
-
}
|
405
|
-
}
|
73
|
+
schema = { 'maxProperties' => 2 }
|
406
74
|
|
407
|
-
|
408
|
-
|
75
|
+
assert_valid schema, {'a' => 1, 'b' => 2}
|
76
|
+
assert_valid schema, {'a' => 1}
|
77
|
+
assert_valid schema, {}
|
409
78
|
|
410
|
-
|
411
|
-
assert(JSON::Validator.validate(schema,data))
|
412
|
-
|
413
|
-
data = {"a" => nil, "b" => nil, "c" => nil}
|
414
|
-
assert(!JSON::Validator.validate(schema,data))
|
79
|
+
refute_valid schema, {'a' => 1, 'b' => 2, 'c' => 3}
|
415
80
|
end
|
416
81
|
|
417
|
-
|
82
|
+
def test_strict_properties
|
418
83
|
schema = {
|
419
84
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
420
85
|
"properties" => {
|
@@ -494,156 +159,6 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
494
159
|
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
495
160
|
end
|
496
161
|
|
497
|
-
def test_unique_items
|
498
|
-
# Set up the default datatype
|
499
|
-
schema = {
|
500
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
501
|
-
"properties" => {
|
502
|
-
"a" => {"uniqueItems" => true}
|
503
|
-
}
|
504
|
-
}
|
505
|
-
|
506
|
-
data = {
|
507
|
-
"a" => nil
|
508
|
-
}
|
509
|
-
|
510
|
-
# Test with nulls
|
511
|
-
data["a"] = [nil,5]
|
512
|
-
assert(JSON::Validator.validate(schema,data))
|
513
|
-
|
514
|
-
data["a"] = [nil,nil]
|
515
|
-
assert(!JSON::Validator.validate(schema,data))
|
516
|
-
|
517
|
-
# Test with booleans
|
518
|
-
data["a"] = [true,4]
|
519
|
-
assert(JSON::Validator.validate(schema,data))
|
520
|
-
|
521
|
-
data["a"] = [true,false]
|
522
|
-
assert(JSON::Validator.validate(schema,data))
|
523
|
-
|
524
|
-
data["a"] = [true,true]
|
525
|
-
assert(!JSON::Validator.validate(schema,data))
|
526
|
-
|
527
|
-
# Test with numbers
|
528
|
-
data["a"] = [4,true]
|
529
|
-
assert(JSON::Validator.validate(schema,data))
|
530
|
-
|
531
|
-
data["a"] = [4,4.1]
|
532
|
-
assert(JSON::Validator.validate(schema,data))
|
533
|
-
|
534
|
-
data["a"] = [4,4]
|
535
|
-
assert(!JSON::Validator.validate(schema,data))
|
536
|
-
|
537
|
-
# Test with strings
|
538
|
-
data["a"] = ['a',true]
|
539
|
-
assert(JSON::Validator.validate(schema,data))
|
540
|
-
|
541
|
-
data["a"] = ['a','ab']
|
542
|
-
assert(JSON::Validator.validate(schema,data))
|
543
|
-
|
544
|
-
data["a"] = ['a','a']
|
545
|
-
assert(!JSON::Validator.validate(schema,data))
|
546
|
-
|
547
|
-
# Test with arrays
|
548
|
-
data["a"] = [[1],true]
|
549
|
-
assert(JSON::Validator.validate(schema,data))
|
550
|
-
|
551
|
-
data["a"] = [[1,2],[1,3]]
|
552
|
-
assert(JSON::Validator.validate(schema,data))
|
553
|
-
|
554
|
-
data["a"] = [[1,2,3],[1,2,3]]
|
555
|
-
assert(!JSON::Validator.validate(schema,data))
|
556
|
-
|
557
|
-
# Test with objects
|
558
|
-
data["a"] = [{"a" => 1},true]
|
559
|
-
assert(JSON::Validator.validate(schema,data))
|
560
|
-
|
561
|
-
data["a"] = [{"a" => 1},{"a" => 2}]
|
562
|
-
assert(JSON::Validator.validate(schema,data))
|
563
|
-
|
564
|
-
data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
|
565
|
-
assert(!JSON::Validator.validate(schema,data))
|
566
|
-
end
|
567
|
-
|
568
|
-
|
569
|
-
def test_pattern
|
570
|
-
# Set up the default datatype
|
571
|
-
schema = {
|
572
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
573
|
-
"properties" => {
|
574
|
-
"a" => {"pattern" => "\\d+ taco"}
|
575
|
-
}
|
576
|
-
}
|
577
|
-
|
578
|
-
data = {
|
579
|
-
"a" => nil
|
580
|
-
}
|
581
|
-
|
582
|
-
# Test strings
|
583
|
-
data["a"] = "156 taco bell"
|
584
|
-
assert(JSON::Validator.validate(schema,data))
|
585
|
-
|
586
|
-
# Test a non-string
|
587
|
-
data["a"] = 5
|
588
|
-
assert(JSON::Validator.validate(schema,data))
|
589
|
-
|
590
|
-
data["a"] = "taco"
|
591
|
-
assert(!JSON::Validator.validate(schema,data))
|
592
|
-
end
|
593
|
-
|
594
|
-
|
595
|
-
def test_min_length
|
596
|
-
# Set up the default datatype
|
597
|
-
schema = {
|
598
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
599
|
-
"properties" => {
|
600
|
-
"a" => {"minLength" => 1}
|
601
|
-
}
|
602
|
-
}
|
603
|
-
|
604
|
-
data = {
|
605
|
-
"a" => nil
|
606
|
-
}
|
607
|
-
|
608
|
-
# Try out strings
|
609
|
-
data["a"] = "t"
|
610
|
-
assert(JSON::Validator.validate(schema,data))
|
611
|
-
|
612
|
-
data["a"] = ""
|
613
|
-
assert(!JSON::Validator.validate(schema,data))
|
614
|
-
|
615
|
-
# Try out non-string
|
616
|
-
data["a"] = 5
|
617
|
-
assert(JSON::Validator.validate(schema,data))
|
618
|
-
end
|
619
|
-
|
620
|
-
|
621
|
-
def test_max_length
|
622
|
-
# Set up the default datatype
|
623
|
-
schema = {
|
624
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
625
|
-
"properties" => {
|
626
|
-
"a" => {"maxLength" => 1}
|
627
|
-
}
|
628
|
-
}
|
629
|
-
|
630
|
-
data = {
|
631
|
-
"a" => nil
|
632
|
-
}
|
633
|
-
|
634
|
-
# Try out strings
|
635
|
-
data["a"] = "t"
|
636
|
-
assert(JSON::Validator.validate(schema,data))
|
637
|
-
|
638
|
-
data["a"] = "tt"
|
639
|
-
assert(!JSON::Validator.validate(schema,data))
|
640
|
-
|
641
|
-
# Try out non-string
|
642
|
-
data["a"] = 5
|
643
|
-
assert(JSON::Validator.validate(schema,data))
|
644
|
-
end
|
645
|
-
|
646
|
-
|
647
162
|
def test_enum
|
648
163
|
# Set up the default datatype
|
649
164
|
schema = {
|
@@ -659,24 +174,24 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
659
174
|
|
660
175
|
# Make sure all of the above are valid...
|
661
176
|
data["a"] = 1
|
662
|
-
|
177
|
+
assert_valid schema, data
|
663
178
|
|
664
179
|
data["a"] = 'boo'
|
665
|
-
|
180
|
+
assert_valid schema, data
|
666
181
|
|
667
182
|
data["a"] = [1,2,3]
|
668
|
-
|
183
|
+
assert_valid schema, data
|
669
184
|
|
670
185
|
data["a"] = {"a" => "b"}
|
671
|
-
|
186
|
+
assert_valid schema, data
|
672
187
|
|
673
188
|
# Test something that doesn't exist
|
674
189
|
data["a"] = 'taco'
|
675
|
-
|
190
|
+
refute_valid schema, data
|
676
191
|
|
677
192
|
# Try it without the key
|
678
193
|
data = {}
|
679
|
-
|
194
|
+
assert_valid schema, data
|
680
195
|
end
|
681
196
|
|
682
197
|
def test_enum_with_schema_validation
|
@@ -696,177 +211,115 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
696
211
|
assert(JSON::Validator.validate(schema,data,:validate_schema => true))
|
697
212
|
end
|
698
213
|
|
699
|
-
|
700
|
-
def test_multiple_of
|
701
|
-
# Set up the default datatype
|
214
|
+
def test_list_option
|
702
215
|
schema = {
|
703
216
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
704
|
-
"
|
705
|
-
|
706
|
-
}
|
707
|
-
}
|
708
|
-
|
709
|
-
data = {
|
710
|
-
"a" => nil
|
217
|
+
"type" => "object",
|
218
|
+
"required" => ["a"],
|
219
|
+
"properties" => { "a" => {"type" => "integer"} }
|
711
220
|
}
|
712
221
|
|
713
|
-
data["a"
|
714
|
-
assert(JSON::Validator.validate(schema,data))
|
715
|
-
|
716
|
-
data["a"] = 3.4
|
717
|
-
assert(!JSON::Validator.validate(schema,data))
|
718
|
-
|
719
|
-
schema["properties"]["a"]["multipleOf"] = 2.0
|
720
|
-
|
721
|
-
data["a"] = 4.0
|
722
|
-
assert(JSON::Validator.validate(schema,data))
|
222
|
+
data = [{"a" => 1},{"a" => 2},{"a" => 3}]
|
223
|
+
assert(JSON::Validator.validate(schema,data,:list => true))
|
224
|
+
refute_valid schema, data
|
723
225
|
|
724
|
-
data
|
725
|
-
assert(JSON::Validator.validate(schema,data))
|
226
|
+
data = {"a" => 1}
|
227
|
+
assert(!JSON::Validator.validate(schema,data,:list => true))
|
726
228
|
|
727
|
-
data["a"
|
728
|
-
schema
|
729
|
-
assert(!JSON::Validator.validate(schema,data))
|
229
|
+
data = [{"a" => 1},{"b" => 2},{"a" => 3}]
|
230
|
+
assert(!JSON::Validator.validate(schema,data,:list => true))
|
730
231
|
end
|
731
232
|
|
732
|
-
|
733
|
-
def test_pattern_properties
|
734
|
-
# Set up the default datatype
|
233
|
+
def test_default_with_strict_and_anyof
|
735
234
|
schema = {
|
736
|
-
"
|
737
|
-
|
738
|
-
|
739
|
-
|
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
|
+
]
|
740
254
|
}
|
741
255
|
|
742
256
|
data = {
|
743
|
-
"
|
744
|
-
"1 taco" => 1,
|
745
|
-
"20 tacos" => 20
|
257
|
+
"bar" => "baz"
|
746
258
|
}
|
747
259
|
|
748
|
-
assert(JSON::Validator.validate(schema,data))
|
749
|
-
data["20 tacos"] = "string!"
|
750
|
-
assert(!JSON::Validator.validate(schema,data))
|
260
|
+
assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
|
751
261
|
end
|
752
262
|
|
753
|
-
|
754
|
-
def test_additional_properties
|
755
|
-
# Test no additional properties allowed
|
756
|
-
schema = {
|
757
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
758
|
-
"properties" => {
|
759
|
-
"a" => { "type" => "integer" }
|
760
|
-
},
|
761
|
-
"additionalProperties" => false
|
762
|
-
}
|
763
|
-
|
764
|
-
data = {
|
765
|
-
"a" => 10
|
766
|
-
}
|
767
|
-
|
768
|
-
assert(JSON::Validator.validate(schema,data))
|
769
|
-
data["b"] = 5
|
770
|
-
assert(!JSON::Validator.validate(schema,data))
|
771
|
-
|
772
|
-
# Test additional properties match a schema
|
773
|
-
schema["additionalProperties"] = { "type" => "string" }
|
774
|
-
data["b"] = "taco"
|
775
|
-
assert(JSON::Validator.validate(schema,data))
|
776
|
-
data["b"] = 5
|
777
|
-
assert(!JSON::Validator.validate(schema,data))
|
778
|
-
|
779
|
-
# Make sure this works with pattern properties set, too
|
263
|
+
def test_default_with_anyof
|
780
264
|
schema = {
|
781
|
-
"
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
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
|
+
]
|
786
284
|
}
|
787
285
|
|
788
|
-
data = {
|
789
|
-
"5 tacos" => 5,
|
790
|
-
"20 tacos" => 20
|
791
|
-
}
|
286
|
+
data = {}
|
792
287
|
|
793
|
-
assert(JSON::Validator.validate(schema,data))
|
794
|
-
data[
|
795
|
-
assert(!JSON::Validator.validate(schema,data))
|
288
|
+
assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
|
289
|
+
assert(data['foo'] == 'view')
|
796
290
|
end
|
797
291
|
|
798
|
-
|
799
|
-
def test_items
|
292
|
+
def test_default_with_strict_and_oneof
|
800
293
|
schema = {
|
801
|
-
"
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
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
|
+
}
|
815
312
|
]
|
816
313
|
}
|
817
314
|
|
818
|
-
data =
|
819
|
-
|
820
|
-
data = [1,"string",3]
|
821
|
-
assert(JSON::Validator.validate(schema,data))
|
822
|
-
data = ["string",1]
|
823
|
-
assert(!JSON::Validator.validate(schema,data))
|
824
|
-
|
825
|
-
schema = {
|
826
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
827
|
-
"items" => [
|
828
|
-
{"type" => "integer"},
|
829
|
-
{"type" => "string"}
|
830
|
-
],
|
831
|
-
"additionalItems" => false
|
832
|
-
}
|
833
|
-
|
834
|
-
data = [1,"string"]
|
835
|
-
assert(JSON::Validator.validate(schema,data))
|
836
|
-
data = [1,"string",3]
|
837
|
-
assert(!JSON::Validator.validate(schema,data))
|
838
|
-
|
839
|
-
schema = {"$schema" => "http://json-schema.org/draft-04/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
|
840
|
-
|
841
|
-
data = [1,"string"]
|
842
|
-
assert(JSON::Validator.validate(schema,data))
|
843
|
-
data = [1,"string",3]
|
844
|
-
assert(JSON::Validator.validate(schema,data))
|
845
|
-
data = [1,"string","string"]
|
846
|
-
assert(!JSON::Validator.validate(schema,data))
|
847
|
-
end
|
848
|
-
|
849
|
-
|
850
|
-
def test_list_option
|
851
|
-
schema = {
|
852
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
853
|
-
"type" => "object",
|
854
|
-
"required" => ["a"],
|
855
|
-
"properties" => { "a" => {"type" => "integer"} }
|
315
|
+
data = {
|
316
|
+
"bar" => "baz"
|
856
317
|
}
|
857
318
|
|
858
|
-
data
|
859
|
-
assert(
|
860
|
-
assert(!JSON::Validator.validate(schema,data))
|
861
|
-
|
862
|
-
data = {"a" => 1}
|
863
|
-
assert(!JSON::Validator.validate(schema,data,:list => true))
|
864
|
-
|
865
|
-
data = [{"a" => 1},{"b" => 2},{"a" => 3}]
|
866
|
-
assert(!JSON::Validator.validate(schema,data,:list => true))
|
319
|
+
assert(JSON::Validator.validate(schema, data, :insert_defaults => true, :strict => true))
|
320
|
+
assert(!data.key?('foo'))
|
867
321
|
end
|
868
322
|
|
869
|
-
|
870
323
|
def test_self_reference
|
871
324
|
schema = {
|
872
325
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
@@ -874,62 +327,10 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
874
327
|
"properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
|
875
328
|
}
|
876
329
|
|
877
|
-
|
878
|
-
|
879
|
-
data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
|
880
|
-
assert(!JSON::Validator.validate(schema,data))
|
881
|
-
end
|
882
|
-
|
883
|
-
|
884
|
-
def test_format_ipv4
|
885
|
-
schema = {
|
886
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
887
|
-
"type" => "object",
|
888
|
-
"properties" => { "a" => {"type" => "string", "format" => "ipv4"}}
|
889
|
-
}
|
890
|
-
|
891
|
-
data = {"a" => "1.1.1.1"}
|
892
|
-
assert(JSON::Validator.validate(schema,data))
|
893
|
-
data = {"a" => "1.1.1"}
|
894
|
-
assert(!JSON::Validator.validate(schema,data))
|
895
|
-
data = {"a" => "1.1.1.300"}
|
896
|
-
assert(!JSON::Validator.validate(schema,data))
|
897
|
-
data = {"a" => 5}
|
898
|
-
assert(!JSON::Validator.validate(schema,data))
|
899
|
-
data = {"a" => "1.1.1"}
|
900
|
-
assert(!JSON::Validator.validate(schema,data))
|
901
|
-
data = {"a" => "1.1.1.1b"}
|
902
|
-
assert(!JSON::Validator.validate(schema,data))
|
903
|
-
data = {"a" => "b1.1.1.1"}
|
904
|
-
end
|
905
|
-
|
906
|
-
|
907
|
-
def test_format_ipv6
|
908
|
-
schema = {
|
909
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
910
|
-
"type" => "object",
|
911
|
-
"properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
|
912
|
-
}
|
913
|
-
|
914
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
|
915
|
-
assert(JSON::Validator.validate(schema,data))
|
916
|
-
data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
|
917
|
-
assert(JSON::Validator.validate(schema,data))
|
918
|
-
data = {"a" => "1111:2222:8888::eeee:ffff"}
|
919
|
-
assert(JSON::Validator.validate(schema,data))
|
920
|
-
data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
|
921
|
-
assert(!JSON::Validator.validate(schema,data))
|
922
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
|
923
|
-
assert(!JSON::Validator.validate(schema,data))
|
924
|
-
data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
|
925
|
-
assert(!JSON::Validator.validate(schema,data))
|
926
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
927
|
-
assert(!JSON::Validator.validate(schema,data))
|
928
|
-
assert(JSON::Validator.validate(schema, {"a" => "::1"}), 'validate with shortcut')
|
929
|
-
assert(!JSON::Validator.validate(schema, {"a" => "42"}), 'not validate a simple number')
|
330
|
+
assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}}
|
331
|
+
refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
|
930
332
|
end
|
931
333
|
|
932
|
-
|
933
334
|
def test_format_datetime
|
934
335
|
schema = {
|
935
336
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
@@ -937,30 +338,18 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
937
338
|
"properties" => { "a" => {"type" => "string", "format" => "date-time"}}
|
938
339
|
}
|
939
340
|
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
data = {"a" => "2010-13-01T12:00:00Z"}
|
953
|
-
assert(!JSON::Validator.validate(schema,data))
|
954
|
-
data = {"a" => "2010-01-01T24:00:00Z"}
|
955
|
-
assert(JSON::Validator.validate(schema,data))
|
956
|
-
data = {"a" => "2010-01-01T12:60:00Z"}
|
957
|
-
assert(!JSON::Validator.validate(schema,data))
|
958
|
-
data = {"a" => "2010-01-01T12:00:60Z"}
|
959
|
-
assert(JSON::Validator.validate(schema,data))
|
960
|
-
data = {"a" => "2010-01-01T12:00:00z"}
|
961
|
-
assert(JSON::Validator.validate(schema,data))
|
962
|
-
data = {"a" => "2010-01-0112:00:00Z"}
|
963
|
-
assert(!JSON::Validator.validate(schema,data))
|
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"}
|
964
353
|
end
|
965
354
|
|
966
355
|
def test_format_uri
|
@@ -969,9 +358,9 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
969
358
|
data3 = {"a" => "http://ja.wikipedia.org/wiki/メインページ"}
|
970
359
|
|
971
360
|
schema = {
|
972
|
-
|
973
|
-
|
974
|
-
|
361
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
362
|
+
"type" => "object",
|
363
|
+
"properties" => { "a" => {"type" => "string", "format" => "uri"}}
|
975
364
|
}
|
976
365
|
|
977
366
|
assert(JSON::Validator.validate(schema,data1))
|
@@ -979,34 +368,6 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
979
368
|
assert(JSON::Validator.validate(schema,data3))
|
980
369
|
end
|
981
370
|
|
982
|
-
def test_format_unknown
|
983
|
-
schema = {
|
984
|
-
"type" => "object",
|
985
|
-
"properties" => { "a" => {"type" => "string", "format" => "unknown"}}
|
986
|
-
}
|
987
|
-
|
988
|
-
data = {"a" => "I can write what I want here"}
|
989
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft4))
|
990
|
-
data = {"a" => ""}
|
991
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft4))
|
992
|
-
end
|
993
|
-
|
994
|
-
|
995
|
-
def test_format_union
|
996
|
-
data1 = {"a" => "boo"}
|
997
|
-
data2 = {"a" => nil}
|
998
|
-
|
999
|
-
schema = {
|
1000
|
-
"$schema" => "http://json-schema.org/draft-04/schema#",
|
1001
|
-
"type" => "object",
|
1002
|
-
"properties" => { "a" => {"type" => ["string","null"], "format" => "ipv4"}}
|
1003
|
-
}
|
1004
|
-
assert(!JSON::Validator.validate(schema,data1))
|
1005
|
-
assert(JSON::Validator.validate(schema,data2))
|
1006
|
-
end
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
371
|
def test_schema
|
1011
372
|
schema = {
|
1012
373
|
"$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
|
@@ -1020,7 +381,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1020
381
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
1021
382
|
"type" => "object"
|
1022
383
|
}
|
1023
|
-
|
384
|
+
assert_valid schema, data
|
1024
385
|
end
|
1025
386
|
|
1026
387
|
def test_dependency
|
@@ -1037,9 +398,9 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1037
398
|
}
|
1038
399
|
|
1039
400
|
data = {"a" => 1, "b" => 2}
|
1040
|
-
|
401
|
+
assert_valid schema, data
|
1041
402
|
data = {"a" => 1}
|
1042
|
-
|
403
|
+
refute_valid schema, data
|
1043
404
|
|
1044
405
|
schema = {
|
1045
406
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
@@ -1055,9 +416,9 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1055
416
|
}
|
1056
417
|
|
1057
418
|
data = {"a" => 1, "c" => 2}
|
1058
|
-
|
419
|
+
refute_valid schema, data
|
1059
420
|
data = {"a" => 1, "b" => 2, "c" => 3}
|
1060
|
-
|
421
|
+
assert_valid schema, data
|
1061
422
|
end
|
1062
423
|
|
1063
424
|
def test_schema_dependency
|
@@ -1097,7 +458,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1097
458
|
}
|
1098
459
|
|
1099
460
|
data = {:b => 2}
|
1100
|
-
|
461
|
+
assert_valid schema, data
|
1101
462
|
assert_nil(data["a"])
|
1102
463
|
assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
|
1103
464
|
assert_equal(42, data["a"])
|
@@ -1114,7 +475,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1114
475
|
}
|
1115
476
|
|
1116
477
|
data = {:b => 2}
|
1117
|
-
|
478
|
+
refute_valid schema, data
|
1118
479
|
assert_nil(data["a"])
|
1119
480
|
assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
|
1120
481
|
assert_equal(42, data["a"])
|
@@ -1131,7 +492,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1131
492
|
}
|
1132
493
|
|
1133
494
|
data = {:b => 2}
|
1134
|
-
|
495
|
+
refute_valid schema, data
|
1135
496
|
assert_nil(data["a"])
|
1136
497
|
assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
|
1137
498
|
assert_nil(data["a"])
|
@@ -1147,7 +508,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1147
508
|
}
|
1148
509
|
|
1149
510
|
data = {:b => 2}
|
1150
|
-
|
511
|
+
assert_valid schema, data
|
1151
512
|
assert_nil(data["a"])
|
1152
513
|
assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
|
1153
514
|
assert_equal("42",data["a"])
|
@@ -1171,16 +532,16 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1171
532
|
}
|
1172
533
|
|
1173
534
|
data = {"a" => "hello", "b" => 5}
|
1174
|
-
|
535
|
+
assert_valid schema, data
|
1175
536
|
|
1176
537
|
data = {"a" => "hello"}
|
1177
|
-
|
538
|
+
assert_valid schema, data
|
1178
539
|
|
1179
540
|
data = {"a" => "hello", "b" => "taco"}
|
1180
|
-
|
541
|
+
refute_valid schema, data
|
1181
542
|
|
1182
543
|
data = {"b" => 5}
|
1183
|
-
|
544
|
+
refute_valid schema, data
|
1184
545
|
end
|
1185
546
|
|
1186
547
|
|
@@ -1199,19 +560,19 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1199
560
|
}
|
1200
561
|
|
1201
562
|
data = {"a" => "hello", "b" => 5}
|
1202
|
-
|
563
|
+
assert_valid schema, data
|
1203
564
|
|
1204
565
|
data = {"a" => "hello"}
|
1205
|
-
|
566
|
+
assert_valid schema, data
|
1206
567
|
|
1207
568
|
data = {"a" => "hello", "b" => "taco"}
|
1208
|
-
|
569
|
+
assert_valid schema, data
|
1209
570
|
|
1210
571
|
data = {"b" => 5}
|
1211
|
-
|
572
|
+
assert_valid schema, data
|
1212
573
|
|
1213
574
|
data = {"a" => 5, "b" => "taco"}
|
1214
|
-
|
575
|
+
refute_valid schema, data
|
1215
576
|
end
|
1216
577
|
|
1217
578
|
|
@@ -1230,20 +591,20 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1230
591
|
}
|
1231
592
|
|
1232
593
|
data = {"a" => "hello", "b" => 5}
|
1233
|
-
|
594
|
+
refute_valid schema, data
|
1234
595
|
|
1235
596
|
# This passes because b is not required, thus matches both schemas
|
1236
597
|
data = {"a" => "hello"}
|
1237
|
-
|
598
|
+
refute_valid schema, data
|
1238
599
|
|
1239
600
|
data = {"a" => "hello", "b" => "taco"}
|
1240
|
-
|
601
|
+
assert_valid schema, data
|
1241
602
|
|
1242
603
|
data = {"b" => 5}
|
1243
|
-
|
604
|
+
assert_valid schema, data
|
1244
605
|
|
1245
606
|
data = {"a" => 5, "b" => "taco"}
|
1246
|
-
|
607
|
+
refute_valid schema, data
|
1247
608
|
end
|
1248
609
|
|
1249
610
|
|
@@ -1257,13 +618,13 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1257
618
|
}
|
1258
619
|
|
1259
620
|
data = {"a" => 1}
|
1260
|
-
|
621
|
+
assert_valid schema, data
|
1261
622
|
|
1262
623
|
data = {"a" => "hi!"}
|
1263
|
-
|
624
|
+
refute_valid schema, data
|
1264
625
|
|
1265
626
|
data = {"a" => true}
|
1266
|
-
|
627
|
+
refute_valid schema, data
|
1267
628
|
|
1268
629
|
# Sub-schema not
|
1269
630
|
schema = {
|
@@ -1285,19 +646,19 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1285
646
|
}
|
1286
647
|
|
1287
648
|
data = {"a" => 1}
|
1288
|
-
|
649
|
+
assert_valid schema, data
|
1289
650
|
|
1290
651
|
data = {"a" => "hi!"}
|
1291
|
-
|
652
|
+
refute_valid schema, data
|
1292
653
|
|
1293
654
|
data = {"a" => true}
|
1294
|
-
|
655
|
+
refute_valid schema, data
|
1295
656
|
|
1296
657
|
data = {"a" => {"b" => true}}
|
1297
|
-
|
658
|
+
refute_valid schema, data
|
1298
659
|
|
1299
660
|
data = {"a" => {"b" => 5}}
|
1300
|
-
|
661
|
+
assert_valid schema, data
|
1301
662
|
end
|
1302
663
|
|
1303
664
|
def test_not_fully_validate
|
@@ -1333,10 +694,10 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1333
694
|
}
|
1334
695
|
|
1335
696
|
data = [1,2,3]
|
1336
|
-
|
697
|
+
assert_valid schema, data
|
1337
698
|
|
1338
699
|
data = [-1,2,3]
|
1339
|
-
|
700
|
+
refute_valid schema, data
|
1340
701
|
end
|
1341
702
|
end
|
1342
703
|
|