@flash-gordon@adam12 that makes sense to not mix schema with validations. How would you solve reusing validation rules across different contracts?
Let’s say we have:
Delivery = Dry::Schema.Params do
required(:id).filled(:integer)
# more schema restrictions
end
class DeliveriesAddition < Dry::Validation::Contract
params do
required(:deliveries).value(:array, min_size?: 1).each(Delivery)
end
rule(:deliveries).each do
key.failure("from_after must be before from_before") if value[:from_after] >= value[:from_before]
# some more rules
end
end
class DeliveriesUpdate < Dry::Validation::Contract
params do
required(:deliveries).value(:array, min_size?: 1).each(Delivery)
end
rule(:deliveries).each do
key.failure("from_after must be before from_before") if value[:from_after] >= value[:from_before]
# some more rules that are the same as in DeliveriesAddition
end
end
The goal here would be to not repeat the custom rules in every contract file but just use some common “mixin”.
I can see macros dry-rb - dry-validation v1.6 - Macros but looks like it would require defining every single rule by some custom name. Is it a way to go or is there a better way?