salestation 5.0.0 → 5.1.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
  SHA256:
3
- metadata.gz: 63f86b3537b2db5857f54ef439442a3ca0ef1a4f078d602989783814fd70b718
4
- data.tar.gz: 2836da19672da049c461769c8482e4c9cab2e3ce6d6292293051639c0a0cb8e7
3
+ metadata.gz: 43663c5f6d78d7238e66e9ac88a06a49facfcaf45a329534e44e20891ea1baa9
4
+ data.tar.gz: 97661dd29a53ab956cbe0c78522b382d052bd4c6f339208fe126f7d3c51f8526
5
5
  SHA512:
6
- metadata.gz: dc79ff3f36806274195543e4fc0e13f27d098d1239b3c0706cd6a7ed3f643a764a71b2b34426ccfbe19ebc9f29b0018a8c8619a1c2f9729c180df70a2b640906
7
- data.tar.gz: 4b4d65d3e6158c9ff0025f2e5548b650dab2d0ca3bf4c5dca3f51b11943be552c44eadda05cb5830962157ef2d1200e8067b731eebbc74bb501d83e8a39d7753
6
+ metadata.gz: ee4cf2f592d41548e187277789ec6d3484748dc7cc152f3a9b4e4b63b438314e5fe96ab3aeb92dbd4729a485b4d9e73b518904459309b19f3da7a507595100ce
7
+ data.tar.gz: 22a1c5415ded67ebc22c5f67ef6332706571a156000aac7e5195101c73be14dc950b5f208fb24b19e27c94b70c3c854dc8a5da485a01b030d578e9ccaac92c57
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .rspec_persistence
@@ -18,7 +18,7 @@ module Salestation
18
18
  Errors::InvalidInput.new(errors: result.errors, hints: result.hints)
19
19
  )
20
20
  end
21
- elsif dry_validation_version <= Gem::Version.new('1.8')
21
+ elsif dry_validation_version < Gem::Version.new('2.0')
22
22
  if result.success?
23
23
  request.replace_input(input)
24
24
  else
@@ -12,37 +12,73 @@ module Salestation
12
12
  def initialize
13
13
  @fields = []
14
14
  @field_error_types = {}
15
+ @field_error_messages = {}
15
16
  end
16
17
 
17
- def on(field)
18
- @fields << field
18
+ def on(*nested_fields)
19
+ @fields << nested_fields
19
20
  self
20
21
  end
21
22
 
22
23
  def with_type(*types)
23
- @field_error_types[@fields.last] = types
24
+ @field_error_types[field_to_key(@fields.last)] = types
25
+ self
26
+ end
27
+
28
+ def with_message(*messages)
29
+ @field_error_messages[field_to_key(@fields.last)] = messages
24
30
  self
25
31
  end
26
32
 
27
33
  def matches?(actual)
28
34
  @fields.all? do |field|
29
- check_field_exists(field, actual) &&
30
- check_field_error_types(field, actual)
35
+ check_field_exists(actual, *field) &&
36
+ check_field_error_types(field, actual) &&
37
+ check_field_error_messages(field, actual)
31
38
  end
32
39
  end
33
40
 
34
41
  private
35
42
 
36
- def check_field_exists(field, actual)
37
- actual[:error_details].key?(field)
43
+ def check_field_exists(actual, field, *nested_fields)
44
+ actual[:error_details].key?(field) &&
45
+ (
46
+ nested_fields.empty? ||
47
+ actual[:error_details][field].any? do |nested_hash|
48
+ check_field_exists(nested_hash, *nested_fields)
49
+ end
50
+ )
38
51
  end
39
52
 
40
53
  def check_field_error_types(field, actual)
41
- return true unless @field_error_types[field]
54
+ return true unless @field_error_types[field_to_key(field)]
55
+
56
+ actual_error_types = fetch_field_attribute_nested(actual, :type, *field)
57
+ (@field_error_types[field_to_key(field)] - actual_error_types).empty?
58
+ end
59
+
60
+ def check_field_error_messages(field, actual)
61
+ return true unless @field_error_messages[field_to_key(field)]
42
62
 
43
- actual_error_types = actual[:error_details].fetch(field).map { |f| f.fetch(:type) }
63
+ actual_error_messages = fetch_field_attribute_nested(actual, :message, *field)
64
+ (@field_error_messages[field_to_key(field)] - actual_error_messages).empty?
65
+ end
66
+
67
+
68
+ def fetch_field_attribute_nested(actual, attribute, field, *nested_fields)
69
+ if nested_fields.empty?
70
+ actual[:error_details].fetch(field).map { |f| f.fetch(attribute) }
71
+ else
72
+ actual[:error_details][field]
73
+ .lazy
74
+ .map { |error_hash| fetch_field_attribute_nested(error_hash, attribute, *nested_fields) }
75
+ .reject(&:nil?)
76
+ .first
77
+ end
78
+ end
44
79
 
45
- (@field_error_types[field] - actual_error_types).empty?
80
+ def field_to_key(fields)
81
+ fields.join('->').to_sym
46
82
  end
47
83
  end
48
84
  end
@@ -44,6 +44,23 @@ module Salestation
44
44
  # .on(:phone_number).with_type(Glia::Errors::INVALID_FORMAT_ERROR)
45
45
  # )
46
46
  #
47
+ # also has support for nested errors:
48
+ # nesting can be represented by calling `on` method with multiple arguments, for example when
49
+ # you have a hash like
50
+ # {
51
+ # nested: {
52
+ # name: ''
53
+ # }
54
+ # }
55
+ # Then you can call it like glia_input_validation_error.on(:nested, :name)
56
+ #
57
+ # expect(result).to be_failure
58
+ # .with_invalid_input
59
+ # .containing(
60
+ # glia_input_validation_error
61
+ # .on(:nested, :name).with_type(Glia::Errors::INVALID_VALUE_ERROR)
62
+ # )
63
+ #
47
64
  # Everything after be_failure is optional. You could also use `.containing`
48
65
  # multiple times like this:
49
66
  #
data/salestation.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "salestation"
7
- spec.version = "5.0.0"
7
+ spec.version = "5.1.0"
8
8
  spec.authors = ["Glia TechMovers"]
9
9
  spec.email = ["techmovers@glia.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: salestation
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glia TechMovers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-11 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler