-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix rule.each on optional value #709
Fix rule.each on optional value #709
Conversation
I am now getting this rubocop warning after the change.
I'm not sure what I can do to fix it. |
Thanks for opening this PR. There was an issue with our CI setup in main, I just fixed it so could you rebase this branch and f-push? Also, don't worry about that rubocop warning. |
When a key has a `maybe` macro, nil values are valid. However, if a `rule.each` is specified on that key, an error is throw saying: ``` `block in each': undefined method `each_with_index' for nil:NilClass (NoMethodError) ```
Sometimes Rubocop is very picky and can be configure to have that ABC rule less problematic index bc05267..45ba724 100644
--- a/lib/dry/validation/rule.rb
+++ b/lib/dry/validation/rule.rb
@@ -82,16 +82,16 @@ module Dry
@keys = []
@block = proc do
- unless result.base_error?(root) || !values.key?(root)
- values[root].each_with_index do |_, idx|
- path = [*Schema::Path[root].to_a, idx]
+ next if skip_values_evaluation?(root)
- next if result.schema_error?(path)
+ values[root].each_with_index do |_, idx|
+ path = [*Schema::Path[root].to_a, idx]
- evaluator = with(macros: macros, keys: [path], index: idx, &block)
+ next if result.schema_error?(path)
- failures.concat(evaluator.failures)
- end
+ evaluator = with(macros: macros, keys: [path], index: idx, &block)
+
+ failures.concat(evaluator.failures)
end
end
@@ -100,6 +100,15 @@ module Dry
self
end
+ # Helper to determine the need for value's review
+ #
+ # @api private
+ def skip_values_evaluation?(root)
+ result.base_error?(root) || !values.key?(root) || !values.key?(root)
+ end |
@esparta thanks, we can address the metric separately from this PR though 😄 |
Thanks for merging this PR out! ❤️ I agree, the nesting is quite deep. |
Skip processing rule.each on optional value when input is nil
When a key has a
maybe
macro, nil values are valid. However, if arule.each
is specified on that key, an error is throw saying: