dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
425 stars 110 forks source link

Type coercion doesn't work #383

Closed sdogruyol closed 3 years ago

sdogruyol commented 3 years ago

Describe the bug

Type coercion doesn't work for Dry::Schema.Params

To Reproduce

IndexSchema = Dry::Schema.Params do
  optional(:filter).hash do
    optional(:template).filled(:bool?)
  end
end

Here's the following error, template is not cast into boolean

<Dry::Schema::Result{filter=>{:template=>"true"}} errors={:filter=>{:template=>["must be boolean"]}} path=[]>

Expected behavior

Type coercion should work by default

My environment

flash-gordon commented 3 years ago

:bool? is a predicate, it checks if the value is literally true or false. Type names don't end with a question mark:

IndexSchema = Dry::Schema.Params do
  optional(:filter).hash do
    optional(:template).filled(:bool)
  end
end

dry-schema> IndexSchema.('filter' => { 'template' => 'true' })
# => #<Dry::Schema::Result{:filter=>{:template=>true}} errors={} path=[]>