json-schema 2.4.1 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,169 +1,37 @@
|
|
1
|
-
require '
|
2
|
-
require File.dirname(__FILE__) + '/../lib/json-schema'
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
3
2
|
|
4
|
-
class JSONSchemaDraft2Test < Test
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
"properties" => {
|
9
|
-
"a" => {}
|
10
|
-
}
|
11
|
-
}
|
12
|
-
data = {
|
13
|
-
"a" => nil
|
14
|
-
}
|
15
|
-
|
16
|
-
# Test integers
|
17
|
-
schema["properties"]["a"]["type"] = "integer"
|
18
|
-
data["a"] = 5
|
19
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
20
|
-
|
21
|
-
data["a"] = 5.2
|
22
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
23
|
-
|
24
|
-
data['a'] = 'string'
|
25
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
26
|
-
|
27
|
-
data['a'] = true
|
28
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
29
|
-
|
30
|
-
|
31
|
-
# Test numbers
|
32
|
-
schema["properties"]["a"]["type"] = "number"
|
33
|
-
data["a"] = 5
|
34
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
35
|
-
|
36
|
-
data["a"] = 5.2
|
37
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
38
|
-
|
39
|
-
data['a'] = 'string'
|
40
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
41
|
-
|
42
|
-
data['a'] = true
|
43
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
44
|
-
|
45
|
-
|
46
|
-
# Test strings
|
47
|
-
schema["properties"]["a"]["type"] = "string"
|
48
|
-
data["a"] = 5
|
49
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
50
|
-
|
51
|
-
data["a"] = 5.2
|
52
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
53
|
-
|
54
|
-
data['a'] = 'string'
|
55
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
56
|
-
|
57
|
-
data['a'] = true
|
58
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
59
|
-
|
60
|
-
|
61
|
-
# Test booleans
|
62
|
-
schema["properties"]["a"]["type"] = "boolean"
|
63
|
-
data["a"] = 5
|
64
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
65
|
-
|
66
|
-
data["a"] = 5.2
|
67
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
68
|
-
|
69
|
-
data['a'] = 'string'
|
70
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
71
|
-
|
72
|
-
data['a'] = true
|
73
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
74
|
-
|
75
|
-
data['a'] = false
|
76
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
77
|
-
|
78
|
-
|
79
|
-
# Test object
|
80
|
-
schema["properties"]["a"]["type"] = "object"
|
81
|
-
data["a"] = {}
|
82
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
83
|
-
|
84
|
-
data["a"] = 5.2
|
85
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
86
|
-
|
87
|
-
data['a'] = 'string'
|
88
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
89
|
-
|
90
|
-
data['a'] = true
|
91
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
92
|
-
|
93
|
-
|
94
|
-
# Test array
|
95
|
-
schema["properties"]["a"]["type"] = "array"
|
96
|
-
data["a"] = []
|
97
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
98
|
-
|
99
|
-
data["a"] = 5.2
|
100
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
101
|
-
|
102
|
-
data['a'] = 'string'
|
103
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
104
|
-
|
105
|
-
data['a'] = true
|
106
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
107
|
-
|
108
|
-
|
109
|
-
# Test null
|
110
|
-
schema["properties"]["a"]["type"] = "null"
|
111
|
-
data["a"] = nil
|
112
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
113
|
-
|
114
|
-
data["a"] = 5.2
|
115
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
116
|
-
|
117
|
-
data['a'] = 'string'
|
118
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
119
|
-
|
120
|
-
data['a'] = true
|
121
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
122
|
-
|
123
|
-
|
124
|
-
# Test any
|
125
|
-
schema["properties"]["a"]["type"] = "any"
|
126
|
-
data["a"] = 5
|
127
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
128
|
-
|
129
|
-
data["a"] = 5.2
|
130
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
131
|
-
|
132
|
-
data['a'] = 'string'
|
133
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
134
|
-
|
135
|
-
data['a'] = true
|
136
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
137
|
-
|
138
|
-
|
139
|
-
# Test a union type
|
140
|
-
schema["properties"]["a"]["type"] = ["integer","string"]
|
141
|
-
data["a"] = 5
|
142
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
143
|
-
|
144
|
-
data["a"] = 'boo'
|
145
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
3
|
+
class JSONSchemaDraft2Test < Minitest::Test
|
4
|
+
def schema_version
|
5
|
+
:draft2
|
6
|
+
end
|
146
7
|
|
147
|
-
|
148
|
-
|
8
|
+
def exclusive_minimum
|
9
|
+
{ 'minimumCanEqual' => false }
|
10
|
+
end
|
149
11
|
|
150
|
-
|
151
|
-
|
12
|
+
def exclusive_maximum
|
13
|
+
{ 'maximumCanEqual' => false }
|
14
|
+
end
|
152
15
|
|
153
|
-
|
154
|
-
|
16
|
+
def multiple_of
|
17
|
+
'divisibleBy'
|
18
|
+
end
|
155
19
|
|
156
|
-
|
157
|
-
|
20
|
+
include ArrayValidation::ItemsTests
|
21
|
+
include ArrayValidation::UniqueItemsTests
|
158
22
|
|
159
|
-
|
160
|
-
|
23
|
+
include NumberValidation::MinMaxTests
|
24
|
+
include NumberValidation::MultipleOfTests
|
161
25
|
|
162
|
-
|
163
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
164
|
-
end
|
26
|
+
include ObjectValidation::AdditionalPropertiesTests
|
165
27
|
|
28
|
+
include StringValidation::ValueTests
|
29
|
+
include StringValidation::FormatTests
|
30
|
+
include StringValidation::DateAndTimeFormatTests
|
166
31
|
|
32
|
+
include TypeValidation::SimpleTypeTests
|
33
|
+
include TypeValidation::AnyTypeTests
|
34
|
+
include TypeValidation::SchemaUnionTypeTests
|
167
35
|
|
168
36
|
def test_optional
|
169
37
|
# Set up the default datatype
|
@@ -174,9 +42,9 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
174
42
|
}
|
175
43
|
data = {}
|
176
44
|
|
177
|
-
|
45
|
+
refute_valid schema, data
|
178
46
|
data['a'] = "Hello"
|
179
|
-
|
47
|
+
assert_valid schema, data
|
180
48
|
|
181
49
|
schema = {
|
182
50
|
"properties" => {
|
@@ -185,309 +53,9 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
185
53
|
}
|
186
54
|
|
187
55
|
data = {}
|
188
|
-
|
189
|
-
|
56
|
+
assert_valid schema, data
|
190
57
|
end
|
191
58
|
|
192
|
-
|
193
|
-
|
194
|
-
def test_minimum
|
195
|
-
# Set up the default datatype
|
196
|
-
schema = {
|
197
|
-
"properties" => {
|
198
|
-
"a" => {"minimum" => 5}
|
199
|
-
}
|
200
|
-
}
|
201
|
-
|
202
|
-
data = {
|
203
|
-
"a" => nil
|
204
|
-
}
|
205
|
-
|
206
|
-
|
207
|
-
# Test an integer
|
208
|
-
data["a"] = 5
|
209
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
210
|
-
|
211
|
-
data["a"] = 4
|
212
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
213
|
-
|
214
|
-
# Test a float
|
215
|
-
data["a"] = 5.0
|
216
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
217
|
-
|
218
|
-
data["a"] = 4.9
|
219
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
220
|
-
|
221
|
-
# Test a non-number
|
222
|
-
data["a"] = "a string"
|
223
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
224
|
-
|
225
|
-
# Test exclusiveMinimum
|
226
|
-
schema["properties"]["a"]["minimumCanEqual"] = false
|
227
|
-
|
228
|
-
data["a"] = 6
|
229
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
230
|
-
|
231
|
-
data["a"] = 5
|
232
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
233
|
-
|
234
|
-
# Test with float
|
235
|
-
data["a"] = 5.00000001
|
236
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
237
|
-
|
238
|
-
data["a"] = 5.0
|
239
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
240
|
-
end
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
def test_maximum
|
245
|
-
# Set up the default datatype
|
246
|
-
schema = {
|
247
|
-
"properties" => {
|
248
|
-
"a" => {"maximum" => 5}
|
249
|
-
}
|
250
|
-
}
|
251
|
-
|
252
|
-
data = {
|
253
|
-
"a" => nil
|
254
|
-
}
|
255
|
-
|
256
|
-
|
257
|
-
# Test an integer
|
258
|
-
data["a"] = 5
|
259
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
260
|
-
|
261
|
-
data["a"] = 6
|
262
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
263
|
-
|
264
|
-
# Test a float
|
265
|
-
data["a"] = 5.0
|
266
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
267
|
-
|
268
|
-
data["a"] = 5.1
|
269
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
270
|
-
|
271
|
-
# Test a non-number
|
272
|
-
data["a"] = "a string"
|
273
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
274
|
-
|
275
|
-
# Test exclusiveMinimum
|
276
|
-
schema["properties"]["a"]["maximumCanEqual"] = false
|
277
|
-
|
278
|
-
data["a"] = 4
|
279
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
280
|
-
|
281
|
-
data["a"] = 5
|
282
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
283
|
-
|
284
|
-
# Test with float
|
285
|
-
data["a"] = 4.9999999
|
286
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
287
|
-
|
288
|
-
data["a"] = 5.0
|
289
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
290
|
-
end
|
291
|
-
|
292
|
-
|
293
|
-
def test_min_items
|
294
|
-
# Set up the default datatype
|
295
|
-
schema = {
|
296
|
-
"properties" => {
|
297
|
-
"a" => {"minItems" => 1}
|
298
|
-
}
|
299
|
-
}
|
300
|
-
|
301
|
-
data = {
|
302
|
-
"a" => nil
|
303
|
-
}
|
304
|
-
|
305
|
-
# Test with an array
|
306
|
-
data["a"] = ["boo"]
|
307
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
308
|
-
|
309
|
-
data["a"] = []
|
310
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
311
|
-
|
312
|
-
# Test with a non-array
|
313
|
-
data["a"] = "boo"
|
314
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
315
|
-
end
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
def test_max_items
|
320
|
-
# Set up the default datatype
|
321
|
-
schema = {
|
322
|
-
"properties" => {
|
323
|
-
"a" => {"maxItems" => 1}
|
324
|
-
}
|
325
|
-
}
|
326
|
-
|
327
|
-
data = {
|
328
|
-
"a" => nil
|
329
|
-
}
|
330
|
-
|
331
|
-
# Test with an array
|
332
|
-
data["a"] = ["boo"]
|
333
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
334
|
-
|
335
|
-
data["a"] = ["boo","taco"]
|
336
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
337
|
-
|
338
|
-
# Test with a non-array
|
339
|
-
data["a"] = "boo"
|
340
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
341
|
-
end
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
def test_unique_items
|
346
|
-
# Set up the default datatype
|
347
|
-
schema = {
|
348
|
-
"properties" => {
|
349
|
-
"a" => {"uniqueItems" => true}
|
350
|
-
}
|
351
|
-
}
|
352
|
-
|
353
|
-
data = {
|
354
|
-
"a" => nil
|
355
|
-
}
|
356
|
-
|
357
|
-
# Test with nulls
|
358
|
-
data["a"] = [nil,5]
|
359
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
360
|
-
|
361
|
-
data["a"] = [nil,nil]
|
362
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
363
|
-
|
364
|
-
# Test with booleans
|
365
|
-
data["a"] = [true,4]
|
366
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
367
|
-
|
368
|
-
data["a"] = [true,false]
|
369
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
370
|
-
|
371
|
-
data["a"] = [true,true]
|
372
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
373
|
-
|
374
|
-
# Test with numbers
|
375
|
-
data["a"] = [4,true]
|
376
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
377
|
-
|
378
|
-
data["a"] = [4,4.1]
|
379
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
380
|
-
|
381
|
-
data["a"] = [4,4]
|
382
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
383
|
-
|
384
|
-
# Test with strings
|
385
|
-
data["a"] = ['a',true]
|
386
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
387
|
-
|
388
|
-
data["a"] = ['a','ab']
|
389
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
390
|
-
|
391
|
-
data["a"] = ['a','a']
|
392
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
393
|
-
|
394
|
-
# Test with arrays
|
395
|
-
data["a"] = [[1],true]
|
396
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
397
|
-
|
398
|
-
data["a"] = [[1,2],[1,3]]
|
399
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
400
|
-
|
401
|
-
data["a"] = [[1,2,3],[1,2,3]]
|
402
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
403
|
-
|
404
|
-
# Test with objects
|
405
|
-
data["a"] = [{"a" => 1},true]
|
406
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
407
|
-
|
408
|
-
data["a"] = [{"a" => 1},{"a" => 2}]
|
409
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
410
|
-
|
411
|
-
data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
|
412
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
413
|
-
end
|
414
|
-
|
415
|
-
|
416
|
-
def test_pattern
|
417
|
-
# Set up the default datatype
|
418
|
-
schema = {
|
419
|
-
"properties" => {
|
420
|
-
"a" => {"pattern" => "\\d+ taco"}
|
421
|
-
}
|
422
|
-
}
|
423
|
-
|
424
|
-
data = {
|
425
|
-
"a" => nil
|
426
|
-
}
|
427
|
-
|
428
|
-
# Test strings
|
429
|
-
data["a"] = "156 taco bell"
|
430
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
431
|
-
|
432
|
-
# Test a non-string
|
433
|
-
data["a"] = 5
|
434
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
435
|
-
|
436
|
-
data["a"] = "taco"
|
437
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
438
|
-
end
|
439
|
-
|
440
|
-
|
441
|
-
def test_min_length
|
442
|
-
# Set up the default datatype
|
443
|
-
schema = {
|
444
|
-
"properties" => {
|
445
|
-
"a" => {"minLength" => 1}
|
446
|
-
}
|
447
|
-
}
|
448
|
-
|
449
|
-
data = {
|
450
|
-
"a" => nil
|
451
|
-
}
|
452
|
-
|
453
|
-
# Try out strings
|
454
|
-
data["a"] = "t"
|
455
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
456
|
-
|
457
|
-
data["a"] = ""
|
458
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
459
|
-
|
460
|
-
# Try out non-string
|
461
|
-
data["a"] = 5
|
462
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
463
|
-
end
|
464
|
-
|
465
|
-
|
466
|
-
def test_max_length
|
467
|
-
# Set up the default datatype
|
468
|
-
schema = {
|
469
|
-
"properties" => {
|
470
|
-
"a" => {"maxLength" => 1}
|
471
|
-
}
|
472
|
-
}
|
473
|
-
|
474
|
-
data = {
|
475
|
-
"a" => nil
|
476
|
-
}
|
477
|
-
|
478
|
-
# Try out strings
|
479
|
-
data["a"] = "t"
|
480
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
481
|
-
|
482
|
-
data["a"] = "tt"
|
483
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
484
|
-
|
485
|
-
# Try out non-string
|
486
|
-
data["a"] = 5
|
487
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
488
|
-
end
|
489
|
-
|
490
|
-
|
491
59
|
def test_enum
|
492
60
|
# Set up the default datatype
|
493
61
|
schema = {
|
@@ -502,60 +70,26 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
502
70
|
|
503
71
|
# Make sure all of the above are valid...
|
504
72
|
data["a"] = 1
|
505
|
-
|
73
|
+
assert_valid schema, data
|
506
74
|
|
507
75
|
data["a"] = 'boo'
|
508
|
-
|
76
|
+
assert_valid schema, data
|
509
77
|
|
510
78
|
data["a"] = [1,2,3]
|
511
|
-
|
79
|
+
assert_valid schema, data
|
512
80
|
|
513
81
|
data["a"] = {"a" => "b"}
|
514
|
-
|
82
|
+
assert_valid schema, data
|
515
83
|
|
516
84
|
# Test something that doesn't exist
|
517
85
|
data["a"] = 'taco'
|
518
|
-
|
86
|
+
refute_valid schema, data
|
519
87
|
|
520
88
|
# Try it without the key
|
521
89
|
data = {}
|
522
|
-
|
523
|
-
end
|
524
|
-
|
525
|
-
|
526
|
-
def test_divisible_by
|
527
|
-
# Set up the default datatype
|
528
|
-
schema = {
|
529
|
-
"properties" => {
|
530
|
-
"a" => {"divisibleBy" => 1.1}
|
531
|
-
}
|
532
|
-
}
|
533
|
-
|
534
|
-
data = {
|
535
|
-
"a" => nil
|
536
|
-
}
|
537
|
-
|
538
|
-
data["a"] = 3.3
|
539
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
540
|
-
|
541
|
-
data["a"] = 3.4
|
542
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
543
|
-
|
544
|
-
schema["properties"]["a"]["divisibleBy"] = 2.0
|
545
|
-
|
546
|
-
data["a"] = 4.0
|
547
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
548
|
-
|
549
|
-
data["a"] = 'boo'
|
550
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
551
|
-
|
552
|
-
data["a"] = 5
|
553
|
-
schema["properties"]["a"]["divisibleBy"] = 0
|
554
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
90
|
+
assert_valid schema, data
|
555
91
|
end
|
556
92
|
|
557
|
-
|
558
|
-
|
559
93
|
def test_disallow
|
560
94
|
# Set up the default datatype
|
561
95
|
schema = {
|
@@ -570,172 +104,21 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
570
104
|
|
571
105
|
|
572
106
|
data["a"] = 'string'
|
573
|
-
|
107
|
+
assert_valid schema, data
|
574
108
|
|
575
109
|
data["a"] = 5
|
576
|
-
|
110
|
+
refute_valid schema, data
|
577
111
|
|
578
112
|
|
579
113
|
schema["properties"]["a"]["disallow"] = ["integer","string"]
|
580
114
|
data["a"] = 'string'
|
581
|
-
|
115
|
+
refute_valid schema, data
|
582
116
|
|
583
117
|
data["a"] = 5
|
584
|
-
|
118
|
+
refute_valid schema, data
|
585
119
|
|
586
120
|
data["a"] = false
|
587
|
-
|
588
|
-
|
589
|
-
end
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
def test_additional_properties
|
594
|
-
# Test no additional properties allowed
|
595
|
-
schema = {
|
596
|
-
"properties" => {
|
597
|
-
"a" => { "type" => "integer" }
|
598
|
-
},
|
599
|
-
"additionalProperties" => false
|
600
|
-
}
|
601
|
-
|
602
|
-
data = {
|
603
|
-
"a" => 10
|
604
|
-
}
|
605
|
-
|
606
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
607
|
-
data["b"] = 5
|
608
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
609
|
-
|
610
|
-
# Test additional properties match a schema
|
611
|
-
schema["additionalProperties"] = { "type" => "string" }
|
612
|
-
data["b"] = "taco"
|
613
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
614
|
-
data["b"] = 5
|
615
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
616
|
-
end
|
617
|
-
|
618
|
-
|
619
|
-
def test_items
|
620
|
-
schema = {
|
621
|
-
"items" => { "type" => "integer" }
|
622
|
-
}
|
623
|
-
|
624
|
-
data = [1,2,4]
|
625
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
626
|
-
data = [1,2,"string"]
|
627
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
628
|
-
|
629
|
-
schema = {
|
630
|
-
"items" => [
|
631
|
-
{"type" => "integer"},
|
632
|
-
{"type" => "string"}
|
633
|
-
]
|
634
|
-
}
|
635
|
-
|
636
|
-
data = [1,"string"]
|
637
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
638
|
-
data = [1,"string",3]
|
639
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
640
|
-
data = ["string",1]
|
641
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
642
|
-
|
643
|
-
end
|
644
|
-
|
645
|
-
|
646
|
-
def test_format_ipv4
|
647
|
-
schema = {
|
648
|
-
"type" => "object",
|
649
|
-
"properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
|
650
|
-
}
|
651
|
-
|
652
|
-
data = {"a" => "1.1.1.1"}
|
653
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
654
|
-
data = {"a" => "1.1.1"}
|
655
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
656
|
-
data = {"a" => "1.1.1.300"}
|
657
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
658
|
-
data = {"a" => 5}
|
659
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
660
|
-
data = {"a" => "1.1.1"}
|
661
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
662
|
-
data = {"a" => "1.1.1.1b"}
|
663
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
664
|
-
data = {"a" => "b1.1.1.1"}
|
665
|
-
end
|
666
|
-
|
667
|
-
|
668
|
-
def test_format_ipv6
|
669
|
-
schema = {
|
670
|
-
"type" => "object",
|
671
|
-
"properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
|
672
|
-
}
|
673
|
-
|
674
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
|
675
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
676
|
-
data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
|
677
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
678
|
-
data = {"a" => "1111:2222:8888::eeee:ffff"}
|
679
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
680
|
-
data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
|
681
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
682
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
|
683
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
684
|
-
data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
|
685
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
686
|
-
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
687
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
688
|
-
assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft2), 'validate with shortcut')
|
689
|
-
assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft2), 'not validate a simple number')
|
690
|
-
end
|
691
|
-
|
692
|
-
def test_format_time
|
693
|
-
schema = {
|
694
|
-
"type" => "object",
|
695
|
-
"properties" => { "a" => {"type" => "string", "format" => "time"}}
|
696
|
-
}
|
697
|
-
|
698
|
-
data = {"a" => "12:00:00"}
|
699
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
700
|
-
data = {"a" => "12:00"}
|
701
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
702
|
-
data = {"a" => "12:00:60"}
|
703
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
704
|
-
data = {"a" => "12:60:00"}
|
705
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
706
|
-
data = {"a" => "24:00:00"}
|
707
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
708
|
-
data = {"a" => "0:00:00"}
|
709
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
710
|
-
data = {"a" => "-12:00:00"}
|
711
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
712
|
-
data = {"a" => "12:00:00b"}
|
713
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
714
|
-
data = {"a" => "12:00:00\nabc"}
|
715
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
716
|
-
end
|
717
|
-
|
718
|
-
|
719
|
-
def test_format_date
|
720
|
-
schema = {
|
721
|
-
"type" => "object",
|
722
|
-
"properties" => { "a" => {"type" => "string", "format" => "date"}}
|
723
|
-
}
|
724
|
-
|
725
|
-
data = {"a" => "2010-01-01"}
|
726
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
727
|
-
data = {"a" => "2010-01-32"}
|
728
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
729
|
-
data = {"a" => "n2010-01-01"}
|
730
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
731
|
-
data = {"a" => "2010-1-01"}
|
732
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
733
|
-
data = {"a" => "2010-01-1"}
|
734
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
735
|
-
data = {"a" => "2010-01-01n"}
|
736
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
737
|
-
data = {"a" => "2010-01-01\nabc"}
|
738
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
121
|
+
assert_valid schema, data
|
739
122
|
end
|
740
123
|
|
741
124
|
def test_format_datetime
|
@@ -744,49 +127,15 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
744
127
|
"properties" => { "a" => {"type" => "string", "format" => "date-time"}}
|
745
128
|
}
|
746
129
|
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
757
|
-
data = {"a" => "2010-01-01T12:00:60Z"}
|
758
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
759
|
-
data = {"a" => "2010-01-01T12:00:00z"}
|
760
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
761
|
-
data = {"a" => "2010-01-0112:00:00Z"}
|
762
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
763
|
-
data = {"a" => "2010-01-01T12:00:00Z\nabc"}
|
764
|
-
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
765
|
-
end
|
766
|
-
|
767
|
-
def test_format_unknown
|
768
|
-
schema = {
|
769
|
-
"type" => "object",
|
770
|
-
"properties" => { "a" => {"type" => "string", "format" => "unknown"}}
|
771
|
-
}
|
772
|
-
|
773
|
-
data = {"a" => "I can write what I want here"}
|
774
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
775
|
-
data = {"a" => ""}
|
776
|
-
assert(JSON::Validator.validate(schema,data,:version => :draft2))
|
777
|
-
end
|
778
|
-
|
779
|
-
|
780
|
-
def test_format_union
|
781
|
-
data1 = {"a" => "boo"}
|
782
|
-
data2 = {"a" => nil}
|
783
|
-
|
784
|
-
schema = {
|
785
|
-
"type" => "object",
|
786
|
-
"properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
|
787
|
-
}
|
788
|
-
assert(!JSON::Validator.validate(schema,data1,:version => :draft2))
|
789
|
-
assert(JSON::Validator.validate(schema,data2,:version => :draft2))
|
130
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
|
131
|
+
refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
|
132
|
+
refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
|
133
|
+
refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
|
134
|
+
refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
|
135
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
|
136
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
|
137
|
+
refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
|
138
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"}
|
790
139
|
end
|
791
140
|
|
792
141
|
end
|