dry-schema icon indicating copy to clipboard operation
dry-schema copied to clipboard

Optional array with nested params schema doesn't handle string keys

Open tadeusz-niemiec opened this issue 5 years ago • 1 comments

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

tadeusz-niemiec avatar Sep 15 '20 10:09 tadeusz-niemiec

For the record, this is probably caused by the fact that Macros::Each doesn't rewrite the current type using the inner_schema.

solnic avatar Sep 17 '20 10:09 solnic