You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error is returned back on rule.each when passed in the input value is nil. nil input should be accepted when a maybe is used on the key.
Error: undefined method `each_with_index' for nil:NilClass (NoMethodError)
To Reproduce
require 'dry-validation'
input = {
webhooks: nil
}
Schema = Dry::Schema.Params do
required(:id).value(:string)
end
Contract = Dry::Validation.Contract do
params do
optional(:webhooks).maybe { type?(Array) & max_size?(10) }
end
register_macro(:schema) do |macro:|
key.failure('something')
end
rule(:webhooks).each(schema: Schema)
end
result = Contract.call(input)
puts result.to_h
Stack trace:
9: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/contract.rb:105:in `call'
8: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/result.rb:25:in `new'
7: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/contract.rb:106:in `block in call'
6: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/contract.rb:106:in `each'
5: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/contract.rb:109:in `block (2 levels) in call'
4: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/rule.rb:38:in `call'
3: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/rule.rb:38:in `new'
2: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/evaluator.rb:77:in `initialize'
1: from /.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/evaluator.rb:77:in `instance_exec'
/.../.rvm/gems/ruby-2.6.6/gems/dry-validation-1.7.0/lib/dry/validation/rule.rb:86:in `block in each': undefined method `each_with_index' for nil:NilClass (NoMethodError)
Thanks for reporting this. I'm not sure if we want to implicitly skip applying rules just because something is nil without actually making sure that nil is an acceptable value. IIRC it does happen already in some cases but I've always had mixed feelings about this. Having said that, for consistency's sake we should fix it like you suggested.
Describe the bug
Error is returned back on
rule.each
when passed in the input value isnil
.nil
input should be accepted when amaybe
is used on the key.Error: undefined method `each_with_index' for nil:NilClass (NoMethodError)
To Reproduce
Stack trace:
Location where error occurs: https://github.com/dry-rb/dry-validation/blob/main/lib/dry/validation/rule.rb#L86
Expected behavior
The contract should pass validation since the
maybe
specifies thatnil
values are accepted.Potential solution
This could be fixed by adding a
|| values[root].nil?
to the conditional check on the unless block. Is this solution OK?My environment
The text was updated successfully, but these errors were encountered: