Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params type conversion is ignored #174

Open
kminevskiy opened this issue Jul 25, 2019 · 1 comment

Comments

@kminevskiy
Copy link

commented Jul 25, 2019

Describe the bug

To Reproduce

# Custom type
module CustomTypes
  include Dry.Types()

  Person = Nominal::Any.constructor(&:to_h)
end

# Person class
class Person
  def to_h
    { name: 'Luke', age: 21 }
  end
end

# Person schema
PersonSchema = Dry::Schema.Params do
  required(:name).filled(:string)
  required(:age).value(:integer, gt?: 18)
end

# Contract
class NewUserContract < Dry::Validation::Contract
  params do
    required(:person).type(CustomTypes::Person).schema(PersonSchema)
  end
end

Then, instead of converting params using custom type Person, the error is being raised:

luke = Person.new

contract = NewUserContract.new

contract.call(person: luke)

undefined method `key?' for #<Person:0x00007fdd48873390>

Converting object to hash explicitly works, but then the type(Type) portion of the contract / schema definition is useless.

luke = Person.new

contract = NewUserContract.new

contract.call(person: luke.to_h)

#<Dry::Validation::Result{:person=>{:name=>"Luke", :age=>21}} errors={}>

Expected behavior

luke = Person.new

contract = NewUserContract.new

contract.call(person: luke)

#<Dry::Validation::Result{:person=>{:name=>"Luke", :age=>21}} errors={}>

Your environment

  • Affects my production application: Yes
  • Ruby version: 2.6.3
  • OS: 10.14.6

@solnic solnic added the 🐛 bug label Jul 26, 2019

@solnic solnic added this to the 1.3.2 milestone Jul 26, 2019

@solnic

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

This is a tricky problem to solve. The reason why is that both hash and schema methods are designed to work with hashes and they set their own type which, unfortunately, silently overwrites any potential custom type that was previously set either via type or value methods.

I think this specific issue only manifests a general problem we have here which is respecting custom types. I need to "zoom out" and see how it could be solved and as a result, this issue will be solved too.

@solnic solnic modified the milestones: 1.3.2, 1.4.0 Aug 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.