Define validation rules with `rule` and `each`
mainIn dry-validation, rules are used to perform complex validation logic that goes beyond simple schema checks. Rules are typically defined within a Contract.
rule: Used to define a rule for specific keys. You can pass macros to the rule to specify how it should behave.each: Used to define a validation function that is applied to every element of an array. Theeachmethod can take macros as arguments. The validation function is only applied if the schema checks for the array item passed successfully.
When a rule is executed, it creates an Evaluator instance. The block provided to the rule is evaluated within the context of this Evaluator, allowing you to use methods like key, value, and failure.
# Example of using rule and each within a contract
rule(:nums).each do |index:|
key([:number, index]).failure("must be greater than 0") if value < 0
end
rule(:nums).each(min: 3)
rule(address: :city) do
key.failure("oops") if value != 'Munich'
end