dry-rb / dry-rb/dry-validation
`Dry::Validation::Contract` behaves different when defined with a `params Dry::Schema::Params(parent: RawSchema)` vs. `params ParamsSchema`
- Dominant language
- Ruby
- Stars
- 1.4k
- Forks
- 195
- PR merge metrics
- No merged PRs in 30d
Description
## Describe the bug
When `Dry::Validation::Contract` contains `params Dry::Schema::Params(parent: RawSchema)` (where `RawSchema` is defined using `Dry::Schema.define do ... end`), it fails to coerce empty Strings into Integers. However, if I define a `ParamsSchema` using `Dry::Schema::Params() do ... end` containing the same param definitions, and add `params ParamsSchema` to my `Dry::Validation::Contract` class, it works as expected.
## To Reproduce
```ruby
require 'dry/types'
require 'dry/schema'
require 'dry/validation'
module Types
include Dry::Types()
end
RawSchema = Dry::Schema.define do
required(:foo).filled(:string)
optional(:bar).maybe(:integer)
end
ParamsSchema = Dry::Schema::Params() do
required(:foo).filled(:string)
optional(:bar).maybe(:integer)
end
class WrappedParamsSchemaValidation < Dry::Validation::Contract
params Dry::Schema::Params(parent: RawSchema)
end
class ParamsSchemaValidation < Dry::Validation::Contract
params ParamsSchema
end
params = {"foo" => "a", "bar" => ""}
p WrappedParamsSchemaValidation.new.call(params)
p ParamsSchemaValidation.new.call(params)
```
## Expected behavior
```
#"a", :bar=>nil} errors={}>
#"a", :bar=>nil} errors={}>
```
## Actual behavior
```
#"a", :bar=>""} errors={:bar=>["must be an integer"]}>
#"a", :bar=>nil} errors={}>
```
## My environment
- Affects my production application: **NO** (the app is not running in production)
- Ruby version: `ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux]`
- dry-types: 1.7.1
- dry-schema: 1.13.1
- dry-validation: 1.10.0
Contributor guide
Assessment
This issue has not been assessed yet.