Dry-validation multiple rules for same field


#1

Hello!

I’m still fairly new to dry-validation, so please bear with me…

Let’s say I have a field called “ratings” that must be an array of 0 < N <= 2 objects, each of which must look like { product_id: 5, rating: “good” }. What’s the best way to capture this in dry-validation such that validating an array with more than two elements that has objects missing the “rating” key will return both error messages (i.e. “size cannot be greater than 2” and “{“0”:{“rating”:[“is missing”]},“1”:{“rating”:[“is missing”]}}”) for the “ratings” field? I’ve tried lots of variations such as the following, but cannot get it to return both errors; only the error msg for the first failing rule encountered:

required(:ratings) do
  array? & max_size?(2) & each do
    schema do
      required(:product_id).filled(:int?)
      required(:rating).filled(:str?)
    end
  end
end
# results in only getting back the "size cannot be greater than 2" error
# message since the size check comes before the each block.

I realize I could make a differently named rule for checking one of the rules, but I’d rather have both errors under the “ratings” key.

Not sure if I’m just missing something or if what I’m after is impossible/doesn’t make sense/is an anti-pattern.

Thanks!


#2

I think the concept you are looking for is called hints, which are messages relating to validation rules which were not run because earlier validations failed. They are described here: http://dry-rb.org/gems/dry-validation/basics/working-with-schemas/

I’ve never used them with an array though, so you’ll have to try and see if it works. I kind of doubt it, because I’d guess it’s never going to validate any of your individual hashes if the outer array fails validation. The hints seem like they are intended for a single field with multiple validations where you just want to inform the user of all validation rules at once, while still getting the performance benefit of short-circuit validations. What you really want is to not short-circuit the validation of the internal array, and I’m not sure there is a way to do that or not.