dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
425 stars 110 forks source link

Nested Hash schema with Custom Type constructor not constructing the right type #396

Open edgenard opened 2 years ago

edgenard commented 2 years ago

Describe the bug

When using a custom type for example: ExpirationDate = Types::DateTime.constructor { |value| value.to_time.round.to_datetime } The constructor does not apply to nested hashes in a schema:


    ExpirationDate = Types::DateTime.constructor { |value| value.to_time.round.to_datetime }
    NestedCustomTypeHash = Dry::Schema.define do
      required(:foo).hash do
        required(:bar).value(ExpirationDate)
      end
    end

result = NestedCustomTypeHash.call(foo: { bar: '2021-11-11T00:00:00+00:00'} )
# result.errors.to_h => {:foo=>{:bar=>["must be a date time"]}}

To Reproduce

I've reproduced it in this PR: https://github.com/dry-rb/dry-schema/pull/395 but could not figure out how to get the spec to pass

Expected behavior

result = NestedCustomTypeHash.call(foo: { bar: '2021-11-11T00:00:00+00:00'} )
# result.to_h => {:foo=>{:bar=> #<DateTime: 2021-11-11T00:00:00+00:00 ((2459530j,0s,0n),+0s,2299161j)>}}

My environment