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