Hey,
in my current project I have lots of use-cases, where I would need to name base rules. Here’s some pseudo code:
class BookingContract < Dry::Validation::Contract
params do
required(:reference_number).value(:string)
required(:type).value(:string)
...
end
rule(name: :must_be_in_external_system, keys: [:reference_number, :type]) do |context:|
payment = ext_payment_api.find(type: values[:type], reference_number: values[:reference_number])
unless (payment.nil?)
context[:payment] = payment
else
base.failure("no payment available in external system!")
end
end
rule(name: :payment_has_to_be_paid) do |context:|
next if base_rule_error?(:must_be_in_external_system)
base.failure("payment has not yet been paid") if context[:payment].paid?
end
rule(name: :correct_payment_type) do |context:|
next if base_rule_error?(:must_be_in_external_system)
base.failure("payment has to be of type TEST") if context[:payment].type != "TEST"
end
end
What do you think about the idea? I think it would offer new options and make rules more readable.
Thanks
best regards