json-schema-openc-fork 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE.md +19 -0
- data/README.textile +452 -0
- data/lib/json-schema.rb +19 -0
- data/lib/json-schema/attribute.rb +43 -0
- data/lib/json-schema/attributes/additionalitems.rb +28 -0
- data/lib/json-schema/attributes/additionalproperties.rb +58 -0
- data/lib/json-schema/attributes/allof.rb +39 -0
- data/lib/json-schema/attributes/anyof.rb +47 -0
- data/lib/json-schema/attributes/dependencies.rb +44 -0
- data/lib/json-schema/attributes/disallow.rb +12 -0
- data/lib/json-schema/attributes/divisibleby.rb +22 -0
- data/lib/json-schema/attributes/enum.rb +24 -0
- data/lib/json-schema/attributes/extends.rb +50 -0
- data/lib/json-schema/attributes/format.rb +14 -0
- data/lib/json-schema/attributes/formats/custom.rb +21 -0
- data/lib/json-schema/attributes/formats/date.rb +24 -0
- data/lib/json-schema/attributes/formats/date_time.rb +36 -0
- data/lib/json-schema/attributes/formats/date_time_v4.rb +15 -0
- data/lib/json-schema/attributes/formats/ip.rb +41 -0
- data/lib/json-schema/attributes/formats/time.rb +22 -0
- data/lib/json-schema/attributes/formats/uri.rb +20 -0
- data/lib/json-schema/attributes/items.rb +26 -0
- data/lib/json-schema/attributes/limit.rb +179 -0
- data/lib/json-schema/attributes/maxdecimal.rb +18 -0
- data/lib/json-schema/attributes/multipleof.rb +11 -0
- data/lib/json-schema/attributes/not.rb +30 -0
- data/lib/json-schema/attributes/oneof.rb +56 -0
- data/lib/json-schema/attributes/pattern.rb +18 -0
- data/lib/json-schema/attributes/patternproperties.rb +22 -0
- data/lib/json-schema/attributes/properties.rb +74 -0
- data/lib/json-schema/attributes/properties_optional.rb +26 -0
- data/lib/json-schema/attributes/ref.rb +74 -0
- data/lib/json-schema/attributes/required.rb +28 -0
- data/lib/json-schema/attributes/type.rb +83 -0
- data/lib/json-schema/attributes/type_v4.rb +29 -0
- data/lib/json-schema/attributes/uniqueitems.rb +16 -0
- data/lib/json-schema/errors/custom_format_error.rb +6 -0
- data/lib/json-schema/errors/json_parse_error.rb +6 -0
- data/lib/json-schema/errors/schema_error.rb +6 -0
- data/lib/json-schema/errors/validation_error.rb +46 -0
- data/lib/json-schema/schema.rb +63 -0
- data/lib/json-schema/schema/reader.rb +113 -0
- data/lib/json-schema/schema/validator.rb +36 -0
- data/lib/json-schema/util/array_set.rb +14 -0
- data/lib/json-schema/util/uri.rb +16 -0
- data/lib/json-schema/util/uuid.rb +285 -0
- data/lib/json-schema/validator.rb +592 -0
- data/lib/json-schema/validators/draft1.rb +45 -0
- data/lib/json-schema/validators/draft2.rb +46 -0
- data/lib/json-schema/validators/draft3.rb +50 -0
- data/lib/json-schema/validators/draft4.rb +56 -0
- data/lib/json-schema/validators/hyper-draft4.rb +14 -0
- data/resources/draft-01.json +155 -0
- data/resources/draft-02.json +166 -0
- data/resources/draft-03.json +174 -0
- data/resources/draft-04.json +150 -0
- data/test/data/all_of_ref_data.json +3 -0
- data/test/data/any_of_ref_data.json +7 -0
- data/test/data/bad_data_1.json +3 -0
- data/test/data/good_data_1.json +3 -0
- data/test/data/one_of_ref_links_data.json +5 -0
- data/test/schemas/address_microformat.json +18 -0
- data/test/schemas/all_of_ref_base_schema.json +6 -0
- data/test/schemas/all_of_ref_schema.json +7 -0
- data/test/schemas/any_of_ref_jane_schema.json +4 -0
- data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
- data/test/schemas/any_of_ref_john_schema.json +4 -0
- data/test/schemas/any_of_ref_schema.json +15 -0
- data/test/schemas/definition_schema.json +15 -0
- data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
- data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
- data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
- data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
- data/test/schemas/good_schema_1.json +10 -0
- data/test/schemas/good_schema_2.json +10 -0
- data/test/schemas/good_schema_extends1.json +10 -0
- data/test/schemas/good_schema_extends2.json +13 -0
- data/test/schemas/inner.schema.json +21 -0
- data/test/schemas/one_of_ref_links_schema.json +16 -0
- data/test/schemas/ref john with spaces schema.json +11 -0
- data/test/schemas/relative_definition_schema.json +8 -0
- data/test/schemas/self_link_schema.json +17 -0
- data/test/schemas/up_link_schema.json +17 -0
- data/test/test_all_of_ref_schema.rb +35 -0
- data/test/test_any_of_ref_schema.rb +35 -0
- data/test/test_bad_schema_ref.rb +39 -0
- data/test/test_common_test_suite.rb +66 -0
- data/test/test_custom_format.rb +116 -0
- data/test/test_definition.rb +15 -0
- data/test/test_extended_schema.rb +62 -0
- data/test/test_extends_and_additionalProperties.rb +52 -0
- data/test/test_files_v3.rb +43 -0
- data/test/test_fragment_resolution.rb +30 -0
- data/test/test_fragment_validation_with_ref.rb +34 -0
- data/test/test_full_validation.rb +208 -0
- data/test/test_helper.rb +47 -0
- data/test/test_initialize_data.rb +118 -0
- data/test/test_jsonschema_draft1.rb +171 -0
- data/test/test_jsonschema_draft2.rb +142 -0
- data/test/test_jsonschema_draft3.rb +502 -0
- data/test/test_jsonschema_draft4.rb +704 -0
- data/test/test_list_option.rb +21 -0
- data/test/test_merge_missing_values.rb +45 -0
- data/test/test_minitems.rb +16 -0
- data/test/test_one_of.rb +85 -0
- data/test/test_ruby_schema.rb +59 -0
- data/test/test_schema_loader.rb +74 -0
- data/test/test_schema_type_attribute.rb +20 -0
- data/test/test_schema_validation.rb +185 -0
- data/test/test_stringify.rb +48 -0
- data/test/test_uri_related.rb +67 -0
- data/test/test_validator.rb +53 -0
- metadata +284 -0
@@ -0,0 +1,502 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../test_helper', __FILE__)
|
3
|
+
|
4
|
+
class JSONSchemaDraft3Test < Minitest::Test
|
5
|
+
def schema_version
|
6
|
+
:draft3
|
7
|
+
end
|
8
|
+
|
9
|
+
def exclusive_minimum
|
10
|
+
{ 'exclusiveMinimum' => true }
|
11
|
+
end
|
12
|
+
|
13
|
+
def exclusive_maximum
|
14
|
+
{ 'exclusiveMaximum' => true }
|
15
|
+
end
|
16
|
+
|
17
|
+
def multiple_of
|
18
|
+
'divisibleBy'
|
19
|
+
end
|
20
|
+
|
21
|
+
include ArrayValidation::ItemsTests
|
22
|
+
include ArrayValidation::AdditionalItemsTests
|
23
|
+
include ArrayValidation::UniqueItemsTests
|
24
|
+
|
25
|
+
include NumberValidation::MinMaxTests
|
26
|
+
include NumberValidation::MultipleOfTests
|
27
|
+
|
28
|
+
include ObjectValidation::AdditionalPropertiesTests
|
29
|
+
include ObjectValidation::PatternPropertiesTests
|
30
|
+
|
31
|
+
include StringValidation::ValueTests
|
32
|
+
include StringValidation::FormatTests
|
33
|
+
include StringValidation::DateAndTimeFormatTests
|
34
|
+
|
35
|
+
include TypeValidation::SimpleTypeTests
|
36
|
+
include TypeValidation::AnyTypeTests
|
37
|
+
include TypeValidation::SchemaUnionTypeTests
|
38
|
+
|
39
|
+
def test_types
|
40
|
+
# Set up the default datatype
|
41
|
+
schema = {
|
42
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
43
|
+
"properties" => {
|
44
|
+
"a" => {}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
data = {
|
48
|
+
"a" => nil
|
49
|
+
}
|
50
|
+
|
51
|
+
# Test an array of unioned-type objects that prevent additionalProperties
|
52
|
+
schema["properties"]["a"] = {
|
53
|
+
'type' => 'array',
|
54
|
+
'items' => {
|
55
|
+
'type' => [
|
56
|
+
{ 'type' => 'object', 'properties' => { "b" => { "type" => "integer" } } },
|
57
|
+
{ 'type' => 'object', 'properties' => { "c" => { "type" => "string" } } }
|
58
|
+
],
|
59
|
+
'additionalProperties' => false
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
data["a"] = [{"b" => 5}, {"c" => "foo"}]
|
64
|
+
errors = JSON::Validator.fully_validate(schema, data)
|
65
|
+
assert(errors.empty?, errors.join("\n"))
|
66
|
+
|
67
|
+
# This should actually pass, because this matches the first schema in the union
|
68
|
+
data["a"] << {"c" => false}
|
69
|
+
assert_valid schema, data
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_required
|
73
|
+
# Set up the default datatype
|
74
|
+
schema = {
|
75
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
76
|
+
"properties" => {
|
77
|
+
"a" => {"required" => true}
|
78
|
+
}
|
79
|
+
}
|
80
|
+
data = {}
|
81
|
+
|
82
|
+
refute_valid schema, data
|
83
|
+
data['a'] = "Hello"
|
84
|
+
assert_valid schema, data
|
85
|
+
|
86
|
+
schema = {
|
87
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
88
|
+
"properties" => {
|
89
|
+
"a" => {"type" => "integer"}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
data = {}
|
94
|
+
assert_valid schema, data
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_strict_properties
|
98
|
+
schema = {
|
99
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
100
|
+
"properties" => {
|
101
|
+
"a" => {"type" => "string"},
|
102
|
+
"b" => {"type" => "string"}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
data = {"a" => "a"}
|
107
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
108
|
+
|
109
|
+
data = {"b" => "b"}
|
110
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
111
|
+
|
112
|
+
data = {"a" => "a", "b" => "b"}
|
113
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
114
|
+
|
115
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
116
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_strict_properties_required_props
|
120
|
+
schema = {
|
121
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
122
|
+
"properties" => {
|
123
|
+
"a" => {"type" => "string", "required" => true},
|
124
|
+
"b" => {"type" => "string", "required" => false}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
data = {"a" => "a"}
|
129
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
130
|
+
|
131
|
+
data = {"b" => "b"}
|
132
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
133
|
+
|
134
|
+
data = {"a" => "a", "b" => "b"}
|
135
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_strict_properties_additional_props
|
139
|
+
schema = {
|
140
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
141
|
+
"properties" => {
|
142
|
+
"a" => {"type" => "string"},
|
143
|
+
"b" => {"type" => "string"}
|
144
|
+
},
|
145
|
+
"additionalProperties" => {"type" => "integer"}
|
146
|
+
}
|
147
|
+
|
148
|
+
data = {"a" => "a"}
|
149
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
150
|
+
|
151
|
+
data = {"b" => "b"}
|
152
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
153
|
+
|
154
|
+
data = {"a" => "a", "b" => "b"}
|
155
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
156
|
+
|
157
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
158
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
159
|
+
|
160
|
+
data = {"a" => "a", "b" => "b", "c" => 3}
|
161
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_strict_properties_pattern_props
|
165
|
+
schema = {
|
166
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
167
|
+
"properties" => {
|
168
|
+
"a" => {"type" => "string"},
|
169
|
+
"b" => {"type" => "string"}
|
170
|
+
},
|
171
|
+
"patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
|
172
|
+
}
|
173
|
+
|
174
|
+
data = {"a" => "a"}
|
175
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
176
|
+
|
177
|
+
data = {"b" => "b"}
|
178
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
179
|
+
|
180
|
+
data = {"a" => "a", "b" => "b"}
|
181
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
182
|
+
|
183
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
184
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
185
|
+
|
186
|
+
data = {"a" => "a", "b" => "b", "c" => 3}
|
187
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
188
|
+
|
189
|
+
data = {"a" => "a", "b" => "b", "23 taco" => 3}
|
190
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
191
|
+
|
192
|
+
data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
|
193
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_enum
|
197
|
+
# Set up the default datatype
|
198
|
+
schema = {
|
199
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
200
|
+
"properties" => {
|
201
|
+
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
data = {
|
206
|
+
"a" => nil
|
207
|
+
}
|
208
|
+
|
209
|
+
# Make sure all of the above are valid...
|
210
|
+
data["a"] = 1
|
211
|
+
assert_valid schema, data
|
212
|
+
|
213
|
+
data["a"] = 'boo'
|
214
|
+
assert_valid schema, data
|
215
|
+
|
216
|
+
data["a"] = [1,2,3]
|
217
|
+
assert_valid schema, data
|
218
|
+
|
219
|
+
data["a"] = {"a" => "b"}
|
220
|
+
assert_valid schema, data
|
221
|
+
|
222
|
+
# Test something that doesn't exist
|
223
|
+
data["a"] = 'taco'
|
224
|
+
refute_valid schema, data
|
225
|
+
|
226
|
+
# Try it without the key
|
227
|
+
data = {}
|
228
|
+
assert_valid schema, data
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_disallow
|
232
|
+
# Set up the default datatype
|
233
|
+
schema = {
|
234
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
235
|
+
"properties" => {
|
236
|
+
"a" => {"disallow" => "integer"}
|
237
|
+
}
|
238
|
+
}
|
239
|
+
|
240
|
+
data = {
|
241
|
+
"a" => nil
|
242
|
+
}
|
243
|
+
|
244
|
+
|
245
|
+
data["a"] = 'string'
|
246
|
+
assert_valid schema, data
|
247
|
+
|
248
|
+
data["a"] = 5
|
249
|
+
refute_valid schema, data
|
250
|
+
|
251
|
+
|
252
|
+
schema["properties"]["a"]["disallow"] = ["integer","string"]
|
253
|
+
data["a"] = 'string'
|
254
|
+
refute_valid schema, data
|
255
|
+
|
256
|
+
data["a"] = 5
|
257
|
+
refute_valid schema, data
|
258
|
+
|
259
|
+
data["a"] = false
|
260
|
+
assert_valid schema, data
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
def test_extends
|
267
|
+
schema = {
|
268
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
269
|
+
"properties" => {
|
270
|
+
"a" => { "type" => "integer"}
|
271
|
+
}
|
272
|
+
}
|
273
|
+
|
274
|
+
schema2 = {
|
275
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
276
|
+
"properties" => {
|
277
|
+
"a" => { "maximum" => 5 }
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
data = {
|
282
|
+
"a" => 10
|
283
|
+
}
|
284
|
+
|
285
|
+
assert_valid schema, data
|
286
|
+
assert(!JSON::Validator.validate(schema2,data))
|
287
|
+
|
288
|
+
schema["extends"] = schema2
|
289
|
+
|
290
|
+
refute_valid schema, data
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_list_option
|
294
|
+
schema = {
|
295
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
296
|
+
"type" => "object",
|
297
|
+
"properties" => { "a" => {"type" => "integer", "required" => true} }
|
298
|
+
}
|
299
|
+
|
300
|
+
data = [{"a" => 1},{"a" => 2},{"a" => 3}]
|
301
|
+
assert(JSON::Validator.validate(schema,data,:list => true))
|
302
|
+
refute_valid schema, data
|
303
|
+
|
304
|
+
data = {"a" => 1}
|
305
|
+
assert(!JSON::Validator.validate(schema,data,:list => true))
|
306
|
+
|
307
|
+
data = [{"a" => 1},{"b" => 2},{"a" => 3}]
|
308
|
+
assert(!JSON::Validator.validate(schema,data,:list => true))
|
309
|
+
end
|
310
|
+
|
311
|
+
|
312
|
+
def test_self_reference
|
313
|
+
schema = {
|
314
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
315
|
+
"type" => "object",
|
316
|
+
"properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
|
317
|
+
}
|
318
|
+
|
319
|
+
assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}}
|
320
|
+
refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_format_datetime
|
324
|
+
schema = {
|
325
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
326
|
+
"type" => "object",
|
327
|
+
"properties" => { "a" => {"type" => "string", "format" => "date-time"}}
|
328
|
+
}
|
329
|
+
|
330
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
|
331
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00.1Z"}
|
332
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00,1Z"}
|
333
|
+
refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
|
334
|
+
refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
|
335
|
+
refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
|
336
|
+
refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
|
337
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
|
338
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
|
339
|
+
refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
|
340
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00.1Z\nabc"}
|
341
|
+
|
342
|
+
# test with a specific timezone
|
343
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00+01"}
|
344
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00+01:00"}
|
345
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00+01:30"}
|
346
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00+0234"}
|
347
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00+01:"}
|
348
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00+0"}
|
349
|
+
# do not allow mixing Z and specific timezone
|
350
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00Z+01"}
|
351
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00+01Z"}
|
352
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00+01:30Z"}
|
353
|
+
refute_valid schema, {"a" => "2010-01-01T12:00:00+0Z"}
|
354
|
+
|
355
|
+
# test without any timezone
|
356
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00"}
|
357
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"}
|
358
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00,12345"}
|
359
|
+
assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"}
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_format_uri
|
363
|
+
data1 = {"a" => "http://gitbuh.com"}
|
364
|
+
data2 = {"a" => "::boo"}
|
365
|
+
data3 = {"a" => "http://ja.wikipedia.org/wiki/メインページ"}
|
366
|
+
|
367
|
+
schema = {
|
368
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
369
|
+
"type" => "object",
|
370
|
+
"properties" => { "a" => {"type" => "string", "format" => "uri"}}
|
371
|
+
}
|
372
|
+
|
373
|
+
assert(JSON::Validator.validate(schema,data1))
|
374
|
+
assert(!JSON::Validator.validate(schema,data2))
|
375
|
+
assert(JSON::Validator.validate(schema,data3))
|
376
|
+
end
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
def test_schema
|
381
|
+
schema = {
|
382
|
+
"$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
|
383
|
+
"type" => "object"
|
384
|
+
}
|
385
|
+
|
386
|
+
data = {"a" => "taco"}
|
387
|
+
assert(!JSON::Validator.validate(schema, data))
|
388
|
+
|
389
|
+
schema = {
|
390
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
391
|
+
"type" => "object"
|
392
|
+
}
|
393
|
+
assert_valid schema, data
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_dependency
|
397
|
+
schema = {
|
398
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
399
|
+
"type" => "object",
|
400
|
+
"properties" => {
|
401
|
+
"a" => {"type" => "integer"},
|
402
|
+
"b" => {"type" => "integer"}
|
403
|
+
},
|
404
|
+
"dependencies" => {
|
405
|
+
"a" => "b"
|
406
|
+
}
|
407
|
+
}
|
408
|
+
|
409
|
+
data = {"a" => 1, "b" => 2}
|
410
|
+
assert_valid schema, data
|
411
|
+
data = {"a" => 1}
|
412
|
+
refute_valid schema, data
|
413
|
+
|
414
|
+
schema = {
|
415
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
416
|
+
"type" => "object",
|
417
|
+
"properties" => {
|
418
|
+
"a" => {"type" => "integer"},
|
419
|
+
"b" => {"type" => "integer"},
|
420
|
+
"c" => {"type" => "integer"}
|
421
|
+
},
|
422
|
+
"dependencies" => {
|
423
|
+
"a" => ["b","c"]
|
424
|
+
}
|
425
|
+
}
|
426
|
+
|
427
|
+
data = {"a" => 1, "c" => 2}
|
428
|
+
refute_valid schema, data
|
429
|
+
data = {"a" => 1, "b" => 2, "c" => 3}
|
430
|
+
assert_valid schema, data
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_default
|
434
|
+
schema = {
|
435
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
436
|
+
"type" => "object",
|
437
|
+
"properties" => {
|
438
|
+
"a" => {"type" => "integer", "default" => 42},
|
439
|
+
"b" => {"type" => "integer"}
|
440
|
+
}
|
441
|
+
}
|
442
|
+
|
443
|
+
data = {:b => 2}
|
444
|
+
assert_valid schema, data
|
445
|
+
assert_nil(data["a"])
|
446
|
+
assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
|
447
|
+
assert_equal(42, data["a"])
|
448
|
+
assert_equal(2, data[:b])
|
449
|
+
|
450
|
+
schema = {
|
451
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
452
|
+
"type" => "object",
|
453
|
+
"properties" => {
|
454
|
+
"a" => {"type" => "integer", "default" => 42, "required" => true},
|
455
|
+
"b" => {"type" => "integer"}
|
456
|
+
}
|
457
|
+
}
|
458
|
+
|
459
|
+
data = {:b => 2}
|
460
|
+
refute_valid schema, data
|
461
|
+
assert_nil(data["a"])
|
462
|
+
assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
|
463
|
+
assert_equal(42, data["a"])
|
464
|
+
assert_equal(2, data[:b])
|
465
|
+
|
466
|
+
schema = {
|
467
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
468
|
+
"type" => "object",
|
469
|
+
"properties" => {
|
470
|
+
"a" => {"type" => "integer", "default" => 42, "required" => true, "readonly" => true},
|
471
|
+
"b" => {"type" => "integer"}
|
472
|
+
}
|
473
|
+
}
|
474
|
+
|
475
|
+
data = {:b => 2}
|
476
|
+
refute_valid schema, data
|
477
|
+
assert_nil(data["a"])
|
478
|
+
assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
|
479
|
+
assert_nil(data["a"])
|
480
|
+
assert_equal(2, data[:b])
|
481
|
+
|
482
|
+
schema = {
|
483
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
484
|
+
"type" => "object",
|
485
|
+
"properties" => {
|
486
|
+
"a" => {"type" => "integer", "default" => "42"},
|
487
|
+
"b" => {"type" => "integer"}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
|
491
|
+
data = {:b => 2}
|
492
|
+
assert_valid schema, data
|
493
|
+
assert_nil(data["a"])
|
494
|
+
assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
|
495
|
+
assert_equal("42",data["a"])
|
496
|
+
assert_equal(2, data[:b])
|
497
|
+
|
498
|
+
end
|
499
|
+
|
500
|
+
|
501
|
+
end
|
502
|
+
|