Skip processing `rule.each` on optional value when input is nil
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) ```
This commit is contained in:
parent
1bf0e0c3ff
commit
035f880730
|
@ -82,7 +82,7 @@ module Dry
|
|||
@keys = []
|
||||
|
||||
@block = proc do
|
||||
unless result.base_error?(root) || !values.key?(root)
|
||||
unless result.base_error?(root) || !values.key?(root) || values[root].nil?
|
||||
values[root].each_with_index do |_, idx|
|
||||
path = [*Schema::Path[root].to_a, idx]
|
||||
|
||||
|
|
|
@ -50,4 +50,24 @@ RSpec.describe Dry::Validation::Contract, ".rule" do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with an optional value" do
|
||||
let(:contract_class) do
|
||||
Class.new(Dry::Validation::Contract) do
|
||||
schema do
|
||||
required(:tags).maybe(:array)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when using .each macro" do
|
||||
it "does not fail when input is nil" do
|
||||
contract_class.rule(:tags).each do
|
||||
key.failure("should not be called")
|
||||
end
|
||||
|
||||
expect(contract.(tags: nil).errors.to_h).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue