Module: Hanami::Action::Validatable::ClassMethods Private

Defined in:
lib/hanami/action/validatable.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Validatable API class methods

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#contract(klass = nil, &block) ⇒ Object

Defines a validation contract for the params passed to Hanami::Action#call.

This feature isn’t mandatory, but is highly recommended for secure handling of params: because params come from an untrusted source, it’s good practice to filter these to only the keys and types required for your action’s use case.

The given block is evaluated inside a ‘Dry::Validation::Contract` class. This allows you to use all features of dry-validation contracts

The resulting contract becomes part of a dedicated params class for the action, inheriting from Params.

Instead of defining the params validation contract inline, you can alternatively provide a concrete params class, which should inherit from Params.

Examples:

Inline definition

require "hanami/controller"

class Signup < Hanami::Action
  contract do
    params do
      required(:first_name)
      required(:last_name)
      required(:email)
    end

    rule(:email) do
      # custom rule logic here
    end
  end

  def handle(req, *)
    puts req.params.class            # => Signup::Params
    puts req.params.class.superclass # => Hanami::Action::Params

    puts req.params[:first_name]     # => "Luca"
    puts req.params[:admin]          # => nil
  end
end

Concrete class

require "hanami/controller"

class SignupParams < Hanami::Action::Params
  contract do
    params do
      required(:first_name)
      required(:last_name)
      required(:email)
    end

    rule(:email) do
      # custom rule logic here
    end
  end
end

class Signup < Hanami::Action
  params SignupParams

  def handle(req, *)
    puts req.params.class            # => SignupParams
    puts req.params.class.superclass # => Hanami::Action::Params

    req.params[:first_name]          # => "Luca"
    req.params[:admin]               # => nil
  end
end

Parameters:

  • klass (Class, nil) (defaults to: nil)

    a Hanami::Action::Params subclass

  • block (Proc)

    the params schema definition

Returns:

  • void

See Also:

Since:

  • 2.2.0



192
193
194
195
196
# File 'lib/hanami/action/validatable.rb', line 192

def contract(klass = nil, &block)
  contract_class = klass || Class.new(Dry::Validation::Contract, &block)

  config.contract_class = contract_class
end

#params(klass = nil, &block) ⇒ Object

Defines a validation schema for the params passed to Hanami::Action#call.

This feature isn’t mandatory, but is highly recommended for secure handling of params: because params come from an untrusted source, it’s good practice to filter these to only the keys and types required for your action’s use case.

The given block is evaluated inside a ‘params` schema of a `Dry::Validation::Contract` class. This constrains the validation to simple structure and type rules only. If you want to use all the features of dry-validation contracts, use #contract instead.

The resulting contract becomes part of a dedicated params class for the action, inheriting from Params.

Instead of defining the params validation schema inline, you can alternatively provide a concrete params class, which should inherit from Params.

Examples:

Inline definition

require "hanami/controller"

class Signup < Hanami::Action
  params do
    required(:first_name)
    required(:last_name)
    required(:email)
  end

  def handle(req, *)
    puts req.params.class            # => Signup::Params
    puts req.params.class.superclass # => Hanami::Action::Params

    puts req.params[:first_name]     # => "Luca"
    puts req.params[:admin]          # => nil
  end
end

Concrete class

require "hanami/controller"

class SignupParams < Hanami::Action::Params
  params do
    required(:first_name)
    required(:last_name)
    required(:email)
  end
end

class Signup < Hanami::Action
  params SignupParams

  def handle(req, *)
    puts req.params.class            # => SignupParams
    puts req.params.class.superclass # => Hanami::Action::Params

    req.params[:first_name]          # => "Luca"
    req.params[:admin]               # => nil
  end
end

Parameters:

  • klass (Class, nil) (defaults to: nil)

    a Hanami::Action::Params subclass

  • block (Proc)

    the params schema definition

Returns:

  • void

See Also:

Since:

  • 0.3.0



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hanami/action/validatable.rb', line 98

def params(klass = nil, &block)
  contract_class =
    if klass.nil?
      Class.new(Dry::Validation::Contract) { params(&block) }
    elsif klass < Params
      # Handle subclasses of Hanami::Action::Params.
      klass._contract.class
    else
      klass
    end

  config.contract_class = contract_class
end