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!