json-schema 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.textile +30 -18
- data/lib/json-schema/attributes/properties.rb +14 -2
- data/lib/json-schema/validator.rb +15 -13
- data/test/test_jsonschema_draft3.rb +285 -238
- metadata +5 -7
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NDQzOTViYTAzYzllZGY1OWQxMTE5NjczYzM0MTc1YTM5OTI4NDU3OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZDNhMDY5MjQ4MDFmZDMzY2I2MGY4YjY4YTNkZGZiM2Q1YzM3OWVhMg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDRhZjZlOTA3MWUxYjkwZjE1MDFjN2JlZjM1ZGE0ZDU5ZDE1NDVjMzE5NjQ2
|
10
|
+
YjhkZTc4ZGU3MjNlYzA3ZmRlYzRjM2FiYWI4NzMxNzU5NTNkODYwODllZmY3
|
11
|
+
ZmYxMDcyMTA0MTA3MjNiYzQ5YWE5ODVlNzk4ZjdmYmNmY2JiNzE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzY0MzlhNDM5MjUwYjczMjcwNTBjMmNjY2ExN2IwNDEzNDk2NzQ5YjUyMjY4
|
14
|
+
NzRlODJhMmI5YTBjNmViNTZlOTYzZDI4YzE3YjA5YjE5N2Y1NDRhN2Q3ZTBh
|
15
|
+
MDE0MzM2NGYwYjJlYzYxZTk3OWFmMWZkNWI2Y2UzMjM0M2Y5NjY=
|
data/README.textile
CHANGED
@@ -18,7 +18,7 @@ From the git repo:
|
|
18
18
|
|
19
19
|
<pre>
|
20
20
|
$ gem build json-schema.gemspec
|
21
|
-
$ gem install json-schema-1.
|
21
|
+
$ gem install json-schema-1.2.0.gem
|
22
22
|
</pre>
|
23
23
|
|
24
24
|
|
@@ -55,7 +55,7 @@ h3. Validate a JSON string against a JSON schema file
|
|
55
55
|
<pre>
|
56
56
|
require 'rubygems'
|
57
57
|
require 'json-schema'
|
58
|
-
|
58
|
+
|
59
59
|
JSON::Validator.validate('schema.json', '{"a" : 5}')
|
60
60
|
</pre>
|
61
61
|
|
@@ -69,19 +69,31 @@ data = ['user','user','user']
|
|
69
69
|
JSON::Validator.validate('user.json', data, :list => true)
|
70
70
|
</pre>
|
71
71
|
|
72
|
+
h3. Strictly validate an object's properties
|
73
|
+
|
74
|
+
With the <code>:strict</code>code> option, validation fails when an object contains properties that are not defined in the schema's property list or doesn't match the <code>additionalProperties</code> property. Furthermore, all properties are treated as <code>required</code> by default.
|
75
|
+
|
76
|
+
<pre>
|
77
|
+
require 'rubygems'
|
78
|
+
require 'json-schema'
|
79
|
+
|
80
|
+
data = ['user','user','user']
|
81
|
+
JSON::Validator.validate('user.json', data, :list => true)
|
82
|
+
</pre>
|
83
|
+
|
72
84
|
h3. Catch a validation error and print it out
|
73
85
|
|
74
86
|
<pre>
|
75
87
|
require 'rubygems'
|
76
88
|
require 'json-schema'
|
77
|
-
|
89
|
+
|
78
90
|
schema = {
|
79
91
|
"type" => "object",
|
80
92
|
"properties" => {
|
81
93
|
"a" => {"type" => "integer", "required" => true}
|
82
94
|
}
|
83
95
|
}
|
84
|
-
|
96
|
+
|
85
97
|
data = {
|
86
98
|
"a" => "taco"
|
87
99
|
}
|
@@ -99,7 +111,7 @@ h3. Fully validate against a schema and catch all errors
|
|
99
111
|
<pre>
|
100
112
|
require 'rubygems'
|
101
113
|
require 'json-schema'
|
102
|
-
|
114
|
+
|
103
115
|
schema = {
|
104
116
|
"type" => "object",
|
105
117
|
"properties" => {
|
@@ -107,14 +119,14 @@ schema = {
|
|
107
119
|
"b" => {"type" => "string", "required" => "true"}
|
108
120
|
}
|
109
121
|
}
|
110
|
-
|
122
|
+
|
111
123
|
data = {
|
112
124
|
"a" => "taco"
|
113
125
|
}
|
114
126
|
|
115
127
|
errors = JSON::Validator.fully_validate(schema, data)
|
116
128
|
|
117
|
-
# ["The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", "The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#"]
|
129
|
+
# ["The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", "The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#"]
|
118
130
|
</pre>
|
119
131
|
|
120
132
|
h3. Fully validate against a schema and catch all errors as objects
|
@@ -122,7 +134,7 @@ h3. Fully validate against a schema and catch all errors as objects
|
|
122
134
|
<pre>
|
123
135
|
require 'rubygems'
|
124
136
|
require 'json-schema'
|
125
|
-
|
137
|
+
|
126
138
|
schema = {
|
127
139
|
"type" => "object",
|
128
140
|
"properties" => {
|
@@ -130,7 +142,7 @@ schema = {
|
|
130
142
|
"b" => {"type" => "string", "required" => "true"}
|
131
143
|
}
|
132
144
|
}
|
133
|
-
|
145
|
+
|
134
146
|
data = {
|
135
147
|
"a" => "taco"
|
136
148
|
}
|
@@ -163,7 +175,7 @@ JSON::Validator.validate(schema, data, :validate_schema => true)
|
|
163
175
|
|
164
176
|
h3. Validate a JSON object against a JSON schema object, while inserting default values from the schema
|
165
177
|
|
166
|
-
With the :insert_defaults option set to true any missing property that has a
|
178
|
+
With the :insert_defaults option set to true any missing property that has a
|
167
179
|
default value specified in the schema will be inserted into the validated data. The inserted default value is validated hence catching a schema that specifies an invalid default value.
|
168
180
|
|
169
181
|
<pre>
|
@@ -172,13 +184,13 @@ require 'json-schema'
|
|
172
184
|
|
173
185
|
schema = {
|
174
186
|
"type" => "object",
|
175
|
-
"properties" => {
|
187
|
+
"properties" => {
|
176
188
|
"a" => {"type" => "integer", "default" => 42, "required" => "true"},
|
177
189
|
"b" => {"type" => "integer"}
|
178
190
|
}
|
179
191
|
}
|
180
192
|
|
181
|
-
# Would not normally validate because "a" is missing and required by schema,
|
193
|
+
# Would not normally validate because "a" is missing and required by schema,
|
182
194
|
# but "default" option allows insertion of valid default.
|
183
195
|
data = {
|
184
196
|
"b" => 5
|
@@ -189,9 +201,9 @@ JSON::Validator.validate(schema, data)
|
|
189
201
|
|
190
202
|
JSON::Validator.validate(schema, data, :insert_defaults => true)
|
191
203
|
# true
|
192
|
-
# data = {
|
193
|
-
# "a" => 42,
|
194
|
-
# "b" => 5
|
204
|
+
# data = {
|
205
|
+
# "a" => 42,
|
206
|
+
# "b" => 5
|
195
207
|
# }
|
196
208
|
|
197
209
|
</pre>
|
@@ -223,7 +235,7 @@ For this example, we are going to extend the "JSON Schema Draft 3":http://tools.
|
|
223
235
|
<pre>
|
224
236
|
require 'rubygems'
|
225
237
|
require 'json-schema'
|
226
|
-
|
238
|
+
|
227
239
|
class BitwiseAndAttribute < JSON::Schema::Attribute
|
228
240
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
229
241
|
if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
|
@@ -240,7 +252,7 @@ class ExtendedSchema < JSON::Schema::Validator
|
|
240
252
|
@attributes["bitwise-and"] = BitwiseAndAttribute
|
241
253
|
@uri = URI.parse("http://test.com/test.json")
|
242
254
|
end
|
243
|
-
|
255
|
+
|
244
256
|
JSON::Validator.register_validator(self.new)
|
245
257
|
end
|
246
258
|
|
@@ -277,7 +289,7 @@ If more than one of the supported JSON backends are installed, the <code>yajl-ru
|
|
277
289
|
<pre>
|
278
290
|
JSON::Validator.json_backend = :json
|
279
291
|
</pre>
|
280
|
-
|
292
|
+
|
281
293
|
Optionally, the JSON Schema library supports using the MultiJSON library for selecting JSON backends. If the MultiJSON library is installed, it will be autoloaded.
|
282
294
|
|
283
295
|
h2. Notes
|
@@ -9,11 +9,11 @@ module JSON
|
|
9
9
|
data[property] = (default.is_a?(Hash) ? default.clone : default)
|
10
10
|
end
|
11
11
|
|
12
|
-
if property_schema['required'] and !data.has_key?(property)
|
12
|
+
if (property_schema['required'] || options[:strict] == true) and !data.has_key?(property)
|
13
13
|
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
|
14
14
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
if data.has_key?(property)
|
18
18
|
schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
|
19
19
|
fragments << property
|
@@ -21,6 +21,18 @@ module JSON
|
|
21
21
|
fragments.pop
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
# When strict is true, ensure no undefined properties exist in the data
|
26
|
+
if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
|
27
|
+
diff = data.select do |k,v|
|
28
|
+
!current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
|
29
|
+
end
|
30
|
+
|
31
|
+
if diff.size > 0
|
32
|
+
message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{diff.keys.join(", ")}'"
|
33
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
34
|
+
end
|
35
|
+
end
|
24
36
|
end
|
25
37
|
end
|
26
38
|
end
|
@@ -20,7 +20,7 @@ module JSON
|
|
20
20
|
message = "#{message} in schema #{schema.uri}"
|
21
21
|
super(message)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def to_string
|
25
25
|
if @sub_errors.empty?
|
26
26
|
message
|
@@ -30,7 +30,7 @@ module JSON
|
|
30
30
|
full_message
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def to_hash
|
35
35
|
base = {:schema => @schema.uri, :fragment => ::JSON::Schema::Attribute.build_fragment(fragments), :message => message, :failed_attribute => @failed_attribute.to_s.split(":").last.split("Attribute").first}
|
36
36
|
if !@sub_errors.empty?
|
@@ -62,7 +62,7 @@ module JSON
|
|
62
62
|
raise error
|
63
63
|
end
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def self.validation_errors(validator)
|
67
67
|
validator.validation_errors
|
68
68
|
end
|
@@ -111,7 +111,8 @@ module JSON
|
|
111
111
|
:validate_schema => false,
|
112
112
|
:record_errors => false,
|
113
113
|
:errors_as_objects => false,
|
114
|
-
:insert_defaults => false
|
114
|
+
:insert_defaults => false,
|
115
|
+
:strict => false
|
115
116
|
}
|
116
117
|
@@validators = {}
|
117
118
|
@@default_validator = nil
|
@@ -119,7 +120,7 @@ module JSON
|
|
119
120
|
@@json_backend = nil
|
120
121
|
@@serializer = nil
|
121
122
|
@@mutex = Mutex.new
|
122
|
-
|
123
|
+
|
123
124
|
def self.version_string_for(version)
|
124
125
|
# I'm not a fan of this, but it's quick and dirty to get it working for now
|
125
126
|
return "draft-03" unless version
|
@@ -154,7 +155,8 @@ module JSON
|
|
154
155
|
|
155
156
|
@validation_options = @options[:record_errors] ? {:record_errors => true} : {}
|
156
157
|
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
|
157
|
-
|
158
|
+
@validation_options[:strict] = true if @options[:strict] == true
|
159
|
+
|
158
160
|
# validate the schema, if requested
|
159
161
|
if @options[:validate_schema]
|
160
162
|
begin
|
@@ -164,7 +166,7 @@ module JSON
|
|
164
166
|
raise $!
|
165
167
|
end
|
166
168
|
end
|
167
|
-
|
169
|
+
|
168
170
|
@@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
|
169
171
|
@data = initialize_data(data)
|
170
172
|
@@mutex.synchronize { build_schemas(@base_schema) }
|
@@ -295,7 +297,7 @@ module JSON
|
|
295
297
|
def validation_error(error)
|
296
298
|
@errors.push(error)
|
297
299
|
end
|
298
|
-
|
300
|
+
|
299
301
|
def validation_errors
|
300
302
|
@errors
|
301
303
|
end
|
@@ -394,7 +396,7 @@ module JSON
|
|
394
396
|
@@json_backend
|
395
397
|
end
|
396
398
|
end
|
397
|
-
|
399
|
+
|
398
400
|
def json_backend=(backend)
|
399
401
|
if defined?(MultiJson)
|
400
402
|
backend = backend == 'json' ? 'json_gem' : backend
|
@@ -425,7 +427,7 @@ module JSON
|
|
425
427
|
end
|
426
428
|
end
|
427
429
|
end
|
428
|
-
|
430
|
+
|
429
431
|
if !defined?(MultiJson)
|
430
432
|
if begin
|
431
433
|
Gem::Specification::find_by_name('json')
|
@@ -438,7 +440,7 @@ module JSON
|
|
438
440
|
@@available_json_backends << 'json'
|
439
441
|
@@json_backend = 'json'
|
440
442
|
end
|
441
|
-
|
443
|
+
|
442
444
|
# Try force-loading json for rubies > 1.9.2
|
443
445
|
begin
|
444
446
|
require 'json'
|
@@ -458,11 +460,11 @@ module JSON
|
|
458
460
|
@@available_json_backends << 'yajl'
|
459
461
|
@@json_backend = 'yajl'
|
460
462
|
end
|
461
|
-
|
463
|
+
|
462
464
|
if @@json_backend == 'yajl'
|
463
465
|
@@serializer = lambda{|o| Yajl::Encoder.encode(o) }
|
464
466
|
else
|
465
|
-
@@serializer = lambda{|o| Marshal.dump(o) }
|
467
|
+
@@serializer = lambda{|o| Marshal.dump(o) }
|
466
468
|
end
|
467
469
|
end
|
468
470
|
end
|
@@ -13,7 +13,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
13
13
|
data = {
|
14
14
|
"a" => nil
|
15
15
|
}
|
16
|
-
|
16
|
+
|
17
17
|
# Test integers
|
18
18
|
schema["properties"]["a"]["type"] = "integer"
|
19
19
|
data["a"] = 5
|
@@ -21,16 +21,16 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
21
21
|
|
22
22
|
data["a"] = 5.2
|
23
23
|
assert(!JSON::Validator.validate(schema,data))
|
24
|
-
|
24
|
+
|
25
25
|
data['a'] = 'string'
|
26
26
|
assert(!JSON::Validator.validate(schema,data))
|
27
|
-
|
27
|
+
|
28
28
|
data['a'] = true
|
29
29
|
assert(!JSON::Validator.validate(schema,data))
|
30
|
-
|
30
|
+
|
31
31
|
assert(JSON::Validator.validate({'type' => 'integer'}, 3))
|
32
32
|
assert(!JSON::Validator.validate({'type' => 'integer'}, "hello"))
|
33
|
-
|
33
|
+
|
34
34
|
# Test numbers
|
35
35
|
schema["properties"]["a"]["type"] = "number"
|
36
36
|
data["a"] = 5
|
@@ -38,18 +38,18 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
data["a"] = 5.2
|
40
40
|
assert(JSON::Validator.validate(schema,data))
|
41
|
-
|
41
|
+
|
42
42
|
data['a'] = 'string'
|
43
43
|
assert(!JSON::Validator.validate(schema,data))
|
44
|
-
|
44
|
+
|
45
45
|
data['a'] = true
|
46
46
|
assert(!JSON::Validator.validate(schema,data))
|
47
|
-
|
47
|
+
|
48
48
|
assert(JSON::Validator.validate({'type' => 'number'}, 3))
|
49
49
|
assert(JSON::Validator.validate({'type' => 'number'}, 3.14159265358979))
|
50
50
|
assert(!JSON::Validator.validate({'type' => 'number'}, "hello"))
|
51
|
-
|
52
|
-
|
51
|
+
|
52
|
+
|
53
53
|
# Test strings
|
54
54
|
schema["properties"]["a"]["type"] = "string"
|
55
55
|
data["a"] = 5
|
@@ -57,18 +57,18 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
57
57
|
|
58
58
|
data["a"] = 5.2
|
59
59
|
assert(!JSON::Validator.validate(schema,data))
|
60
|
-
|
60
|
+
|
61
61
|
data['a'] = 'string'
|
62
62
|
assert(JSON::Validator.validate(schema,data))
|
63
|
-
|
63
|
+
|
64
64
|
data['a'] = true
|
65
65
|
assert(!JSON::Validator.validate(schema,data))
|
66
|
-
|
66
|
+
|
67
67
|
assert(JSON::Validator.validate({'type' => 'string'}, 'hello'))
|
68
68
|
assert(!JSON::Validator.validate({'type' => 'string'}, 3.14159265358979))
|
69
69
|
assert(!JSON::Validator.validate({'type' => 'string'}, []))
|
70
|
-
|
71
|
-
|
70
|
+
|
71
|
+
|
72
72
|
# Test booleans
|
73
73
|
schema["properties"]["a"]["type"] = "boolean"
|
74
74
|
data["a"] = 5
|
@@ -76,23 +76,23 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
76
76
|
|
77
77
|
data["a"] = 5.2
|
78
78
|
assert(!JSON::Validator.validate(schema,data))
|
79
|
-
|
79
|
+
|
80
80
|
data['a'] = 'string'
|
81
81
|
assert(!JSON::Validator.validate(schema,data))
|
82
|
-
|
82
|
+
|
83
83
|
data['a'] = true
|
84
84
|
assert(JSON::Validator.validate(schema,data))
|
85
|
-
|
85
|
+
|
86
86
|
data['a'] = false
|
87
87
|
assert(JSON::Validator.validate(schema,data))
|
88
|
-
|
88
|
+
|
89
89
|
assert(JSON::Validator.validate({'type' => 'boolean'}, true))
|
90
90
|
assert(JSON::Validator.validate({'type' => 'boolean'}, false))
|
91
91
|
assert(!JSON::Validator.validate({'type' => 'boolean'}, nil))
|
92
92
|
assert(!JSON::Validator.validate({'type' => 'boolean'}, 3))
|
93
93
|
assert(!JSON::Validator.validate({'type' => 'boolean'}, "hello"))
|
94
|
-
|
95
|
-
|
94
|
+
|
95
|
+
|
96
96
|
# Test object
|
97
97
|
schema["properties"]["a"]["type"] = "object"
|
98
98
|
data["a"] = {}
|
@@ -100,20 +100,20 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
100
100
|
|
101
101
|
data["a"] = 5.2
|
102
102
|
assert(!JSON::Validator.validate(schema,data))
|
103
|
-
|
103
|
+
|
104
104
|
data['a'] = 'string'
|
105
105
|
assert(!JSON::Validator.validate(schema,data))
|
106
|
-
|
106
|
+
|
107
107
|
data['a'] = true
|
108
108
|
assert(!JSON::Validator.validate(schema,data))
|
109
|
-
|
109
|
+
|
110
110
|
assert(JSON::Validator.validate({'type' => 'object'}, {'a' => true}))
|
111
111
|
assert(JSON::Validator.validate({'type' => 'object'}, {}))
|
112
112
|
assert(!JSON::Validator.validate({'type' => 'object'}, []))
|
113
113
|
assert(!JSON::Validator.validate({'type' => 'object'}, 3))
|
114
114
|
assert(!JSON::Validator.validate({'type' => 'object'}, "hello"))
|
115
|
-
|
116
|
-
|
115
|
+
|
116
|
+
|
117
117
|
# Test array
|
118
118
|
schema["properties"]["a"]["type"] = "array"
|
119
119
|
data["a"] = []
|
@@ -121,20 +121,20 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
121
121
|
|
122
122
|
data["a"] = 5.2
|
123
123
|
assert(!JSON::Validator.validate(schema,data))
|
124
|
-
|
124
|
+
|
125
125
|
data['a'] = 'string'
|
126
126
|
assert(!JSON::Validator.validate(schema,data))
|
127
|
-
|
127
|
+
|
128
128
|
data['a'] = true
|
129
129
|
assert(!JSON::Validator.validate(schema,data))
|
130
|
-
|
130
|
+
|
131
131
|
assert(JSON::Validator.validate({'type' => 'array'}, ['a']))
|
132
132
|
assert(JSON::Validator.validate({'type' => 'array'}, []))
|
133
133
|
assert(!JSON::Validator.validate({'type' => 'array'}, {}))
|
134
134
|
assert(!JSON::Validator.validate({'type' => 'array'}, 3))
|
135
135
|
assert(!JSON::Validator.validate({'type' => 'array'}, "hello"))
|
136
|
-
|
137
|
-
|
136
|
+
|
137
|
+
|
138
138
|
# Test null
|
139
139
|
schema["properties"]["a"]["type"] = "null"
|
140
140
|
data["a"] = nil
|
@@ -142,19 +142,19 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
142
142
|
|
143
143
|
data["a"] = 5.2
|
144
144
|
assert(!JSON::Validator.validate(schema,data))
|
145
|
-
|
145
|
+
|
146
146
|
data['a'] = 'string'
|
147
147
|
assert(!JSON::Validator.validate(schema,data))
|
148
|
-
|
148
|
+
|
149
149
|
data['a'] = true
|
150
150
|
assert(!JSON::Validator.validate(schema,data))
|
151
|
-
|
151
|
+
|
152
152
|
assert(JSON::Validator.validate({'type' => 'null'}, nil))
|
153
153
|
assert(!JSON::Validator.validate({'type' => 'null'}, false))
|
154
154
|
assert(!JSON::Validator.validate({'type' => 'null'}, []))
|
155
155
|
assert(!JSON::Validator.validate({'type' => 'null'}, "hello"))
|
156
|
-
|
157
|
-
|
156
|
+
|
157
|
+
|
158
158
|
# Test any
|
159
159
|
schema["properties"]["a"]["type"] = "any"
|
160
160
|
data["a"] = 5
|
@@ -162,52 +162,52 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
162
162
|
|
163
163
|
data["a"] = 5.2
|
164
164
|
assert(JSON::Validator.validate(schema,data))
|
165
|
-
|
165
|
+
|
166
166
|
data['a'] = 'string'
|
167
167
|
assert(JSON::Validator.validate(schema,data))
|
168
|
-
|
168
|
+
|
169
169
|
data['a'] = true
|
170
170
|
assert(JSON::Validator.validate(schema,data))
|
171
|
-
|
171
|
+
|
172
172
|
assert(JSON::Validator.validate({'type' => 'any'}, true))
|
173
173
|
assert(JSON::Validator.validate({'type' => 'any'}, nil))
|
174
174
|
assert(JSON::Validator.validate({'type' => 'any'}, {}))
|
175
175
|
assert(JSON::Validator.validate({'type' => 'any'}, 3))
|
176
176
|
assert(JSON::Validator.validate({'type' => 'any'}, "hello"))
|
177
|
-
|
178
|
-
|
177
|
+
|
178
|
+
|
179
179
|
# Test a union type
|
180
180
|
schema["properties"]["a"]["type"] = ["integer","string"]
|
181
181
|
data["a"] = 5
|
182
182
|
assert(JSON::Validator.validate(schema,data))
|
183
|
-
|
183
|
+
|
184
184
|
data["a"] = 'boo'
|
185
185
|
assert(JSON::Validator.validate(schema,data))
|
186
|
-
|
186
|
+
|
187
187
|
data["a"] = false
|
188
188
|
assert(!JSON::Validator.validate(schema,data))
|
189
|
-
|
189
|
+
|
190
190
|
assert(JSON::Validator.validate({'type' => ['string', 'null']}, "hello"))
|
191
191
|
assert(!JSON::Validator.validate({'type' => ['integer', 'object']}, "hello"))
|
192
|
-
|
192
|
+
|
193
193
|
# Test a union type with schemas
|
194
194
|
schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
|
195
|
-
|
195
|
+
|
196
196
|
data["a"] = "test"
|
197
197
|
assert(JSON::Validator.validate(schema,data))
|
198
|
-
|
198
|
+
|
199
199
|
data["a"] = 5
|
200
200
|
assert(!JSON::Validator.validate(schema,data))
|
201
|
-
|
201
|
+
|
202
202
|
data["a"] = {"b" => 5}
|
203
203
|
assert(JSON::Validator.validate(schema,data))
|
204
|
-
|
204
|
+
|
205
205
|
data["a"] = {"b" => "taco"}
|
206
206
|
assert(!JSON::Validator.validate(schema,data))
|
207
207
|
end
|
208
|
-
|
209
|
-
|
210
|
-
|
208
|
+
|
209
|
+
|
210
|
+
|
211
211
|
def test_required
|
212
212
|
# Set up the default datatype
|
213
213
|
schema = {
|
@@ -217,25 +217,25 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
217
217
|
}
|
218
218
|
}
|
219
219
|
data = {}
|
220
|
-
|
220
|
+
|
221
221
|
assert(!JSON::Validator.validate(schema,data))
|
222
222
|
data['a'] = "Hello"
|
223
223
|
assert(JSON::Validator.validate(schema,data))
|
224
|
-
|
224
|
+
|
225
225
|
schema = {
|
226
226
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
227
227
|
"properties" => {
|
228
228
|
"a" => {"type" => "integer"}
|
229
229
|
}
|
230
230
|
}
|
231
|
-
|
231
|
+
|
232
232
|
data = {}
|
233
233
|
assert(JSON::Validator.validate(schema,data))
|
234
|
-
|
234
|
+
|
235
235
|
end
|
236
|
-
|
237
|
-
|
238
|
-
|
236
|
+
|
237
|
+
|
238
|
+
|
239
239
|
def test_minimum
|
240
240
|
# Set up the default datatype
|
241
241
|
schema = {
|
@@ -244,49 +244,49 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
244
244
|
"a" => {"minimum" => 5}
|
245
245
|
}
|
246
246
|
}
|
247
|
-
|
247
|
+
|
248
248
|
data = {
|
249
249
|
"a" => nil
|
250
250
|
}
|
251
|
-
|
252
|
-
|
251
|
+
|
252
|
+
|
253
253
|
# Test an integer
|
254
254
|
data["a"] = 5
|
255
255
|
assert(JSON::Validator.validate(schema,data))
|
256
|
-
|
256
|
+
|
257
257
|
data["a"] = 4
|
258
258
|
assert(!JSON::Validator.validate(schema,data))
|
259
|
-
|
259
|
+
|
260
260
|
# Test a float
|
261
261
|
data["a"] = 5.0
|
262
262
|
assert(JSON::Validator.validate(schema,data))
|
263
|
-
|
263
|
+
|
264
264
|
data["a"] = 4.9
|
265
265
|
assert(!JSON::Validator.validate(schema,data))
|
266
|
-
|
266
|
+
|
267
267
|
# Test a non-number
|
268
268
|
data["a"] = "a string"
|
269
269
|
assert(JSON::Validator.validate(schema,data))
|
270
|
-
|
270
|
+
|
271
271
|
# Test exclusiveMinimum
|
272
272
|
schema["properties"]["a"]["exclusiveMinimum"] = true
|
273
|
-
|
273
|
+
|
274
274
|
data["a"] = 6
|
275
275
|
assert(JSON::Validator.validate(schema,data))
|
276
|
-
|
276
|
+
|
277
277
|
data["a"] = 5
|
278
278
|
assert(!JSON::Validator.validate(schema,data))
|
279
|
-
|
279
|
+
|
280
280
|
# Test with float
|
281
281
|
data["a"] = 5.00000001
|
282
282
|
assert(JSON::Validator.validate(schema,data))
|
283
|
-
|
283
|
+
|
284
284
|
data["a"] = 5.0
|
285
285
|
assert(!JSON::Validator.validate(schema,data))
|
286
286
|
end
|
287
|
-
|
288
|
-
|
289
|
-
|
287
|
+
|
288
|
+
|
289
|
+
|
290
290
|
def test_maximum
|
291
291
|
# Set up the default datatype
|
292
292
|
schema = {
|
@@ -295,48 +295,48 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
295
295
|
"a" => {"maximum" => 5}
|
296
296
|
}
|
297
297
|
}
|
298
|
-
|
298
|
+
|
299
299
|
data = {
|
300
300
|
"a" => nil
|
301
301
|
}
|
302
|
-
|
303
|
-
|
302
|
+
|
303
|
+
|
304
304
|
# Test an integer
|
305
305
|
data["a"] = 5
|
306
306
|
assert(JSON::Validator.validate(schema,data))
|
307
|
-
|
307
|
+
|
308
308
|
data["a"] = 6
|
309
309
|
assert(!JSON::Validator.validate(schema,data))
|
310
|
-
|
310
|
+
|
311
311
|
# Test a float
|
312
312
|
data["a"] = 5.0
|
313
313
|
assert(JSON::Validator.validate(schema,data))
|
314
|
-
|
314
|
+
|
315
315
|
data["a"] = 5.1
|
316
316
|
assert(!JSON::Validator.validate(schema,data))
|
317
|
-
|
317
|
+
|
318
318
|
# Test a non-number
|
319
319
|
data["a"] = "a string"
|
320
320
|
assert(JSON::Validator.validate(schema,data))
|
321
|
-
|
321
|
+
|
322
322
|
# Test exclusiveMinimum
|
323
323
|
schema["properties"]["a"]["exclusiveMaximum"] = true
|
324
|
-
|
324
|
+
|
325
325
|
data["a"] = 4
|
326
326
|
assert(JSON::Validator.validate(schema,data))
|
327
|
-
|
327
|
+
|
328
328
|
data["a"] = 5
|
329
329
|
assert(!JSON::Validator.validate(schema,data))
|
330
|
-
|
330
|
+
|
331
331
|
# Test with float
|
332
332
|
data["a"] = 4.9999999
|
333
333
|
assert(JSON::Validator.validate(schema,data))
|
334
|
-
|
334
|
+
|
335
335
|
data["a"] = 5.0
|
336
336
|
assert(!JSON::Validator.validate(schema,data))
|
337
337
|
end
|
338
|
-
|
339
|
-
|
338
|
+
|
339
|
+
|
340
340
|
def test_min_items
|
341
341
|
# Set up the default datatype
|
342
342
|
schema = {
|
@@ -345,25 +345,25 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
345
345
|
"a" => {"minItems" => 1}
|
346
346
|
}
|
347
347
|
}
|
348
|
-
|
348
|
+
|
349
349
|
data = {
|
350
350
|
"a" => nil
|
351
351
|
}
|
352
|
-
|
352
|
+
|
353
353
|
# Test with an array
|
354
354
|
data["a"] = ["boo"]
|
355
355
|
assert(JSON::Validator.validate(schema,data))
|
356
|
-
|
356
|
+
|
357
357
|
data["a"] = []
|
358
358
|
assert(!JSON::Validator.validate(schema,data))
|
359
|
-
|
359
|
+
|
360
360
|
# Test with a non-array
|
361
361
|
data["a"] = "boo"
|
362
362
|
assert(JSON::Validator.validate(schema,data))
|
363
363
|
end
|
364
|
-
|
365
|
-
|
366
|
-
|
364
|
+
|
365
|
+
|
366
|
+
|
367
367
|
def test_max_items
|
368
368
|
# Set up the default datatype
|
369
369
|
schema = {
|
@@ -372,25 +372,25 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
372
372
|
"a" => {"maxItems" => 1}
|
373
373
|
}
|
374
374
|
}
|
375
|
-
|
375
|
+
|
376
376
|
data = {
|
377
377
|
"a" => nil
|
378
378
|
}
|
379
|
-
|
379
|
+
|
380
380
|
# Test with an array
|
381
381
|
data["a"] = ["boo"]
|
382
382
|
assert(JSON::Validator.validate(schema,data))
|
383
|
-
|
383
|
+
|
384
384
|
data["a"] = ["boo","taco"]
|
385
385
|
assert(!JSON::Validator.validate(schema,data))
|
386
|
-
|
386
|
+
|
387
387
|
# Test with a non-array
|
388
388
|
data["a"] = "boo"
|
389
389
|
assert(JSON::Validator.validate(schema,data))
|
390
390
|
end
|
391
|
-
|
392
|
-
|
393
|
-
|
391
|
+
|
392
|
+
|
393
|
+
|
394
394
|
def test_unique_items
|
395
395
|
# Set up the default datatype
|
396
396
|
schema = {
|
@@ -399,70 +399,117 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
399
399
|
"a" => {"uniqueItems" => true}
|
400
400
|
}
|
401
401
|
}
|
402
|
-
|
402
|
+
|
403
403
|
data = {
|
404
404
|
"a" => nil
|
405
405
|
}
|
406
|
-
|
406
|
+
|
407
407
|
# Test with nulls
|
408
408
|
data["a"] = [nil,5]
|
409
409
|
assert(JSON::Validator.validate(schema,data))
|
410
|
-
|
410
|
+
|
411
411
|
data["a"] = [nil,nil]
|
412
412
|
assert(!JSON::Validator.validate(schema,data))
|
413
|
-
|
413
|
+
|
414
414
|
# Test with booleans
|
415
415
|
data["a"] = [true,4]
|
416
416
|
assert(JSON::Validator.validate(schema,data))
|
417
|
-
|
417
|
+
|
418
418
|
data["a"] = [true,false]
|
419
419
|
assert(JSON::Validator.validate(schema,data))
|
420
|
-
|
420
|
+
|
421
421
|
data["a"] = [true,true]
|
422
422
|
assert(!JSON::Validator.validate(schema,data))
|
423
|
-
|
423
|
+
|
424
424
|
# Test with numbers
|
425
425
|
data["a"] = [4,true]
|
426
426
|
assert(JSON::Validator.validate(schema,data))
|
427
|
-
|
427
|
+
|
428
428
|
data["a"] = [4,4.1]
|
429
429
|
assert(JSON::Validator.validate(schema,data))
|
430
|
-
|
430
|
+
|
431
431
|
data["a"] = [4,4]
|
432
432
|
assert(!JSON::Validator.validate(schema,data))
|
433
|
-
|
433
|
+
|
434
434
|
# Test with strings
|
435
435
|
data["a"] = ['a',true]
|
436
436
|
assert(JSON::Validator.validate(schema,data))
|
437
|
-
|
437
|
+
|
438
438
|
data["a"] = ['a','ab']
|
439
439
|
assert(JSON::Validator.validate(schema,data))
|
440
|
-
|
440
|
+
|
441
441
|
data["a"] = ['a','a']
|
442
442
|
assert(!JSON::Validator.validate(schema,data))
|
443
|
-
|
443
|
+
|
444
444
|
# Test with arrays
|
445
445
|
data["a"] = [[1],true]
|
446
446
|
assert(JSON::Validator.validate(schema,data))
|
447
|
-
|
447
|
+
|
448
448
|
data["a"] = [[1,2],[1,3]]
|
449
449
|
assert(JSON::Validator.validate(schema,data))
|
450
|
-
|
450
|
+
|
451
451
|
data["a"] = [[1,2,3],[1,2,3]]
|
452
452
|
assert(!JSON::Validator.validate(schema,data))
|
453
|
-
|
453
|
+
|
454
454
|
# Test with objects
|
455
455
|
data["a"] = [{"a" => 1},true]
|
456
456
|
assert(JSON::Validator.validate(schema,data))
|
457
|
-
|
457
|
+
|
458
458
|
data["a"] = [{"a" => 1},{"a" => 2}]
|
459
459
|
assert(JSON::Validator.validate(schema,data))
|
460
|
-
|
460
|
+
|
461
461
|
data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}]
|
462
462
|
assert(!JSON::Validator.validate(schema,data))
|
463
463
|
end
|
464
|
-
|
465
|
-
|
464
|
+
|
465
|
+
def test_strict_properties
|
466
|
+
schema = {
|
467
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
468
|
+
"properties" => {
|
469
|
+
"a" => {"type" => "string"},
|
470
|
+
"b" => {"type" => "string"}
|
471
|
+
}
|
472
|
+
}
|
473
|
+
|
474
|
+
data = {"a" => "a"}
|
475
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
476
|
+
|
477
|
+
data = {"b" => "b"}
|
478
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
479
|
+
|
480
|
+
data = {"a" => "a", "b" => "b"}
|
481
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
482
|
+
|
483
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
484
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_strict_properties_additional_props
|
488
|
+
schema = {
|
489
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
490
|
+
"properties" => {
|
491
|
+
"a" => {"type" => "string"},
|
492
|
+
"b" => {"type" => "string"}
|
493
|
+
},
|
494
|
+
"additionalProperties" => {"type" => "integer"}
|
495
|
+
}
|
496
|
+
|
497
|
+
data = {"a" => "a"}
|
498
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
499
|
+
|
500
|
+
data = {"b" => "b"}
|
501
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
502
|
+
|
503
|
+
data = {"a" => "a", "b" => "b"}
|
504
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
505
|
+
|
506
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
507
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
508
|
+
|
509
|
+
data = {"a" => "a", "b" => "b", "c" => 3}
|
510
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
511
|
+
end
|
512
|
+
|
466
513
|
def test_pattern
|
467
514
|
# Set up the default datatype
|
468
515
|
schema = {
|
@@ -471,24 +518,24 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
471
518
|
"a" => {"pattern" => "\\d+ taco"}
|
472
519
|
}
|
473
520
|
}
|
474
|
-
|
521
|
+
|
475
522
|
data = {
|
476
523
|
"a" => nil
|
477
524
|
}
|
478
|
-
|
525
|
+
|
479
526
|
# Test strings
|
480
527
|
data["a"] = "156 taco bell"
|
481
528
|
assert(JSON::Validator.validate(schema,data))
|
482
|
-
|
529
|
+
|
483
530
|
# Test a non-string
|
484
531
|
data["a"] = 5
|
485
532
|
assert(JSON::Validator.validate(schema,data))
|
486
|
-
|
533
|
+
|
487
534
|
data["a"] = "taco"
|
488
535
|
assert(!JSON::Validator.validate(schema,data))
|
489
536
|
end
|
490
|
-
|
491
|
-
|
537
|
+
|
538
|
+
|
492
539
|
def test_min_length
|
493
540
|
# Set up the default datatype
|
494
541
|
schema = {
|
@@ -497,24 +544,24 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
497
544
|
"a" => {"minLength" => 1}
|
498
545
|
}
|
499
546
|
}
|
500
|
-
|
547
|
+
|
501
548
|
data = {
|
502
549
|
"a" => nil
|
503
550
|
}
|
504
|
-
|
551
|
+
|
505
552
|
# Try out strings
|
506
553
|
data["a"] = "t"
|
507
554
|
assert(JSON::Validator.validate(schema,data))
|
508
|
-
|
555
|
+
|
509
556
|
data["a"] = ""
|
510
557
|
assert(!JSON::Validator.validate(schema,data))
|
511
|
-
|
558
|
+
|
512
559
|
# Try out non-string
|
513
560
|
data["a"] = 5
|
514
561
|
assert(JSON::Validator.validate(schema,data))
|
515
562
|
end
|
516
|
-
|
517
|
-
|
563
|
+
|
564
|
+
|
518
565
|
def test_max_length
|
519
566
|
# Set up the default datatype
|
520
567
|
schema = {
|
@@ -523,24 +570,24 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
523
570
|
"a" => {"maxLength" => 1}
|
524
571
|
}
|
525
572
|
}
|
526
|
-
|
573
|
+
|
527
574
|
data = {
|
528
575
|
"a" => nil
|
529
576
|
}
|
530
|
-
|
577
|
+
|
531
578
|
# Try out strings
|
532
579
|
data["a"] = "t"
|
533
580
|
assert(JSON::Validator.validate(schema,data))
|
534
|
-
|
581
|
+
|
535
582
|
data["a"] = "tt"
|
536
583
|
assert(!JSON::Validator.validate(schema,data))
|
537
|
-
|
584
|
+
|
538
585
|
# Try out non-string
|
539
586
|
data["a"] = 5
|
540
587
|
assert(JSON::Validator.validate(schema,data))
|
541
588
|
end
|
542
|
-
|
543
|
-
|
589
|
+
|
590
|
+
|
544
591
|
def test_enum
|
545
592
|
# Set up the default datatype
|
546
593
|
schema = {
|
@@ -549,34 +596,34 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
549
596
|
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
|
550
597
|
}
|
551
598
|
}
|
552
|
-
|
599
|
+
|
553
600
|
data = {
|
554
601
|
"a" => nil
|
555
602
|
}
|
556
|
-
|
603
|
+
|
557
604
|
# Make sure all of the above are valid...
|
558
605
|
data["a"] = 1
|
559
606
|
assert(JSON::Validator.validate(schema,data))
|
560
|
-
|
607
|
+
|
561
608
|
data["a"] = 'boo'
|
562
609
|
assert(JSON::Validator.validate(schema,data))
|
563
|
-
|
610
|
+
|
564
611
|
data["a"] = [1,2,3]
|
565
612
|
assert(JSON::Validator.validate(schema,data))
|
566
|
-
|
613
|
+
|
567
614
|
data["a"] = {"a" => "b"}
|
568
615
|
assert(JSON::Validator.validate(schema,data))
|
569
|
-
|
616
|
+
|
570
617
|
# Test something that doesn't exist
|
571
618
|
data["a"] = 'taco'
|
572
619
|
assert(!JSON::Validator.validate(schema,data))
|
573
|
-
|
620
|
+
|
574
621
|
# Try it without the key
|
575
622
|
data = {}
|
576
623
|
assert(JSON::Validator.validate(schema,data))
|
577
624
|
end
|
578
|
-
|
579
|
-
|
625
|
+
|
626
|
+
|
580
627
|
def test_divisible_by
|
581
628
|
# Set up the default datatype
|
582
629
|
schema = {
|
@@ -585,32 +632,32 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
585
632
|
"a" => {"divisibleBy" => 1.1}
|
586
633
|
}
|
587
634
|
}
|
588
|
-
|
635
|
+
|
589
636
|
data = {
|
590
637
|
"a" => nil
|
591
638
|
}
|
592
|
-
|
639
|
+
|
593
640
|
data["a"] = 3.3
|
594
641
|
assert(JSON::Validator.validate(schema,data))
|
595
|
-
|
642
|
+
|
596
643
|
data["a"] = 3.4
|
597
644
|
assert(!JSON::Validator.validate(schema,data))
|
598
|
-
|
645
|
+
|
599
646
|
schema["properties"]["a"]["divisibleBy"] = 2.0
|
600
|
-
|
647
|
+
|
601
648
|
data["a"] = 4.0
|
602
649
|
assert(JSON::Validator.validate(schema,data))
|
603
|
-
|
650
|
+
|
604
651
|
data["a"] = 'boo'
|
605
652
|
assert(JSON::Validator.validate(schema,data))
|
606
|
-
|
653
|
+
|
607
654
|
data["a"] = 5
|
608
655
|
schema["properties"]["a"]["divisibleBy"] = 0
|
609
656
|
assert(!JSON::Validator.validate(schema,data))
|
610
657
|
end
|
611
|
-
|
612
|
-
|
613
|
-
|
658
|
+
|
659
|
+
|
660
|
+
|
614
661
|
def test_disallow
|
615
662
|
# Set up the default datatype
|
616
663
|
schema = {
|
@@ -619,60 +666,60 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
619
666
|
"a" => {"disallow" => "integer"}
|
620
667
|
}
|
621
668
|
}
|
622
|
-
|
669
|
+
|
623
670
|
data = {
|
624
671
|
"a" => nil
|
625
672
|
}
|
626
|
-
|
627
|
-
|
673
|
+
|
674
|
+
|
628
675
|
data["a"] = 'string'
|
629
676
|
assert(JSON::Validator.validate(schema,data))
|
630
|
-
|
677
|
+
|
631
678
|
data["a"] = 5
|
632
679
|
assert(!JSON::Validator.validate(schema,data))
|
633
|
-
|
634
|
-
|
680
|
+
|
681
|
+
|
635
682
|
schema["properties"]["a"]["disallow"] = ["integer","string"]
|
636
683
|
data["a"] = 'string'
|
637
684
|
assert(!JSON::Validator.validate(schema,data))
|
638
|
-
|
685
|
+
|
639
686
|
data["a"] = 5
|
640
687
|
assert(!JSON::Validator.validate(schema,data))
|
641
|
-
|
688
|
+
|
642
689
|
data["a"] = false
|
643
690
|
assert(JSON::Validator.validate(schema,data))
|
644
691
|
|
645
692
|
end
|
646
|
-
|
647
|
-
|
648
|
-
|
693
|
+
|
694
|
+
|
695
|
+
|
649
696
|
def test_extends
|
650
697
|
schema = {
|
651
698
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
652
699
|
"properties" => {
|
653
700
|
"a" => { "type" => "integer"}
|
654
|
-
}
|
701
|
+
}
|
655
702
|
}
|
656
|
-
|
703
|
+
|
657
704
|
schema2 = {
|
658
705
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
659
706
|
"properties" => {
|
660
707
|
"a" => { "maximum" => 5 }
|
661
708
|
}
|
662
709
|
}
|
663
|
-
|
710
|
+
|
664
711
|
data = {
|
665
712
|
"a" => 10
|
666
713
|
}
|
667
|
-
|
714
|
+
|
668
715
|
assert(JSON::Validator.validate(schema,data))
|
669
716
|
assert(!JSON::Validator.validate(schema2,data))
|
670
|
-
|
717
|
+
|
671
718
|
schema["extends"] = schema2
|
672
|
-
|
719
|
+
|
673
720
|
assert(!JSON::Validator.validate(schema,data))
|
674
721
|
end
|
675
|
-
|
722
|
+
|
676
723
|
def test_pattern_properties
|
677
724
|
# Set up the default datatype
|
678
725
|
schema = {
|
@@ -681,19 +728,19 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
681
728
|
"\\d+ taco" => {"type" => "integer"}
|
682
729
|
}
|
683
730
|
}
|
684
|
-
|
731
|
+
|
685
732
|
data = {
|
686
733
|
"a" => true,
|
687
734
|
"1 taco" => 1,
|
688
735
|
"20 tacos" => 20
|
689
736
|
}
|
690
|
-
|
737
|
+
|
691
738
|
assert(JSON::Validator.validate(schema,data))
|
692
739
|
data["20 tacos"] = "string!"
|
693
740
|
assert(!JSON::Validator.validate(schema,data))
|
694
741
|
end
|
695
|
-
|
696
|
-
|
742
|
+
|
743
|
+
|
697
744
|
def test_additional_properties
|
698
745
|
# Test no additional properties allowed
|
699
746
|
schema = {
|
@@ -703,22 +750,22 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
703
750
|
},
|
704
751
|
"additionalProperties" => false
|
705
752
|
}
|
706
|
-
|
753
|
+
|
707
754
|
data = {
|
708
755
|
"a" => 10
|
709
756
|
}
|
710
|
-
|
757
|
+
|
711
758
|
assert(JSON::Validator.validate(schema,data))
|
712
759
|
data["b"] = 5
|
713
760
|
assert(!JSON::Validator.validate(schema,data))
|
714
|
-
|
761
|
+
|
715
762
|
# Test additional properties match a schema
|
716
763
|
schema["additionalProperties"] = { "type" => "string" }
|
717
764
|
data["b"] = "taco"
|
718
765
|
assert(JSON::Validator.validate(schema,data))
|
719
766
|
data["b"] = 5
|
720
767
|
assert(!JSON::Validator.validate(schema,data))
|
721
|
-
|
768
|
+
|
722
769
|
# Make sure this works with pattern properties set, too
|
723
770
|
schema = {
|
724
771
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
@@ -727,29 +774,29 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
727
774
|
},
|
728
775
|
"additionalProperties" => false
|
729
776
|
}
|
730
|
-
|
777
|
+
|
731
778
|
data = {
|
732
779
|
"5 tacos" => 5,
|
733
780
|
"20 tacos" => 20
|
734
781
|
}
|
735
|
-
|
782
|
+
|
736
783
|
assert(JSON::Validator.validate(schema,data))
|
737
784
|
data["b"] = 5
|
738
785
|
assert(!JSON::Validator.validate(schema,data))
|
739
786
|
end
|
740
|
-
|
741
|
-
|
787
|
+
|
788
|
+
|
742
789
|
def test_items
|
743
790
|
schema = {
|
744
791
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
745
792
|
"items" => { "type" => "integer" }
|
746
793
|
}
|
747
|
-
|
794
|
+
|
748
795
|
data = [1,2,4]
|
749
796
|
assert(JSON::Validator.validate(schema,data))
|
750
797
|
data = [1,2,"string"]
|
751
798
|
assert(!JSON::Validator.validate(schema,data))
|
752
|
-
|
799
|
+
|
753
800
|
schema = {
|
754
801
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
755
802
|
"items" => [
|
@@ -757,14 +804,14 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
757
804
|
{"type" => "string"}
|
758
805
|
]
|
759
806
|
}
|
760
|
-
|
807
|
+
|
761
808
|
data = [1,"string"]
|
762
809
|
assert(JSON::Validator.validate(schema,data))
|
763
810
|
data = [1,"string",3]
|
764
811
|
assert(JSON::Validator.validate(schema,data))
|
765
812
|
data = ["string",1]
|
766
813
|
assert(!JSON::Validator.validate(schema,data))
|
767
|
-
|
814
|
+
|
768
815
|
schema = {
|
769
816
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
770
817
|
"items" => [
|
@@ -773,14 +820,14 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
773
820
|
],
|
774
821
|
"additionalItems" => false
|
775
822
|
}
|
776
|
-
|
823
|
+
|
777
824
|
data = [1,"string"]
|
778
825
|
assert(JSON::Validator.validate(schema,data))
|
779
826
|
data = [1,"string",3]
|
780
827
|
assert(!JSON::Validator.validate(schema,data))
|
781
|
-
|
828
|
+
|
782
829
|
schema = {"$schema" => "http://json-schema.org/draft-03/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
|
783
|
-
|
830
|
+
|
784
831
|
data = [1,"string"]
|
785
832
|
assert(JSON::Validator.validate(schema,data))
|
786
833
|
data = [1,"string",3]
|
@@ -788,48 +835,48 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
788
835
|
data = [1,"string","string"]
|
789
836
|
assert(!JSON::Validator.validate(schema,data))
|
790
837
|
end
|
791
|
-
|
792
|
-
|
838
|
+
|
839
|
+
|
793
840
|
def test_list_option
|
794
841
|
schema = {
|
795
842
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
796
843
|
"type" => "object",
|
797
844
|
"properties" => { "a" => {"type" => "integer", "required" => true} }
|
798
845
|
}
|
799
|
-
|
846
|
+
|
800
847
|
data = [{"a" => 1},{"a" => 2},{"a" => 3}]
|
801
848
|
assert(JSON::Validator.validate(schema,data,:list => true))
|
802
|
-
assert(!JSON::Validator.validate(schema,data))
|
803
|
-
|
849
|
+
assert(!JSON::Validator.validate(schema,data))
|
850
|
+
|
804
851
|
data = {"a" => 1}
|
805
852
|
assert(!JSON::Validator.validate(schema,data,:list => true))
|
806
|
-
|
853
|
+
|
807
854
|
data = [{"a" => 1},{"b" => 2},{"a" => 3}]
|
808
855
|
assert(!JSON::Validator.validate(schema,data,:list => true))
|
809
856
|
end
|
810
|
-
|
811
|
-
|
857
|
+
|
858
|
+
|
812
859
|
def test_self_reference
|
813
860
|
schema = {
|
814
861
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
815
862
|
"type" => "object",
|
816
863
|
"properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
|
817
864
|
}
|
818
|
-
|
865
|
+
|
819
866
|
data = {"a" => 5, "b" => {"b" => {"a" => 1}}}
|
820
867
|
assert(JSON::Validator.validate(schema,data))
|
821
868
|
data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
|
822
869
|
assert(!JSON::Validator.validate(schema,data))
|
823
870
|
end
|
824
|
-
|
825
|
-
|
871
|
+
|
872
|
+
|
826
873
|
def test_format_ipv4
|
827
874
|
schema = {
|
828
875
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
829
876
|
"type" => "object",
|
830
877
|
"properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
|
831
878
|
}
|
832
|
-
|
879
|
+
|
833
880
|
data = {"a" => "1.1.1.1"}
|
834
881
|
assert(JSON::Validator.validate(schema,data))
|
835
882
|
data = {"a" => "1.1.1"}
|
@@ -844,15 +891,15 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
844
891
|
assert(!JSON::Validator.validate(schema,data))
|
845
892
|
data = {"a" => "b1.1.1.1"}
|
846
893
|
end
|
847
|
-
|
848
|
-
|
894
|
+
|
895
|
+
|
849
896
|
def test_format_ipv6
|
850
897
|
schema = {
|
851
898
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
852
899
|
"type" => "object",
|
853
900
|
"properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
|
854
901
|
}
|
855
|
-
|
902
|
+
|
856
903
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
|
857
904
|
assert(JSON::Validator.validate(schema,data))
|
858
905
|
data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
|
@@ -868,14 +915,14 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
868
915
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
869
916
|
assert(!JSON::Validator.validate(schema,data))
|
870
917
|
end
|
871
|
-
|
918
|
+
|
872
919
|
def test_format_time
|
873
920
|
schema = {
|
874
921
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
875
922
|
"type" => "object",
|
876
923
|
"properties" => { "a" => {"type" => "string", "format" => "time"}}
|
877
924
|
}
|
878
|
-
|
925
|
+
|
879
926
|
data = {"a" => "12:00:00"}
|
880
927
|
assert(JSON::Validator.validate(schema,data))
|
881
928
|
data = {"a" => "12:00"}
|
@@ -893,15 +940,15 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
893
940
|
data = {"a" => "12:00:00b"}
|
894
941
|
assert(!JSON::Validator.validate(schema,data))
|
895
942
|
end
|
896
|
-
|
897
|
-
|
943
|
+
|
944
|
+
|
898
945
|
def test_format_date
|
899
946
|
schema = {
|
900
947
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
901
948
|
"type" => "object",
|
902
949
|
"properties" => { "a" => {"type" => "string", "format" => "date"}}
|
903
950
|
}
|
904
|
-
|
951
|
+
|
905
952
|
data = {"a" => "2010-01-01"}
|
906
953
|
assert(JSON::Validator.validate(schema,data))
|
907
954
|
data = {"a" => "2010-01-32"}
|
@@ -915,20 +962,20 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
915
962
|
data = {"a" => "2010-01-01n"}
|
916
963
|
assert(!JSON::Validator.validate(schema,data))
|
917
964
|
end
|
918
|
-
|
965
|
+
|
919
966
|
def test_format_datetime
|
920
967
|
schema = {
|
921
968
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
922
969
|
"type" => "object",
|
923
970
|
"properties" => { "a" => {"type" => "string", "format" => "date-time"}}
|
924
971
|
}
|
925
|
-
|
972
|
+
|
926
973
|
data = {"a" => "2010-01-01T12:00:00Z"}
|
927
974
|
assert(JSON::Validator.validate(schema,data))
|
928
975
|
data = {"a" => "2010-01-01T12:00:00.1Z"}
|
929
976
|
assert(JSON::Validator.validate(schema,data))
|
930
977
|
data = {"a" => "2010-01-01T12:00:00,1Z"}
|
931
|
-
assert(JSON::Validator.validate(schema,data))
|
978
|
+
assert(JSON::Validator.validate(schema,data))
|
932
979
|
data = {"a" => "2010-01-32T12:00:00Z"}
|
933
980
|
assert(!JSON::Validator.validate(schema,data))
|
934
981
|
data = {"a" => "2010-13-01T12:00:00Z"}
|
@@ -946,12 +993,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
946
993
|
data = {"a" => "2010-01-0112:00:00Z"}
|
947
994
|
assert(!JSON::Validator.validate(schema,data))
|
948
995
|
end
|
949
|
-
|
950
|
-
|
996
|
+
|
997
|
+
|
951
998
|
def test_format_union
|
952
999
|
data1 = {"a" => "boo"}
|
953
1000
|
data2 = {"a" => nil}
|
954
|
-
|
1001
|
+
|
955
1002
|
schema = {
|
956
1003
|
"type" => "object",
|
957
1004
|
"properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
|
@@ -959,25 +1006,25 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
959
1006
|
assert(!JSON::Validator.validate(schema,data1,:version => :draft3))
|
960
1007
|
assert(JSON::Validator.validate(schema,data2,:version => :draft3))
|
961
1008
|
end
|
962
|
-
|
963
|
-
|
964
|
-
|
1009
|
+
|
1010
|
+
|
1011
|
+
|
965
1012
|
def test_schema
|
966
1013
|
schema = {
|
967
1014
|
"$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
|
968
1015
|
"type" => "object"
|
969
1016
|
}
|
970
|
-
|
1017
|
+
|
971
1018
|
data = {"a" => "taco"}
|
972
1019
|
assert(!JSON::Validator.validate(schema,data))
|
973
|
-
|
1020
|
+
|
974
1021
|
schema = {
|
975
1022
|
"$schema" => "http://json-schema.org/draft-03/schema#",
|
976
1023
|
"type" => "object"
|
977
1024
|
}
|
978
1025
|
assert(JSON::Validator.validate(schema,data))
|
979
1026
|
end
|
980
|
-
|
1027
|
+
|
981
1028
|
def test_dependency
|
982
1029
|
schema = {
|
983
1030
|
"type" => "object",
|
@@ -989,12 +1036,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
989
1036
|
"a" => "b"
|
990
1037
|
}
|
991
1038
|
}
|
992
|
-
|
1039
|
+
|
993
1040
|
data = {"a" => 1, "b" => 2}
|
994
1041
|
assert(JSON::Validator.validate(schema,data))
|
995
1042
|
data = {"a" => 1}
|
996
1043
|
assert(!JSON::Validator.validate(schema,data))
|
997
|
-
|
1044
|
+
|
998
1045
|
schema = {
|
999
1046
|
"type" => "object",
|
1000
1047
|
"properties" => {
|
@@ -1006,7 +1053,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1006
1053
|
"a" => ["b","c"]
|
1007
1054
|
}
|
1008
1055
|
}
|
1009
|
-
|
1056
|
+
|
1010
1057
|
data = {"a" => 1, "c" => 2}
|
1011
1058
|
assert(!JSON::Validator.validate(schema,data))
|
1012
1059
|
data = {"a" => 1, "b" => 2, "c" => 3}
|
@@ -1021,7 +1068,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1021
1068
|
"b" => {"type" => "integer"}
|
1022
1069
|
}
|
1023
1070
|
}
|
1024
|
-
|
1071
|
+
|
1025
1072
|
data = {"b" => 2}
|
1026
1073
|
assert(JSON::Validator.validate(schema,data))
|
1027
1074
|
assert_nil(data["a"])
|
@@ -1035,7 +1082,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1035
1082
|
"b" => {"type" => "integer"}
|
1036
1083
|
}
|
1037
1084
|
}
|
1038
|
-
|
1085
|
+
|
1039
1086
|
data = {"b" => 2}
|
1040
1087
|
assert(!JSON::Validator.validate(schema,data))
|
1041
1088
|
assert_nil(data["a"])
|
@@ -1049,7 +1096,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1049
1096
|
"b" => {"type" => "integer"}
|
1050
1097
|
}
|
1051
1098
|
}
|
1052
|
-
|
1099
|
+
|
1053
1100
|
|
1054
1101
|
schema = {
|
1055
1102
|
"type" => "object",
|
@@ -1058,7 +1105,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1058
1105
|
"b" => {"type" => "integer"}
|
1059
1106
|
}
|
1060
1107
|
}
|
1061
|
-
|
1108
|
+
|
1062
1109
|
data = {"b" => 2}
|
1063
1110
|
assert(!JSON::Validator.validate(schema,data))
|
1064
1111
|
assert_nil(data["a"])
|
@@ -1072,7 +1119,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1072
1119
|
"b" => {"type" => "integer"}
|
1073
1120
|
}
|
1074
1121
|
}
|
1075
|
-
|
1122
|
+
|
1076
1123
|
data = {"b" => 2}
|
1077
1124
|
assert(JSON::Validator.validate(schema,data))
|
1078
1125
|
assert_nil(data["a"])
|
@@ -1080,7 +1127,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1080
1127
|
assert_equal("42",data["a"])
|
1081
1128
|
|
1082
1129
|
end
|
1083
|
-
|
1084
|
-
|
1130
|
+
|
1131
|
+
|
1085
1132
|
end
|
1086
1133
|
|