dry-rb / dry-rb/dry-schema

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

Open
#396 0 comments 1 reaction 0 assignees View on GitHub
bug help wanted
Dominant language
Ruby
Stars
492
Forks
123
PR merge metrics
No merged PRs in 30d

Description

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

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

```ruby
result = NestedCustomTypeHash.call(foo: { bar: '2021-11-11T00:00:00+00:00'} )
# result.to_h => {:foo=>{:bar=> #}}
```

## My environment

- Affects my production application: **YES**
- Ruby version: 2.7.5
- OS: MacOs 11.6.2

Contributor guide

Open the contributing guide

Research direction

Start with the nested schema reproduction in the issue and compare it with the attempted spec in pull request #395. Run the provided ExpirationDate example and inspect whether the constructor is applied inside the nested hash; done means the result contains the converted DateTime and the nested validation passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.