dry-rb / dry-schema

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

Sum hash types allow any hash to pass coercion #446

Closed unkmas closed 1 year ago

unkmas commented 1 year ago

Describe the bug

Schema containing sum hash types allow any hash to pass coercion.

To Reproduce

require 'dry-schema'

module Types
  include Dry.Types()
end

FooType = Types::Hash.schema(foo: Types::String)
BarType = Types::Hash.schema(bar: Types::String)

FooBarType = FooType | BarType

FooBarSchema = Dry::Schema.Params do
  required(:foobar).filled(FooBarType)
end

data = {foobar: {something_wrong: true}}

FooBarSchema.call(data)
# => #<Dry::Schema::Result{:foobar=>{:something_wrong=>true}} errors={} path=[]>

# Type itself works fine
FooBarType[data[:foobar]]
# => `block in resolve_missing_keys': :bar is missing in Hash input (Dry::Types::MissingKeyError)

# Hash schemas without sums also works fine:
FooSchema = Dry::Schema.Params do
  required(:foobar).filled(FooType)
end

FooSchema.call(data)
#=> #<Dry::Schema::Result{:foobar=>{}} errors={:foobar=>["must be filled"]} path=[]>

Expected behavior

Coercion should happen for sum hash types.

My environment

solnic commented 1 year ago

This is now fixed in 1.13.1