2 checks in Hanami + dry validation

I am trying to validate a valid character with Hanami (which sits on top of dry-validation ).

The point is this: how to check the mapping associated with two fields: email + password?

I read about custom predicates, but they only seem to be for each field. Another concept is the rule, but according to the examples, it does not connect 2 things as I need.

Here is my code:

module Web::Controllers::Sessions
  class Create
    include Web::Action

    expose :validation # my standard way to show errors in the template

    def call(params)
      @validation = SigninValidator.new(params[:user]).validate
      if @validation.success? 
      # more stuff here
    end
  end
end

class SigninValidator
  include Hanami::Validations::Form

  validations do
    required(:email) { format?(EMAIL_REGEX)}
    required(:password).filled(:str?)

    # I GOT CONFUSED HERE
    # how could I use someting like a repository and relate something like 
    # predicate + message for "email or password doesn't match"

  end
end

Unfortunately, the validation section in the Hanami manual is empty, and I could not find a solution considering the sources (hanami validation and dry-validation).

Any help would be appreciated.

+4
source share
2

. - :

validations do
  required(:email) { format?(EMAIL_REGEX)}
  required(:password).filled(:str?)

  rule(:email_and_password: [:email, :password]) do |email, password|
    # Example, do what you need here
    # Only dry-rb rules are valid here
    email.filled? & password.filled?
  end

  # Or

  validate(:email_and_password: [:email, :password]) do |email, password|
    # Example, do what you need here
    # Any Ruby code is valid here
    email.filled? && password.filled?
  end
end

, :email :password , rule validate.

+2

, , , , / , ? , , . , , . , , .

m45t3r , , , , .

, , , , ! , , , .

0

Source: https://habr.com/ru/post/1679166/


All Articles