json_schema 0.14.4 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4568a23c3abb4e52e8a572f5ef4a097b62371e9
4
- data.tar.gz: 7db413d52b9c17c6ee628d3319747eadbd66011e
3
+ metadata.gz: bbbdc5ae870c697c8af588ae0427710ca85fc468
4
+ data.tar.gz: ec29bd27c6b64d12d18ae3d7369a4070fc880a9d
5
5
  SHA512:
6
- metadata.gz: 9108a578bdb1e5e44f8e28358e770bfe2607d97aa23e060b57393ab5a327cac50607a53aae73ca4a56d2579b98854d754d891c37ead2f2a44b0964ec4438aefc
7
- data.tar.gz: 12495587293fa18e9f18a5c7f0b640c650160bb2b4659fa472584a3dede66060cbfd26dfedee47f38b3c02f5fb1b5b13292db7d8c1651f5ea20b74a69bdd41ed
6
+ metadata.gz: d445398c9c67c8d887d7143625875dc1851bf847e8f11362cb6f806c9603f5d3a94611767a7df49d9c6cca27e96c8902d66446ca6bf928e38a85b35b81548fa1
7
+ data.tar.gz: f49c64c93f448620999b0cf743030966052d88913b15a5f588ad2869c2b20bcf98001786f67f4c4c38a0e95c0487e2be9626545719a7646eae9cbcb4d7ae7bc7
data/README.md CHANGED
@@ -58,7 +58,7 @@ ruby -Ilib -Itest test/json_schema/validator_test.rb -n /anyOf/
58
58
  ## Release
59
59
 
60
60
  1. Update the version in `json_schema.gemspec` as appropriate for [semantic
61
- versioning](http://semver.org).
61
+ versioning](http://semver.org) and add details to `CHANGELOG`.
62
62
  2. Run the `release` task:
63
63
 
64
64
  ```
@@ -1,5 +1,6 @@
1
1
  module JsonSchema
2
2
  class Configuration
3
+ attr_accessor :all_of_sub_errors
3
4
  attr_reader :custom_formats
4
5
  attr_reader :validate_regex_with
5
6
 
@@ -15,6 +16,7 @@ module JsonSchema
15
16
  def reset!
16
17
  @validate_regex_with = nil
17
18
  @custom_formats = {}
19
+ @all_of_sub_errors = false
18
20
  end
19
21
 
20
22
  private
@@ -22,6 +24,5 @@ module JsonSchema
22
24
  def initialize
23
25
  reset!
24
26
  end
25
-
26
27
  end
27
28
  end
@@ -144,11 +144,28 @@ module JsonSchema
144
144
 
145
145
  def validate_all_of(schema, data, errors, path)
146
146
  return true if schema.all_of.empty?
147
- valid = schema.all_of.all? do |subschema|
148
- validate_data(subschema, data, errors, path)
147
+
148
+ # We've kept this feature behind a configuration flag for now because
149
+ # there is some performance implication to producing each sub error.
150
+ # Normally we can short circuit the validation after encountering only
151
+ # one problem, but here we have to evaluate all subschemas every time.
152
+ if JsonSchema.configuration.all_of_sub_errors
153
+ sub_errors = []
154
+ valid = schema.all_of.map do |subschema|
155
+ current_sub_errors = []
156
+ sub_errors << current_sub_errors
157
+ validate_data(subschema, data, current_sub_errors, path)
158
+ end.all?
159
+ else
160
+ sub_errors = nil
161
+ valid = schema.all_of.all? do |subschema|
162
+ validate_data(subschema, data, errors, path)
163
+ end
149
164
  end
165
+
150
166
  message = %{Not all subschemas of "allOf" matched.}
151
- errors << ValidationError.new(schema, path, message, :all_of_failed, data: data) if !valid
167
+ errors << ValidationError.new(schema, path, message, :all_of_failed,
168
+ sub_errors: sub_errors, data: data) if !valid
152
169
  valid
153
170
  end
154
171
 
@@ -592,6 +592,19 @@ describe JsonSchema::Validator do
592
592
  end
593
593
 
594
594
  it "validates allOf" do
595
+ pointer("#/definitions/app/definitions/contrived").merge!(
596
+ "allOf" => [
597
+ { "maxLength" => 30 },
598
+ { "minLength" => 3 }
599
+ ]
600
+ )
601
+ data_sample["contrived"] = "ab"
602
+ refute validate
603
+ assert_includes error_messages, %{Not all subschemas of "allOf" matched.}
604
+ assert_includes error_types, :all_of_failed
605
+ end
606
+
607
+ it "includes the failing condition when validating allOf" do
595
608
  pointer("#/definitions/app/definitions/contrived").merge!(
596
609
  "allOf" => [
597
610
  { "maxLength" => 30 },
@@ -601,7 +614,29 @@ describe JsonSchema::Validator do
601
614
  data_sample["contrived"] = "ab"
602
615
  refute validate
603
616
  assert_includes error_messages, %{At least 3 characters are required; only 2 were supplied.}
617
+ assert_includes error_data, "ab"
618
+ end
619
+
620
+ it "includes all failing conditions for allOf as sub-errors when all_of_sub_errors is true" do
621
+ JsonSchema.configure do |c|
622
+ c.all_of_sub_errors = true
623
+ end
624
+ pointer("#/definitions/app/definitions/contrived").merge!(
625
+ "allOf" => [
626
+ { "minLength" => 5 },
627
+ { "minLength" => 3 }
628
+ ]
629
+ )
630
+ data_sample["contrived"] = "ab"
631
+ refute validate
632
+ assert_includes error_messages, %{Not all subschemas of "allOf" matched.}
604
633
  assert_includes error_types, :all_of_failed
634
+ all_of_error = @validator.errors.find { |error| error.type == :all_of_failed }
635
+ sub_error_messages = all_of_error.sub_errors.map { |errors| errors.map(&:message) }
636
+ sub_error_types = all_of_error.sub_errors.map { |errors| errors.map(&:type) }
637
+ assert_includes sub_error_messages, [%{At least 3 characters are required; only 2 were supplied.}]
638
+ assert_includes sub_error_messages, [%{At least 5 characters are required; only 2 were supplied.}]
639
+ assert_equal sub_error_types, [[:min_length_failed], [:min_length_failed]]
605
640
  assert_includes error_data, "ab"
606
641
  end
607
642
 
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.14.4
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-09 00:00:00.000000000 Z
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecma-re-validator