When I validate an empty parameter against a form schema with a max_size constraint, I get multiple error messages returned.
require "dry-validation"
form = Dry::Validation.Form do
required(:param).filled(max_size?: 5)
end
puts form.({ param: '' }).messages
# {:param=>["must be filled", "size cannot be greater than 5"]}
I would expect that only the first error message would be returned here. Is there another way of expressing the logic I want so that the errors line up with my expectations?
What you’re seeing here is a combination of error messages and hints. Hints are there to let you know about validations that couldn’t run yet because of a prior failure. This enables you e.g. to give the user a full set of notes about that field so they don’t have to see it fail validation multiple times before they figure out the correct value.
As of dry-validation 0.10.0, you can actually access the error messages and hints separately, if you don’t want to see them combined like in your example:
UserSchema = Dry::Validation.Form do
required(:login).filled(size?: 3..64)
required(:age).filled(:int?, gt?: 18)
end
UserSchema.(login: '', age: 17).errors
# {:login=>["must be filled"], :age=>["must be greater than 18"]}
UserSchema.(login: '', age: 17).hints
# {:login=>["length must be within 3 - 64"], :age=>[]}
I pulled this example from our recent blog post announcing dry-v 0.10.0 and others.
So hopefully this should give you all the flexibility you need to work with exactly the set of messages you want!