Is there a way to validate int? and format? together


#1

This is my first time to try dry-validation, then I face an issue when I want to describe my schema to look like:

CardSchema = Dry::Validation.Schema do
  required(:exp_year).filled(int?, format?: /\A\d{4}\Z/)
end

Here is what the error message that I got TypeError: no implicit conversion of Integer into String.


#2

No, this is not supported. In dry-schema that will power dry-validation 1.0.0 I want to introduce this concept. We need a way to apply validation rules to input prior coercion, and this is something that dry-validation cannot do right now.


#3

Thank @solnic for the response, I can’t wait to see it in 1.0.0. In the mean time, here is what I come up:

required(:exp_year).filled(:int?)
validate(validate_exp_year: :exp_year) do |exp_year|
  exp_year.to_s.match?(/\A\d{4}\z/)
end