High-level Rules depending on nested data


#1

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 ?


#2

Yes:

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

#3

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?


#4

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