dry-validation 1.5.1 → 1.5.6

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: 2deba7f8bd2d4ad011e0f4be2aa20d4f20d219d85d0eeec23d3b3e0b12c0bcf4
4
- data.tar.gz: 12f9500062c0239d19f6bf32450db22065a9caa3062e4cb71d79a9370c3229ec
3
+ metadata.gz: baed168e736076bb6f402d21c43f79b7f14014d2c86c2cb4c5ff0d286ed340b1
4
+ data.tar.gz: 648d5963b3d6a25c71e9bac28ff3476318599f50c989d2299486f826109b4015
5
5
  SHA512:
6
- metadata.gz: be6cf6d5dfce8ebec88743b7661eaf4d3cc1294cea249b87e7c02fd00b36013a033fe5feab1ccdb3e6e6020873435fbfc5bf6ee41b5f76836358973f5074843e
7
- data.tar.gz: 77f87593b028dfa608f22835413a73fcbd7c2585e9a98c95f20a158df47fc5ca3f46a0357188f5bd1fc37140de7b57b833ae969aabe55bbdf71628f7db6f645e
6
+ metadata.gz: 5de2142ce8dc474eded404e9f13904ab824dd044ad6be1e3054773b0846ee5987c3008244dacab8595cce94cbb500fb30e3f1a8d435bbed8e400e64065c1c80a
7
+ data.tar.gz: bfbf80ec801ad412437f3f7a95f205d4f85004ba1ff93af61db4c21deb01063bb39b6e389eac761728721de55e14716e16e7eb07d9f53771f4ea18d7564a00b7
@@ -1,14 +1,78 @@
1
- ## unreleased
1
+ ## 1.5.5
2
+
3
+ 2020-09-04
4
+
5
+ ### Fixed
6
+
7
+ - Dependency on dry-schema was bumped to >= 1.5.1. This time for real (@solnic)
8
+
9
+
10
+ [Compare v1.5.5...master](https://github.com/dry-rb/dry-validation/compare/v1.5.5...master)
11
+
12
+ ## 1.5.5
13
+
14
+ 2020-09-03
15
+
16
+ ### Fixed
17
+
18
+ - Dependency on dry-schema was bumped to >= 1.5.2 (see #666 for more info) (@artofhuman)
19
+
20
+
21
+ [Compare v1.5.4...master](https://github.com/dry-rb/dry-validation/compare/v1.5.4...master)
22
+
23
+ ## 1.5.4 2020-08-21
24
+
25
+
26
+ ### Added
27
+
28
+ - You can now pass any key or a path to the rule's `key?` helper (see #664 for more info) (@alassek)
29
+
30
+ ### Fixed
31
+
32
+ - Full messages work correctly with rule failures now (issue #661 fixed via #662) (@stind)
33
+ - Providing a custom message template for array errors works correctly (issue #663 fixed via #665) (@tadeusz-niemiec)
34
+
35
+
36
+ [Compare v1.5.3...v1.5.4](https://github.com/dry-rb/dry-validation/compare/v1.5.3...v1.5.4)
37
+
38
+ ## 1.5.3 2020-07-27
39
+
40
+
41
+ ### Added
42
+
43
+ - You can now access current value's index via `rule(:foo).each do |index:|` (issue #606 done via #657) (@mrbongiolo)
44
+
45
+ ### Fixed
46
+
47
+ - Using `.each(:foo)` works as expected when there are errors related to other keys (issue #659 fixed via #660) (@solnic)
48
+
49
+ ### Changed
50
+
51
+ - `Result#error?` is now a public API and it takes into consideration both schema and rule errors (issue #655 fixed via #656) (@PragTob)
52
+
53
+ [Compare v1.5.2...v1.5.3](https://github.com/dry-rb/dry-validation/compare/v1.5.2...v1.5.3)
54
+
55
+ ## 1.5.2 2020-07-14
56
+
57
+
58
+ ### Fixed
59
+
60
+ - `key?` predicate in rules no longer crashes when the rule path points to a non-existent array value (issue #653 fixed via #654) (@solnic)
61
+
62
+
63
+ [Compare v1.5.1...v1.5.2](https://github.com/dry-rb/dry-validation/compare/v1.5.1...v1.5.2)
64
+
65
+ ## 1.5.1 2020-06-18
2
66
 
3
67
 
4
68
  ### Fixed
5
69
 
6
70
  - dry-monads no longer required for the `:hints` extension (@schokomarie)
7
71
  - Using `full: true` option works as expected with custom rule messages (issue #618 fixed via #651) (@sirfilip)
8
- - Using `locale: ...` option works as expected with hints (issue #589 fixed via #652) (@sirfilip)
72
+ - Using `locale: ...` option works as expected with hints (issue #589 fixed via 652) (@sirfilip)
9
73
 
10
74
 
11
- [Compare v1.5.0...master](https://github.com/dry-rb/dry-validation/compare/v1.5.0...master)
75
+ [Compare v1.5.0...v1.5.1](https://github.com/dry-rb/dry-validation/compare/v1.5.0...v1.5.1)
12
76
 
13
77
  ## 1.5.0 2020-03-11
14
78
 
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_runtime_dependency "dry-core", "~> 0.4"
34
34
  spec.add_runtime_dependency "dry-equalizer", "~> 0.2"
35
35
  spec.add_runtime_dependency "dry-initializer", "~> 3.0"
36
- spec.add_runtime_dependency "dry-schema", "~> 1.5"
36
+ spec.add_runtime_dependency "dry-schema", "~> 1.5", ">= 1.5.2"
37
37
 
38
38
  spec.add_development_dependency "bundler"
39
39
  spec.add_development_dependency "rake"
@@ -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.error?(path)
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.error?(curr_path)
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
@@ -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
@@ -175,7 +182,7 @@ module Dry
175
182
  #
176
183
  # @api public
177
184
  def schema_error?(path)
178
- result.error?(path)
185
+ result.schema_error?(path)
179
186
  end
180
187
 
181
188
  # Check if there are any errors on the current rule
@@ -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|
@@ -101,8 +101,15 @@ module Dry
101
101
 
102
102
  # Check if values include an error for the provided key
103
103
  #
104
- # @api private
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
- return false unless key_path.same_root?(err_path)
126
+ next unless key_path.same_root?(err_path)
120
127
 
121
128
  key_path == err_path
122
129
  }
@@ -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.error?(path)
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
@@ -68,6 +68,8 @@ module Dry
68
68
  if e.is_a?(Array)
69
69
  result = e.all? { |k| key?(k, a) }
70
70
  return result
71
+ elsif e.is_a?(Symbol) && a.is_a?(Array)
72
+ return false
71
73
  else
72
74
  return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
73
75
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = "1.5.1"
5
+ VERSION = "1.5.6"
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.1
4
+ version: 1.5.6
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-06-18 00:00:00.000000000 Z
11
+ date: 2020-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -93,6 +93,9 @@ dependencies:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
95
  version: '1.5'
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.5.2
96
99
  type: :runtime
97
100
  prerelease: false
98
101
  version_requirements: !ruby/object:Gem::Requirement
@@ -100,6 +103,9 @@ dependencies:
100
103
  - - "~>"
101
104
  - !ruby/object:Gem::Version
102
105
  version: '1.5'
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 1.5.2
103
109
  - !ruby/object:Gem::Dependency
104
110
  name: bundler
105
111
  requirement: !ruby/object:Gem::Requirement