I’d like to validate this structure:
I want to check, for each value of the
paths hash (so, for any key of the hash) that it has a
get or a
put nested object with a
I didn’t find any way to apply a nested schema in this situation. Does somebody have a hint for me?
Thanks a lot, and sorry for my trivial question.
This is unsupported, you can track this feature request in dry-schema https://github.com/dry-rb/dry-schema/issues/37
As a workaround, you can write a rule for
:paths and iterate over keys there.
Thx for your quick answer @flash-gordon, and for the hint. I cannot find any example for this kind of situation, if you have a few minutes, could you please help me with that part (iterating over keys)?
I can guess that it starts with something like this, but I’m still confused on the validation part:
value.each do |(k, value)|
# How to check that `value` matches a specific schema, AND to set the errors at the correct path?
# Something like...
key([:paths, k]).failure('invalid') if #what ?
# But how to merge error messages of the nested schema test?
Thanks a lot.
There’s no standard solution for merging errors automatically, you’ll need to do it manually in the way that works for you. For example, I have code like this in one place
case validate_element.(value) # some other contract injected above
in Success then nil
errors.each do |path, messages|
, thanks for your answer, it helped a lot
I finally got this working:
value.each do |(key, value)|
PathSchema.call(value).errors.each do |error|