json-schema_builder 0.8.0 → 0.8.1

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: 2945d0d0e0746865b799fd0e2dcb0e1b066c52db
4
- data.tar.gz: 40da6b42be71e5d59e72115956f9ad98c8950650
3
+ metadata.gz: b4fe5462595486f84e355404b11e92a99f03a564
4
+ data.tar.gz: 3f0940bd48c40ddf384e7456f6c47dab2a8b77ee
5
5
  SHA512:
6
- metadata.gz: 5854cbff037513e952a31785d38e5e2e3b029b389dfafeaff23f0bdc1f07a9969861fec5b3ec2b58db7c7737ede5d726045c39a02b157e8feb6ca42cf468a0ef
7
- data.tar.gz: 01e0e5bb89620531afaf9392e7691ea282e36f31858b54df70cfb938ea64b8d18878262f7c5757b7c2ff001ae2e92f19299f660dba022f4e1c6dca088a2af35f
6
+ metadata.gz: b73789a2efa50e9780bcd9b2f88d84456ff74e02a380d25c69c01506e81d41195025583314407208fce6cfb6ea38542e0834cd7d2379f7b2482563f585a7ed5c
7
+ data.tar.gz: 3b6a8c0bc16cd8561398682e6485802c3b839e7a3c7a6771b7bf29ca3a25a09ba19bc3eb50bc75a71178565ecacf5d755fb9b9e9c893c450ac581cf9338ef5e8
@@ -27,7 +27,7 @@ module JSON
27
27
 
28
28
  def _customize_errors(error_objects)
29
29
  _flatten_errors(error_objects).each do |error|
30
- entities = Array(fragments[error[:fragment]]).select(&:error)
30
+ entities = _entities_handling(error)
31
31
  entities.each do |entity|
32
32
  handler = entity.error
33
33
  case handler
@@ -47,6 +47,16 @@ module JSON
47
47
  [error_object, _flatten_errors(sub_errors)]
48
48
  end.flatten
49
49
  end
50
+
51
+ def _entities_handling(error)
52
+ entities = Array(fragments[error[:fragment]]).select(&:error)
53
+ if error[:failed_attribute] == "Required"
54
+ property = error[:message].match(/required property of '([^']+?)'/)[1]
55
+ property_fragment = "#{error[:fragment]}/#{property}".gsub(%r(//), "/")
56
+ entities |= Array(fragments[property_fragment]).select(&:error)
57
+ end
58
+ entities
59
+ end
50
60
  end
51
61
  end
52
62
  end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.8.0'
3
+ VERSION = '0.8.1'
4
4
  end
5
5
  end
@@ -19,6 +19,29 @@ RSpec.describe Examples::CustomErrors, type: :integration do
19
19
  expect(errors).to include hash_including(message: %q(#/settings requires properties ["name"]))
20
20
  end
21
21
 
22
+ context "with property-specific required errors" do
23
+ let(:schema) { klass.example3 }
24
+
25
+ it "customizes required object errors" do
26
+ errors = schema.fully_validate({ }, customize_errors: true)
27
+ expect(errors.length).to eq(1)
28
+ expect(errors).to include hash_including(message: "Settings is required")
29
+ end
30
+
31
+ it "customizes required object errors" do
32
+ errors = schema.fully_validate({ settings: {} }, customize_errors: true)
33
+ expect(errors.length).to eq(2)
34
+ expect(errors).to include hash_including(message: "Name is required")
35
+ expect(errors).to include hash_including(message: "Other is required")
36
+ end
37
+
38
+ it "customizes required object errors" do
39
+ errors = schema.fully_validate({ settings: { name: "name" } }, customize_errors: true)
40
+ expect(errors.length).to eq(1)
41
+ expect(errors).to include hash_including(message: "Other is required")
42
+ end
43
+ end
44
+
22
45
  it "merges error handlers" do
23
46
  schema = klass.example.schema.merge(klass.example2.schema)
24
47
  errors = schema.fully_validate({ settings: { other: 123 } }, customize_errors: true)
@@ -5,23 +5,25 @@ module Examples
5
5
  def required_error
6
6
  lambda do |entities, error|
7
7
  if error[:failed_attribute] == "Required"
8
- required = entities.map(&:required).inject(:+).uniq
8
+ required = entities.map(&:required).compact.inject(:+).uniq
9
9
  error[:message] = "#{error[:fragment]} requires properties #{required.map(&:to_s).inspect}"
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
- def object_error
14
+ def type_error(message)
15
15
  lambda do |entities, error|
16
- error[:message] = "Custom object error"
16
+ if error[:failed_attribute].start_with?("Type")
17
+ error[:message] = message
18
+ end
17
19
  end
18
20
  end
19
21
 
20
22
  def example
21
23
  object error: required_error do
22
- object :user, required: true, error: object_error
24
+ object :user, required: true, error: type_error("Custom object error")
23
25
  object :settings, error: required_error do
24
- string :name, required: true, error: "Custom name error"
26
+ string :name, required: true, error: type_error("Custom name error")
25
27
  end
26
28
  end
27
29
  end
@@ -29,7 +31,16 @@ module Examples
29
31
  def example2
30
32
  object do
31
33
  object :settings, error: required_error do
32
- string :other, required: true, error: "Other error"
34
+ string :other, required: true, error: type_error("Other error")
35
+ end
36
+ end
37
+ end
38
+
39
+ def example3
40
+ object do
41
+ object :settings, required: true, error: "Settings is required" do
42
+ string :name, required: true, error: "Name is required"
43
+ string :other, required: true, error: "Other is required"
33
44
  end
34
45
  end
35
46
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Parrish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-16 00:00:00.000000000 Z
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler