dry-schema
dry-schema copied to clipboard
Optional array with nested params schema doesn't handle string keys
An optional array with a nested params schema doesn't handle string keys, which causes "is missing" errors. Everything is fine when a hash is provided with symbolized keys. There is also a problem when passing a nil value, which causes NoMethodError.
How to reproduce:
inner_schema = Dry::Schema.Params do
required(:name).filled(:string)
end
schema = Dry::Schema.Params do
required(:users).maybe(:array).each(inner_schema)
end
schema.("users" => [{"name" => 1}]).errors.to_h
# >> { :users => {0=>{:name=>["is missing"]}} }
# it should be: { :users => {0=>{:name=>["must be a string"]}} }
schema.("users" => nil)
# >> NoMethodError: undefined method `map' for nil:NilClass
For the record, this is probably caused by the fact that Macros::Each doesn't rewrite the current type using the inner_schema.