dry-validation 1.5.3 → 1.5.4

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
  SHA256:
3
- metadata.gz: 335dfa8db4764cd1c86619371fea202bfeb0c80cf9a84a2069d7fe4c92a1f5af
4
- data.tar.gz: 851e5d59e8c4a1c9ce338d5e3fb01b02159da120fb2a775bcec5168ac705c20e
3
+ metadata.gz: e10a66994158c6865c81439277cbbe57a629189cd20257c500658821dd520431
4
+ data.tar.gz: 00017e8bc8bf56b4890f5ff441f303556276892681e2c4d732a5ffda52bd54b2
5
5
  SHA512:
6
- metadata.gz: 1da1b675edac12c4d7256cb15b770a87387ffc352330434db7a82f177c65ce21dcc8a4803fce2be1d85d29be8df26979fe6e91d43111cebe04c6875500523c29
7
- data.tar.gz: 102cf75e197be20843af749584c2a6abd57ce8334d940bff7dd06dc4e37c54fd451bb244dc3365bca913334e3b0c3b3bd391c68b3f9c4e13c4da6efb9c69213f
6
+ metadata.gz: 4dcb6dba84761e057a2bb72e2b4fbd47bba9c568ee0b1bc52e6b5bd48bbd6e7dc804f418746b7d61b9a98fbaede7be075fe6f2b6610209ec934ca8857a1898c4
7
+ data.tar.gz: 4a5b3cdd5a6afea0b99a368dc2ce8b244474897692f753e00231b0c77ef4fa7f9ff363b538b1e968ca0a1aa54294efcf0ec44108acaddbcdb1fa267a02ab6b16
@@ -1,3 +1,14 @@
1
+ ## 1.5.4 2020-08-21
2
+
3
+
4
+ ### Fixed
5
+
6
+ - Full messages work correctly with rule failures now (issue #661 fixed via #662) (@stind)
7
+ - Providing a custom message template for array errors works correctly (issue #663 fixed via #665) (@tadeusz-niemiec)
8
+
9
+
10
+ [Compare v1.5.3...v1.5.4](https://github.com/dry-rb/dry-validation/compare/v1.5.3...v1.5.4)
11
+
1
12
  ## 1.5.3 2020-07-27
2
13
 
3
14
 
@@ -5,6 +16,10 @@
5
16
 
6
17
  - You can now access current value's index via `rule(:foo).each do |index:|` (issue #606 done via #657) (@mrbongiolo)
7
18
 
19
+ ### Fixed
20
+
21
+ - Using `.each(:foo)` works as expected when there are errors related to other keys (issue #659 fixed via #660) (@solnic)
22
+
8
23
  ### Changed
9
24
 
10
25
  - `Result#error?` is now a public API and it takes into consideration both schema and rule errors (issue #655 fixed via #656) (@PragTob)
@@ -155,16 +155,23 @@ module Dry
155
155
  #
156
156
  # This is useful when dealing with rules for optional keys
157
157
  #
158
- # @example
158
+ # @example use the default key name
159
159
  # rule(:age) do
160
160
  # key.failure(:invalid) if key? && value < 18
161
161
  # end
162
162
  #
163
+ # @example specify the key name
164
+ # rule(:start_date, :end_date) do
165
+ # if key?(:start_date) && !key?(:end_date)
166
+ # key(:end_date).failure("must provide an end_date with start_date")
167
+ # end
168
+ # end
169
+ #
163
170
  # @return [Boolean]
164
171
  #
165
172
  # @api public
166
- def key?
167
- values.key?(key_name)
173
+ def key?(name = key_name)
174
+ values.key?(name)
168
175
  end
169
176
 
170
177
  # Check if there are any errors on the schema under the provided path
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "dry/validation/message"
4
+ require "dry/schema/message_compiler"
4
5
 
5
6
  module Dry
6
7
  module Validation
7
8
  module Messages
9
+ FULL_MESSAGE_WHITESPACE = Dry::Schema::MessageCompiler::FULL_MESSAGE_WHITESPACE
10
+
8
11
  # Resolve translated messages from failure arguments
9
12
  #
10
13
  # @api public
@@ -33,7 +36,7 @@ module Dry
33
36
  when Symbol
34
37
  Message[->(**opts) { message(message, path: path, tokens: tokens, **opts) }, path, meta]
35
38
  when String
36
- Message[->(**opts) { [message_text(message, path, **opts), meta] }, path, meta]
39
+ Message[->(**opts) { [message_text(message, path: path, **opts), meta] }, path, meta]
37
40
  when Hash
38
41
  meta = message.dup
39
42
  text = meta.delete(:text) { |key|
@@ -51,18 +54,6 @@ module Dry
51
54
  end
52
55
  alias_method :[], :call
53
56
 
54
- # Resolve a message
55
- #
56
- # @return String
57
- #
58
- # @api public
59
- def message_text(message, path, locale: nil, full: false, **opts)
60
- keys = path.to_a.compact
61
- msg_opts = EMPTY_HASH.merge(path: keys, locale: locale || messages.default_locale)
62
-
63
- full ? "#{messages.rule(keys.last, msg_opts) || keys.last} #{message}" : message
64
- end
65
-
66
57
  # Resolve a message
67
58
  #
68
59
  # @return [String]
@@ -81,6 +72,11 @@ module Dry
81
72
  template, meta = messages[rule, msg_opts.merge(path: keys.last)] unless template
82
73
  end
83
74
 
75
+ if !template && keys.size > 1
76
+ non_index_keys = keys.reject { |k| k.is_a?(Integer) }
77
+ template, meta = messages[rule, msg_opts.merge(path: non_index_keys.join(DOT))]
78
+ end
79
+
84
80
  unless template
85
81
  raise MissingMessageError, <<~STR
86
82
  Message template for #{rule.inspect} under #{keys.join(DOT).inspect} was not found
@@ -90,12 +86,29 @@ module Dry
90
86
  parsed_tokens = parse_tokens(tokens)
91
87
  text = template.(template.data(parsed_tokens))
92
88
 
93
- [full ? "#{messages.rule(keys.last, msg_opts)} #{text}" : text, meta]
89
+ [message_text(text, path: path, locale: locale, full: full), meta]
94
90
  end
95
91
  # rubocop:enable Metrics/AbcSize
96
92
 
97
93
  private
98
94
 
95
+ def message_text(text, path:, locale: nil, full: false)
96
+ return text unless full
97
+
98
+ key = key_text(path: path, locale: locale)
99
+
100
+ [key, text].compact.join(FULL_MESSAGE_WHITESPACE[locale])
101
+ end
102
+
103
+ def key_text(path:, locale: nil)
104
+ locale ||= messages.default_locale
105
+
106
+ keys = path.to_a.compact
107
+ msg_opts = {path: keys, locale: locale}
108
+
109
+ messages.rule(keys.last, msg_opts) || keys.last
110
+ end
111
+
99
112
  def parse_tokens(tokens)
100
113
  Hash[
101
114
  tokens.map do |key, token|
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = "1.5.3"
5
+ VERSION = "1.5.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-27 00:00:00.000000000 Z
11
+ date: 2020-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby