dry-validation 1.5.0 → 1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +52 -2
- data/lib/dry/validation/contract.rb +2 -2
- data/lib/dry/validation/evaluator.rb +13 -6
- data/lib/dry/validation/extensions/hints.rb +1 -3
- data/lib/dry/validation/macros.rb +1 -1
- data/lib/dry/validation/message_set.rb +1 -1
- data/lib/dry/validation/messages/resolver.rb +27 -2
- data/lib/dry/validation/result.rb +9 -2
- data/lib/dry/validation/rule.rb +4 -4
- data/lib/dry/validation/values.rb +6 -1
- data/lib/dry/validation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e10a66994158c6865c81439277cbbe57a629189cd20257c500658821dd520431
|
4
|
+
data.tar.gz: 00017e8bc8bf56b4890f5ff441f303556276892681e2c4d732a5ffda52bd54b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dcb6dba84761e057a2bb72e2b4fbd47bba9c568ee0b1bc52e6b5bd48bbd6e7dc804f418746b7d61b9a98fbaede7be075fe6f2b6610209ec934ca8857a1898c4
|
7
|
+
data.tar.gz: 4a5b3cdd5a6afea0b99a368dc2ce8b244474897692f753e00231b0c77ef4fa7f9ff363b538b1e968ca0a1aa54294efcf0ec44108acaddbcdb1fa267a02ab6b16
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,54 @@
|
|
1
|
-
##
|
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
|
+
|
12
|
+
## 1.5.3 2020-07-27
|
13
|
+
|
14
|
+
|
15
|
+
### Added
|
16
|
+
|
17
|
+
- You can now access current value's index via `rule(:foo).each do |index:|` (issue #606 done via #657) (@mrbongiolo)
|
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
|
+
|
23
|
+
### Changed
|
24
|
+
|
25
|
+
- `Result#error?` is now a public API and it takes into consideration both schema and rule errors (issue #655 fixed via #656) (@PragTob)
|
26
|
+
|
27
|
+
[Compare v1.5.2...v1.5.3](https://github.com/dry-rb/dry-validation/compare/v1.5.2...v1.5.3)
|
28
|
+
|
29
|
+
## 1.5.2 2020-07-14
|
30
|
+
|
31
|
+
|
32
|
+
### Fixed
|
33
|
+
|
34
|
+
- `key?` predicate in rules no longer crashes when the rule path points to a non-existent array value (issue #653 fixed via #654) (@solnic)
|
35
|
+
|
36
|
+
|
37
|
+
[Compare v1.5.1...v1.5.2](https://github.com/dry-rb/dry-validation/compare/v1.5.1...v1.5.2)
|
38
|
+
|
39
|
+
## 1.5.1 2020-06-18
|
40
|
+
|
41
|
+
|
42
|
+
### Fixed
|
43
|
+
|
44
|
+
- dry-monads no longer required for the `:hints` extension (@schokomarie)
|
45
|
+
- Using `full: true` option works as expected with custom rule messages (issue #618 fixed via #651) (@sirfilip)
|
46
|
+
- Using `locale: ...` option works as expected with hints (issue #589 fixed via 652) (@sirfilip)
|
47
|
+
|
48
|
+
|
49
|
+
[Compare v1.5.0...v1.5.1](https://github.com/dry-rb/dry-validation/compare/v1.5.0...v1.5.1)
|
50
|
+
|
51
|
+
## 1.5.0 2020-03-11
|
2
52
|
|
3
53
|
|
4
54
|
### Added
|
@@ -11,7 +61,7 @@
|
|
11
61
|
- dry-schema dependency was bumped to `~> 1.5` (@solnic)
|
12
62
|
- [internal] `KeyMap` patches have been removed since dry-schema now provides required functionality (@solnic)
|
13
63
|
|
14
|
-
[Compare v1.4.2...
|
64
|
+
[Compare v1.4.2...v1.5.0](https://github.com/dry-rb/dry-validation/compare/v1.4.2...v1.5.0)
|
15
65
|
|
16
66
|
## 1.4.2 2020-01-18
|
17
67
|
|
@@ -123,14 +123,14 @@ module Dry
|
|
123
123
|
return path.expand.any? { |nested_path| error?(result, nested_path) }
|
124
124
|
end
|
125
125
|
|
126
|
-
return true if result.
|
126
|
+
return true if result.schema_error?(path)
|
127
127
|
|
128
128
|
path
|
129
129
|
.to_a[0..-2]
|
130
130
|
.any? { |key|
|
131
131
|
curr_path = Schema::Path[path.keys[0..path.keys.index(key)]]
|
132
132
|
|
133
|
-
return false unless result.
|
133
|
+
return false unless result.schema_error?(curr_path)
|
134
134
|
|
135
135
|
result.errors.any? { |err|
|
136
136
|
(other = Schema::Path[err.path]).same_root?(curr_path) && other == curr_path
|
@@ -146,7 +146,7 @@ module Dry
|
|
146
146
|
#
|
147
147
|
# @return [Object]
|
148
148
|
#
|
149
|
-
# @public
|
149
|
+
# @api public
|
150
150
|
def value
|
151
151
|
values[key_name]
|
152
152
|
end
|
@@ -155,27 +155,34 @@ 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?(
|
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
|
171
178
|
#
|
172
|
-
# @param [Symbol, String, Array] A Path-compatible spec
|
179
|
+
# @param path [Symbol, String, Array] A Path-compatible spec
|
173
180
|
#
|
174
181
|
# @return [Boolean]
|
175
182
|
#
|
176
183
|
# @api public
|
177
184
|
def schema_error?(path)
|
178
|
-
result.
|
185
|
+
result.schema_error?(path)
|
179
186
|
end
|
180
187
|
|
181
188
|
# Check if there are any errors on the current rule
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/monads/result"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Validation
|
7
5
|
# Hints extension
|
@@ -46,7 +44,7 @@ module Dry
|
|
46
44
|
#
|
47
45
|
# @api public
|
48
46
|
def messages(new_options = EMPTY_HASH)
|
49
|
-
errors.with(hints.to_a, options.merge(**new_options))
|
47
|
+
errors.with(hints(new_options).to_a, options.merge(**new_options))
|
50
48
|
end
|
51
49
|
|
52
50
|
# Return hint messages
|
@@ -85,7 +85,7 @@ module Dry
|
|
85
85
|
# @api private
|
86
86
|
def freeze
|
87
87
|
source_messages.select { |err| err.respond_to?(:evaluate) }.each do |err|
|
88
|
-
idx = source_messages.index(err)
|
88
|
+
idx = messages.index(err) || source_messages.index(err)
|
89
89
|
msg = err.evaluate(locale: locale, full: options[:full])
|
90
90
|
messages[idx] = msg
|
91
91
|
end
|
@@ -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[message, 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|
|
@@ -69,6 +72,11 @@ module Dry
|
|
69
72
|
template, meta = messages[rule, msg_opts.merge(path: keys.last)] unless template
|
70
73
|
end
|
71
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
|
+
|
72
80
|
unless template
|
73
81
|
raise MissingMessageError, <<~STR
|
74
82
|
Message template for #{rule.inspect} under #{keys.join(DOT).inspect} was not found
|
@@ -78,12 +86,29 @@ module Dry
|
|
78
86
|
parsed_tokens = parse_tokens(tokens)
|
79
87
|
text = template.(template.data(parsed_tokens))
|
80
88
|
|
81
|
-
[
|
89
|
+
[message_text(text, path: path, locale: locale, full: full), meta]
|
82
90
|
end
|
83
91
|
# rubocop:enable Metrics/AbcSize
|
84
92
|
|
85
93
|
private
|
86
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
|
+
|
87
112
|
def parse_tokens(tokens)
|
88
113
|
Hash[
|
89
114
|
tokens.map do |key, token|
|
@@ -101,8 +101,15 @@ module Dry
|
|
101
101
|
|
102
102
|
# Check if values include an error for the provided key
|
103
103
|
#
|
104
|
-
# @api
|
104
|
+
# @api public
|
105
105
|
def error?(key)
|
106
|
+
errors.any? { |msg| Schema::Path[msg.path].include?(Schema::Path[key]) }
|
107
|
+
end
|
108
|
+
|
109
|
+
# Check if the base schema (without rules) includes an error for the provided key
|
110
|
+
#
|
111
|
+
# @api private
|
112
|
+
def schema_error?(key)
|
106
113
|
schema_result.error?(key)
|
107
114
|
end
|
108
115
|
|
@@ -116,7 +123,7 @@ module Dry
|
|
116
123
|
key_path = Schema::Path[key]
|
117
124
|
err_path = Schema::Path[error.path]
|
118
125
|
|
119
|
-
|
126
|
+
next unless key_path.same_root?(err_path)
|
120
127
|
|
121
128
|
key_path == err_path
|
122
129
|
}
|
data/lib/dry/validation/rule.rb
CHANGED
@@ -65,8 +65,8 @@ module Dry
|
|
65
65
|
# for a given array item.
|
66
66
|
#
|
67
67
|
# @example
|
68
|
-
# rule(:nums).each do
|
69
|
-
# key.failure("must be greater than 0") if value < 0
|
68
|
+
# rule(:nums).each do |index:|
|
69
|
+
# key([:number, index]).failure("must be greater than 0") if value < 0
|
70
70
|
# end
|
71
71
|
# rule(:nums).each(min: 3)
|
72
72
|
# rule(address: :city) do
|
@@ -86,9 +86,9 @@ module Dry
|
|
86
86
|
values[root].each_with_index do |_, idx|
|
87
87
|
path = [*Schema::Path[root].to_a, idx]
|
88
88
|
|
89
|
-
next if result.
|
89
|
+
next if result.schema_error?(path)
|
90
90
|
|
91
|
-
evaluator = with(macros: macros, keys: [path], &block)
|
91
|
+
evaluator = with(macros: macros, keys: [path], index: idx, &block)
|
92
92
|
|
93
93
|
failures.concat(evaluator.failures)
|
94
94
|
end
|
@@ -35,7 +35,10 @@ module Dry
|
|
35
35
|
# key.failure('must be > 18') if values[:age] <= 18
|
36
36
|
# end
|
37
37
|
#
|
38
|
-
# @param [Symbol]
|
38
|
+
# @param args [Symbol, String, Hash, Array<Symbol>] If given as a single
|
39
|
+
# Symbol, String, Array or Hash, build a key array using
|
40
|
+
# {Dry::Schema::Path} digging for data. If given as positional
|
41
|
+
# arguments, use these with Hash#dig on the data directly.
|
39
42
|
#
|
40
43
|
# @return [Object]
|
41
44
|
#
|
@@ -65,6 +68,8 @@ module Dry
|
|
65
68
|
if e.is_a?(Array)
|
66
69
|
result = e.all? { |k| key?(k, a) }
|
67
70
|
return result
|
71
|
+
elsif e.is_a?(Symbol) && a.is_a?(Array)
|
72
|
+
return false
|
68
73
|
else
|
69
74
|
return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
|
70
75
|
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.
|
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-
|
11
|
+
date: 2020-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|