I’m using dry-validation to validate the parameters for an API, and I’d like to know if there’s a way to add custom validation block errors to a field. For example:
UserSchema = Dry::Validation.Params do
configure do
option :ids
def self.messages
super.merge(
en: { errors: { valid_id: 'id is not valid' } }
)
end
end
required(:id).filled(:int?)
validate(valid_id: :id) do |id|
ids.include?(id)
end
end
schema = UserSchema.with(ids: [1, 2, 3])
schema.(id: 4).errors
# current behaviour
# => {:valid_id=>["id is not valid"]}
# wanted behaviour
# => {:id=>["id is not valid"]}
UserSchema = Dry::Validation.Params do
configure do
option :ids
def self.messages
super.merge(
en: { errors: { id: 'id is not valid' } }
)
end
end
required(:id).filled(:int?)
validate(id: :id) do |id|
ids.include?(id)
end
end
schema = UserSchema.with(ids: [1, 2, 3])
puts schema.(id: 4).errors