json_schema 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_schema/schema_error.rb +3 -2
- data/lib/json_schema/validator.rb +30 -16
- data/test/json_schema/validator_test.rb +15 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55a7249029791e99fb8fc7ad76a76edeab610705
|
4
|
+
data.tar.gz: 71e49d15e5ad71b23f353bc713634763f21c2f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dd1b0c124657f130648484c90b5cce1f7f86deb01eaa6b8f1d9aec40060b4b6d52a546e33a987186b732c12b66f666f5c71391b33df98f1b7ac766becfd69c9
|
7
|
+
data.tar.gz: d0745914ef1acfaa8a039577a8712beb3020827237979deb836dfb59464ce6cdc3424ec9970aa9c9fa8f3c2aae595acff44d819f797e1cc18ad2a808f6315917
|
@@ -18,11 +18,12 @@ module JsonSchema
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class ValidationError < SchemaError
|
21
|
-
attr_accessor :path
|
21
|
+
attr_accessor :path, :sub_errors
|
22
22
|
|
23
|
-
def initialize(schema, path, message, type)
|
23
|
+
def initialize(schema, path, message, type, sub_errors = nil)
|
24
24
|
super(schema, message, type)
|
25
25
|
@path = path
|
26
|
+
@sub_errors = sub_errors
|
26
27
|
end
|
27
28
|
|
28
29
|
def pointer
|
@@ -150,14 +150,22 @@ module JsonSchema
|
|
150
150
|
|
151
151
|
def validate_any_of(schema, data, errors, path)
|
152
152
|
return true if schema.any_of.empty?
|
153
|
-
|
154
|
-
|
153
|
+
|
154
|
+
sub_errors = []
|
155
|
+
|
156
|
+
subschemata_validity = schema.any_of.map do |subschema|
|
157
|
+
current_sub_errors = []
|
158
|
+
sub_errors << current_sub_errors
|
159
|
+
validate_data(subschema, data, current_sub_errors, path)
|
155
160
|
end
|
156
|
-
|
161
|
+
|
162
|
+
unless subschemata_validity.any? { |valid| valid == true }
|
157
163
|
message = %{No subschema in "anyOf" matched.}
|
158
|
-
errors << ValidationError.new(schema, path, message, :any_of_failed)
|
164
|
+
errors << ValidationError.new(schema, path, message, :any_of_failed, sub_errors)
|
165
|
+
return false
|
159
166
|
end
|
160
|
-
|
167
|
+
|
168
|
+
true
|
161
169
|
end
|
162
170
|
|
163
171
|
def validate_dependencies(schema, data, errors, path)
|
@@ -401,19 +409,25 @@ module JsonSchema
|
|
401
409
|
|
402
410
|
def validate_one_of(schema, data, errors, path)
|
403
411
|
return true if schema.one_of.empty?
|
412
|
+
sub_errors = []
|
413
|
+
|
404
414
|
num_valid = schema.one_of.count do |subschema|
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
message =
|
409
|
-
if num_valid == 0
|
410
|
-
%{No subschema in "oneOf" matched.}
|
411
|
-
else
|
412
|
-
%{More than one subschema in "oneOf" matched.}
|
413
|
-
end
|
414
|
-
errors << ValidationError.new(schema, path, message, :one_of_failed)
|
415
|
+
current_sub_errors = []
|
416
|
+
sub_errors << current_sub_errors
|
417
|
+
validate_data(subschema, data, current_sub_errors, path)
|
415
418
|
end
|
416
|
-
|
419
|
+
|
420
|
+
return true if num_valid == 1
|
421
|
+
|
422
|
+
message =
|
423
|
+
if num_valid == 0
|
424
|
+
%{No subschema in "oneOf" matched.}
|
425
|
+
else
|
426
|
+
%{More than one subschema in "oneOf" matched.}
|
427
|
+
end
|
428
|
+
errors << ValidationError.new(schema, path, message, :one_of_failed, sub_errors)
|
429
|
+
|
430
|
+
false
|
417
431
|
end
|
418
432
|
|
419
433
|
def validate_not(schema, data, errors, path)
|
@@ -481,19 +481,31 @@ describe JsonSchema::Validator do
|
|
481
481
|
refute validate
|
482
482
|
assert_includes error_messages, %{No subschema in "anyOf" matched.}
|
483
483
|
assert_includes error_types, :any_of_failed
|
484
|
+
any_of_error = @validator.errors.find { |error| error.type == :any_of_failed }
|
485
|
+
sub_error_messages = any_of_error.sub_errors.map { |errors| errors.map(&:message) }
|
486
|
+
sub_error_types = any_of_error.sub_errors.map { |errors| errors.map(&:type) }
|
487
|
+
assert_includes sub_error_messages, [%{At least 5 characters are required; only 2 were supplied.}]
|
488
|
+
assert_includes sub_error_messages, [%{At least 3 characters are required; only 2 were supplied.}]
|
489
|
+
assert_equal sub_error_types, [[:min_length_failed], [:min_length_failed]]
|
484
490
|
end
|
485
491
|
|
486
492
|
it "validates oneOf" do
|
487
493
|
pointer("#/definitions/app/definitions/contrived").merge!(
|
488
494
|
"oneOf" => [
|
489
495
|
{ "pattern" => "^(foo|aaa)$" },
|
490
|
-
{ "pattern" => "^(foo|zzz)$" }
|
496
|
+
{ "pattern" => "^(foo|zzz)$" },
|
497
|
+
{ "pattern" => "^(hell|no)$" }
|
491
498
|
]
|
492
499
|
)
|
493
500
|
data_sample["contrived"] = "foo"
|
494
501
|
refute validate
|
495
502
|
assert_includes error_messages, %{More than one subschema in "oneOf" matched.}
|
496
503
|
assert_includes error_types, :one_of_failed
|
504
|
+
one_of_error = @validator.errors.find { |error| error.type == :one_of_failed }
|
505
|
+
sub_error_messages = one_of_error.sub_errors.map { |errors| errors.map(&:message) }
|
506
|
+
sub_error_types = one_of_error.sub_errors.map { |errors| errors.map(&:type) }
|
507
|
+
assert_equal sub_error_messages, [[], [], [%{foo does not match /^(hell|no)$/.}]]
|
508
|
+
assert_equal sub_error_types, [[], [], [:pattern_failed]]
|
497
509
|
end
|
498
510
|
|
499
511
|
it "validates not" do
|
@@ -745,11 +757,11 @@ describe JsonSchema::Validator do
|
|
745
757
|
end
|
746
758
|
|
747
759
|
def error_messages
|
748
|
-
@validator.errors.map
|
760
|
+
@validator.errors.map(&:message)
|
749
761
|
end
|
750
762
|
|
751
763
|
def error_types
|
752
|
-
@validator.errors.map
|
764
|
+
@validator.errors.map(&:type)
|
753
765
|
end
|
754
766
|
|
755
767
|
def pointer(path)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|