dry-rb / dry-rb/dry-schema

Exception silenced when raised from nested schema

Open
#445 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

Behavior is inconsistent between "flat" and nested schema when an exception occur.

## To reproduce
```ruby
NestedSchema = Dry::Schema.Params do
required(:value).filter(format?: /^\d+$/).value(:float)
end

ParentSchema = Dry::Schema.Params do
required(:data).hash(NestedSchema)
end

# KO: filter() should fail as it expect a string, got no exception
ParentSchema.call({data: {value: 123}})
=> #{:value=>123.0}} errors={} path=[]>
```

## Expected behavior
I would expect the exception to be forwarded from the nested schema and not silenced as it is the case with a "flat" schema:

```ruby
FlatSchema = Dry::Schema.Params do
required(:data).hash do
required(:value).filter(format?: /^\d+$/).value(:float)
end
end

# OK: filter() fails as it expects a string and gets a numeric -> exception
FlatSchema.call({data: {value: 123}})
=> [..]/.gem/ruby/3.0.2/gems/dry-logic-1.5.0/lib/dry/logic/predicates.rb:226:in `match?': no implicit conversion of Integer into String (TypeError)
[...]
```

Additionaly we can wonder why the `filter` call fails if we provide a value that is actually matching the coercion.

### My environment
Affects my production application: YES
Ruby version: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233)
OS: Fedora 37 beta [aarch64-linux]

Contributor guide

Open the contributing guide

Research direction

Start by running the NestedSchema and FlatSchema examples from the issue and compare their calls with {data: {value: 123}}. Trace how the nested schema handles the filter exception versus the flat schema, and consider the reported coercion case; done means the behavior is consistent and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend-api-design
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.