High-level Rules depending on nested data

I have these rules:

required(:pickOptions).schema do
    required(:value).value(included_in?: ['delivery', 'takeAway'])
end

optional(:address).schema do
    required(:value).filled(:str?)
    optional(:floor).filled(:str?)
    optional(:comment).filled(:str?)
end

Is it possible to add a rule that make address field required if the pickOptions.value is delivery ?

Yes:

rule(:address_required, [:address, [:pickOptions, :value]) do |a, v|
  v.eql?("delivery").then(a.filled?)
end
2 Likes

rule(:address_required, [:address, [:pickOptions, :value]) do |a, v|
v.eql?(“delivery”).then(a.filled?)
end

I tried to use the same approach with validate instead of a rule, and that doesn’t work unfortunately :frowning: Is there any possible workaround here for validate?

Solved by patching validate method with string id, *deps = opts.to_a.flatten(2)