Calling a macro on a depply nested rule

Hello,

I was wondering if it’s possible to apply a macro to each element of an array that is itself nested in an array. Consider the following structure

optional(:rooms).array(:hash) do
  required(:guests).array(:hash, GuestSchema)
end

I’d like to apply a pre-existing macro to each guest in such a structure. If rooms wasn’t array, but a hash I could just do rule(rooms: :guests).each(:macro), but in case of array that doesn’t seem to work. I’m also unable to call a macro from within a rule(:rooms).each block.

Can you provide an executable script/test that shows what you want to achieve?

Of course, I was thinking something like this: https://github.com/dry-rb/dry-validation/compare/master...DawidJanczak:macro-on-deeply-nested-rule (extending previous example in that file) The spec will fail early on while evaluating the schema, but I hope the full scenario will highlight the issue I’m running into.

1 Like

@solnic does the above spec work for you?

Just in case someone else is running into this the way we ended up handling this is by replacing macros with methods accepting key and value and invoking those methods manually for each key and value in the nested list.

Yeah I get it now. We’re missing support for specifying such paths. addresses.phones does not denote that addresses is an array with elements that have phones key. This will be supported eventually so stay tuned.

Feel free to report a feature request about it so that it’s easier to track progress.

Thank you @solnic, I opened an issue here https://github.com/dry-rb/dry-validation/issues/578 :slight_smile:

1 Like