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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fd4983a9632b29cb79ed96ceba036d590e74719
4
- data.tar.gz: 5a819846b5ef11f8be6a5e5e330be6e0de2bc5d9
3
+ metadata.gz: 55a7249029791e99fb8fc7ad76a76edeab610705
4
+ data.tar.gz: 71e49d15e5ad71b23f353bc713634763f21c2f0c
5
5
  SHA512:
6
- metadata.gz: f61c5a056f07c791252e663b82973468b11b72a6dc9b7e2998c236c0bf3babe49865e8c4a081c29d9a6e79082c12c3c4e62dda67ebed2c4c5c494a95e31b2361
7
- data.tar.gz: 742206a63613cc097969f0a3ef429c7c3d5cdc22305e0115e8bb9ff1d90062a5ba605474361184eea2edf058bdb1ae65997cda3a5425b42781bafaca9fd14558
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
- valid = schema.any_of.any? do |subschema|
154
- validate_data(subschema, data, [], path)
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
- if !valid
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
- valid
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
- validate_data(subschema, data, [], path)
406
- end
407
- if num_valid != 1
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
- num_valid == 1
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 { |e| e.message }
760
+ @validator.errors.map(&:message)
749
761
  end
750
762
 
751
763
  def error_types
752
- @validator.errors.map { |e| e.type }
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.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: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest