json-schema 2.1.9 → 2.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 +13 -1
- data/lib/json-schema/attributes/additionalproperties.rb +1 -1
- data/lib/json-schema/attributes/properties.rb +14 -1
- data/lib/json-schema/attributes/properties_v4.rb +17 -0
- data/lib/json-schema/validator.rb +3 -1
- data/test/test_jsonschema_draft3.rb +49 -0
- data/test/test_jsonschema_draft4.rb +48 -0
- metadata +5 -7
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NmM0OTNmZTRkM2JmNmNlYWM2MDA0ZjNlZmFlMzZkM2JjOWRmZGQ1ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTdmODg5Njc3NmRjZDliZGU1YjYxMGQwNWQ1ZGY4NDFhMWEyZjcxZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MjUxNmNmNWUzZWQ1MzgyMDk1YjZiNTkyODM1NzFiNmU2MWI0NDMxY2NkN2Rj
|
10
|
+
M2VhNDBkODlkZDg0MGEyMDZhMWFmZWU2MzIwYTBjNjliYWVkM2Y5YTZhNGM2
|
11
|
+
Y2Y5ZjgzNzJmYzhkZjhlMWI0ODljMzA3Mjk5N2JkNjlhZmIzNWQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MzQ3NWRlNzJhZWY5NWFjZWRmN2U4N2MxMTlkMjgyNDU3OWMxMWFjOWY2MDk5
|
14
|
+
Njk3YjZjZGQyNTk2MjFhZjczZTRkZDgwZmQ3MWFlZDM3YTVkMTAyYmNlMWI1
|
15
|
+
ZWQ4YTkyZDRlNzY5NGYzMDY3MmZkY2ExYmRiMzdkY2U4MzFjNDU=
|
data/README.textile
CHANGED
@@ -22,7 +22,7 @@ From the git repo:
|
|
22
22
|
|
23
23
|
<pre>
|
24
24
|
$ gem build json-schema.gemspec
|
25
|
-
$ gem install json-schema-2.
|
25
|
+
$ gem install json-schema-2.2.0.gem
|
26
26
|
</pre>
|
27
27
|
|
28
28
|
|
@@ -74,6 +74,18 @@ data = ['user','user','user']
|
|
74
74
|
JSON::Validator.validate('user.json', data, :list => true)
|
75
75
|
</pre>
|
76
76
|
|
77
|
+
h3. Strictly validate an object's properties
|
78
|
+
|
79
|
+
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.
|
80
|
+
|
81
|
+
<pre>
|
82
|
+
require 'rubygems'
|
83
|
+
require 'json-schema'
|
84
|
+
|
85
|
+
data = ['user','user','user']
|
86
|
+
JSON::Validator.validate('user.json', data, :list => true)
|
87
|
+
</pre>
|
88
|
+
|
77
89
|
h3. Catch a validation error and print it out
|
78
90
|
|
79
91
|
<pre>
|
@@ -15,7 +15,7 @@ module JSON
|
|
15
15
|
|
16
16
|
addprop = current_schema.schema['additionalProperties']
|
17
17
|
if addprop.is_a?(Hash)
|
18
|
-
matching_properties= extra_properties # & addprop.keys
|
18
|
+
matching_properties = extra_properties # & addprop.keys
|
19
19
|
matching_properties.each do |key|
|
20
20
|
schema = JSON::Schema.new(addprop[key] || addprop, current_schema.uri, validator)
|
21
21
|
fragments << key
|
@@ -13,7 +13,8 @@ module JSON
|
|
13
13
|
data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
|
17
|
+
if (property_schema['required'] || options[:strict] == true) && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym)
|
17
18
|
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
|
18
19
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
19
20
|
end
|
@@ -25,6 +26,18 @@ module JSON
|
|
25
26
|
fragments.pop
|
26
27
|
end
|
27
28
|
end
|
29
|
+
|
30
|
+
# When strict is true, ensure no undefined properties exist in the data
|
31
|
+
if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
|
32
|
+
diff = data.select do |k,v|
|
33
|
+
!current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
|
34
|
+
end
|
35
|
+
|
36
|
+
if diff.size > 0
|
37
|
+
message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{diff.keys.join(", ")}'"
|
38
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
39
|
+
end
|
40
|
+
end
|
28
41
|
end
|
29
42
|
end
|
30
43
|
end
|
@@ -13,6 +13,11 @@ module JSON
|
|
13
13
|
data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
|
14
14
|
end
|
15
15
|
|
16
|
+
if (options[:strict] == true && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym))
|
17
|
+
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
|
18
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
19
|
+
end
|
20
|
+
|
16
21
|
if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
|
17
22
|
schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
|
18
23
|
fragments << property.to_s
|
@@ -21,6 +26,18 @@ module JSON
|
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|
29
|
+
|
30
|
+
# When strict is true, ensure no undefined properties exist in the data
|
31
|
+
if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
|
32
|
+
diff = data.select do |k,v|
|
33
|
+
!current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
|
34
|
+
end
|
35
|
+
|
36
|
+
if diff.size > 0
|
37
|
+
message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{diff.keys.join(", ")}'"
|
38
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
39
|
+
end
|
40
|
+
end
|
24
41
|
end
|
25
42
|
end
|
26
43
|
end
|
@@ -113,7 +113,8 @@ module JSON
|
|
113
113
|
:record_errors => false,
|
114
114
|
:errors_as_objects => false,
|
115
115
|
:insert_defaults => false,
|
116
|
-
:clear_cache => true
|
116
|
+
:clear_cache => true,
|
117
|
+
:strict => false
|
117
118
|
}
|
118
119
|
@@validators = {}
|
119
120
|
@@default_validator = nil
|
@@ -158,6 +159,7 @@ module JSON
|
|
158
159
|
|
159
160
|
@validation_options = @options[:record_errors] ? {:record_errors => true} : {}
|
160
161
|
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
|
162
|
+
@validation_options[:strict] = true if @options[:strict] == true
|
161
163
|
|
162
164
|
@@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
|
163
165
|
@data = initialize_data(data)
|
@@ -480,6 +480,55 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
480
480
|
assert(!JSON::Validator.validate(schema,data))
|
481
481
|
end
|
482
482
|
|
483
|
+
def test_strict_properties
|
484
|
+
schema = {
|
485
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
486
|
+
"properties" => {
|
487
|
+
"a" => {"type" => "string"},
|
488
|
+
"b" => {"type" => "string"}
|
489
|
+
}
|
490
|
+
}
|
491
|
+
|
492
|
+
data = {"a" => "a"}
|
493
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
494
|
+
|
495
|
+
data = {"b" => "b"}
|
496
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
497
|
+
|
498
|
+
data = {"a" => "a", "b" => "b"}
|
499
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
500
|
+
|
501
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
502
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
503
|
+
end
|
504
|
+
|
505
|
+
def test_strict_properties_additional_props
|
506
|
+
schema = {
|
507
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
508
|
+
"properties" => {
|
509
|
+
"a" => {"type" => "string"},
|
510
|
+
"b" => {"type" => "string"}
|
511
|
+
},
|
512
|
+
"additionalProperties" => {"type" => "integer"}
|
513
|
+
}
|
514
|
+
|
515
|
+
data = {"a" => "a"}
|
516
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
517
|
+
|
518
|
+
data = {"b" => "b"}
|
519
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
520
|
+
|
521
|
+
data = {"a" => "a", "b" => "b"}
|
522
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
523
|
+
|
524
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
525
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
526
|
+
|
527
|
+
data = {"a" => "a", "b" => "b", "c" => 3}
|
528
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
529
|
+
end
|
530
|
+
|
531
|
+
|
483
532
|
|
484
533
|
def test_pattern
|
485
534
|
# Set up the default datatype
|
@@ -415,6 +415,54 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
415
415
|
assert(!JSON::Validator.validate(schema,data))
|
416
416
|
end
|
417
417
|
|
418
|
+
def test_strict_properties
|
419
|
+
schema = {
|
420
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
421
|
+
"properties" => {
|
422
|
+
"a" => {"type" => "string"},
|
423
|
+
"b" => {"type" => "string"}
|
424
|
+
}
|
425
|
+
}
|
426
|
+
|
427
|
+
data = {"a" => "a"}
|
428
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
429
|
+
|
430
|
+
data = {"b" => "b"}
|
431
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
432
|
+
|
433
|
+
data = {"a" => "a", "b" => "b"}
|
434
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
435
|
+
|
436
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
437
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_strict_properties_additional_props
|
441
|
+
schema = {
|
442
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
443
|
+
"properties" => {
|
444
|
+
"a" => {"type" => "string"},
|
445
|
+
"b" => {"type" => "string"}
|
446
|
+
},
|
447
|
+
"additionalProperties" => {"type" => "integer"}
|
448
|
+
}
|
449
|
+
|
450
|
+
data = {"a" => "a"}
|
451
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
452
|
+
|
453
|
+
data = {"b" => "b"}
|
454
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
455
|
+
|
456
|
+
data = {"a" => "a", "b" => "b"}
|
457
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
458
|
+
|
459
|
+
data = {"a" => "a", "b" => "b", "c" => "c"}
|
460
|
+
assert(!JSON::Validator.validate(schema,data,:strict => true))
|
461
|
+
|
462
|
+
data = {"a" => "a", "b" => "b", "c" => 3}
|
463
|
+
assert(JSON::Validator.validate(schema,data,:strict => true))
|
464
|
+
end
|
465
|
+
|
418
466
|
|
419
467
|
|
420
468
|
def test_unique_items
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
5
|
-
prerelease:
|
4
|
+
version: 2.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kenny Hoxworth
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-15 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description:
|
15
14
|
email: hoxworth@gmail.com
|
@@ -114,27 +113,26 @@ files:
|
|
114
113
|
homepage: http://github.com/hoxworth/json-schema/tree/master
|
115
114
|
licenses:
|
116
115
|
- MIT
|
116
|
+
metadata: {}
|
117
117
|
post_install_message:
|
118
118
|
rdoc_options: []
|
119
119
|
require_paths:
|
120
120
|
- lib
|
121
121
|
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
122
|
requirements:
|
124
123
|
- - ! '>='
|
125
124
|
- !ruby/object:Gem::Version
|
126
125
|
version: '0'
|
127
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
-
none: false
|
129
127
|
requirements:
|
130
128
|
- - ! '>='
|
131
129
|
- !ruby/object:Gem::Version
|
132
130
|
version: '0'
|
133
131
|
requirements: []
|
134
132
|
rubyforge_project:
|
135
|
-
rubygems_version:
|
133
|
+
rubygems_version: 2.0.7
|
136
134
|
signing_key:
|
137
|
-
specification_version:
|
135
|
+
specification_version: 4
|
138
136
|
summary: Ruby JSON Schema Validator
|
139
137
|
test_files:
|
140
138
|
- test/test_all_of_ref_schema.rb
|