I have a case where I need to validate a structure which is allowed to be nil… sounds odd but bear with me. It doesn’t look like nested validations are respected when the parent is allowed to be nil.
require "dry-validation"
SomeSchema = Dry::Validation.Schema do
optional(:parent_hash).maybe.schema do
required(:child_hash).schema do
required(:attribute).filled(:str?)
end
end
end
puts SomeSchema.(
parent_hash: {
child_hash: { attribute: "Foobar" }
},
).inspect
puts SomeSchema.(
parent_hash: {
child_hash: { attribute: nil }
},
).inspect
puts SomeSchema.(
parent_hash: {
child_hash: nil
},
).inspect
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>"Foobar"}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>nil}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>nil}} errors={}>
Removing the maybe
I get this:
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>"Foobar"}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>nil}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>nil}} errors={:parent_hash=>{:child_hash=>["must be filled"]}}>
Even with the maybe removed the validation is only so deep.
Any help is appreciated.