dry-rb / dry-validation

Validation library with type-safe schemas and rules
https://dry-rb.org/gems/dry-validation
MIT License
1.34k stars 188 forks source link

no implicit conversion of Integer into String #689

Closed kadru closed 3 years ago

kadru commented 3 years ago

Describe the bug

When I use a format validation with a value expected to be a string but when a integer is passed raises an error no implicit conversion of Integer into String

To Reproduce

require "dry-validation"

class ValidatorWithFormat < Dry::Validation::Contract
  params do
    required(:field).filled(format?: /a+/).value(:string)
  end
end

validator = ValidatorWithFormat.new

puts "it fails" if validator.call({ "field" => 0 }).failure?
puts "it pass" if validator.call({ "field" => "a" }).success?

Expected behavior

The validation must fail

My environment

flash-gordon commented 3 years ago

correct usage would be

class ValidatorWithFormat < Dry::Validation::Contract
  params do
    required(:field).filled(:string, format?: /a+/)
  end
end