Support for hanami-validations/dry-validations `rule(...)`
Drowze opened this issue · comments
In dry-validations
and hanami-validations
we can define arbitrary rules, which are particularly handy when defining a validation that acts on multiple attributes, e.g.:
class EventContract < Dry::Validation::Contract
params do
required(:start_date).value(:date)
required(:end_date).value(:date)
end
rule(:end_date, :start_date) do
key.failure('must be after start date') if values[:end_date] < values[:start_date]
end
end
On hanami-controller
, I see that we have support for a params
block within actions, so we can wrap our request params within a hanami-validations params
block and validate accordingly.
...But I don't see any way to use arbitrary rules on hanami-controller. Is it possible on Hanami 2?
I know it was possible in Hanami 1, e.g.:
module Api
class MyAction
include Api::Action
params do
required(:start).filled(:int?)
required(:end).filled(:int?)
rule(dates: %i[start_date end_date]) do |_start, _end|
_end.gt?(value(:_start))
end
end
def call(params)
# ...
end
end
end
In Hanami 2 however, I'm currently simply using concrete classes inheriting from Dry::Validations::Contract and manually instantiating and validating the parameters, but I would like to see a more supported way if possible 🤔