Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hanami-validations/dry-validations rule(...) #421

Open
Drowze opened this issue Mar 23, 2023 · 0 comments
Open

Support for hanami-validations/dry-validations rule(...) #421

Drowze opened this issue Mar 23, 2023 · 0 comments

Comments

@Drowze
Copy link

Drowze commented Mar 23, 2023

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 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant