dry-rb / dry-rb/dry-schema

Default example breaks with dry-schema

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

Description

The documented way of handling default values breaks using a struct in an array in `dry-schema`. When I remove this logic from my struct base class, there are no errors.

This is the example from the docs:

```ruby
class User < Dry::Struct
transform_types do |type|
if type.default?
type.constructor do |value|
value.nil? ? Dry::Types::Undefined : value
end
else
type
end
end

attribute :name, Types::String
attribute :age, Types::Integer.default(18)
end
```

This is what happens when you use the example above in a schema:

```ruby
Class.new(Dry::Validation::Contract) { params { required(:list).array(User) } }.new.call(list: [])
NoMethodError: undefined method `visit_key' for #
Did you mean? visit_any
visit_and
visit_sum
visit_lax
from /Users/joel/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/dry-types-1.4.0/lib/dry/types/predicate_inferrer.rb:55:in `public_send'
```

Removing either the `default` call or the `transform_types` prevents the error from happening.

I think I recall seeing an issue in the past where having the `constructor` call after the `default` call is an issue.

How would you recommend resolving this?

P.S. I'm using dry-struct 1.3.0

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the documented dry-struct example inside a dry-schema contract using required(:list).array(User), then inspect the failing dry-types-1.4.0/lib/dry/types/predicate_inferrer.rb:55 call to visit_key. Done means the schema accepts the array without raising NoMethodError while preserving default handling; the payload names no repository test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.