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

Dry::Validation::Contract behaves different when defined with a params Dry::Schema::Params(parent: RawSchema) vs. params ParamsSchema #729

Open
postmodern opened this issue Apr 7, 2023 · 2 comments

Comments

@postmodern
Copy link

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

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

#<Dry::Validation::Result{:foo=>"a", :bar=>nil} errors={}>
#<Dry::Validation::Result{:foo=>"a", :bar=>nil} errors={}>

Actual behavior

#<Dry::Validation::Result{:foo=>"a", :bar=>""} errors={:bar=>["must be an integer"]}>
#<Dry::Validation::Result{:foo=>"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
@solnic
Copy link
Member

solnic commented Apr 8, 2023

Oh interesting. This does look like a bug because using params should preset processor to use params coercion regardless of the parent schema. Thanks for reporting this, good catch.

@postmodern
Copy link
Author

postmodern commented Apr 12, 2023

@solnic what's your rough estimation for how long it will take to find and fix this bug? Obviously being forced to duplicate the parent schema for both mapping in both HTTP params and a JSON Hash isn't ideal, although it's the only workaround I found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants