Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
edgenard opened this issue Jan 28, 2022 · 0 comments

Comments

@edgenard
Copy link

edgenard commented Jan 28, 2022

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: #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

  • Affects my production application: YES
  • Ruby version: 2.7.5
  • OS: MacOs 11.6.2
@flash-gordon flash-gordon changed the title Nested Hash schema with Custom Type constructor not constructing the right type. Nested Hash schema with Custom Type constructor not constructing the right type Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant