Class: Hanami::Action::Params

Inherits:
BaseParams show all
Includes:
Validations::Form
Defined in:
lib/hanami/action/params.rb

Overview

A set of params requested by the client

It’s able to extract the relevant params from a Rack env of from an Hash.

There are three scenarios:

* When used with Hanami::Router: it contains only the params from the request
* When used standalone: it contains all the Rack env
* Default: it returns the given hash as it is. It's useful for testing purposes.

Since:

  • 0.1.0

Defined Under Namespace

Classes: Errors

Instance Attribute Summary collapse

Attributes inherited from BaseParams

#env

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseParams

#[], #each, #get

Constructor Details

#initialize(env) ⇒ Params

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

Initialize the params and freeze them.

Parameters:

  • env (Hash)

    a Rack env or an hash of params.

Since:

  • 0.1.0



159
160
161
162
163
164
165
166
# File 'lib/hanami/action/params.rb', line 159

def initialize(env)
  @env = env
  super(_extract_params)
  validation = validate
  @params = validation.to_h
  @errors = Errors.new(validation.messages)
  freeze
end

Instance Attribute Details

#errorsHash (readonly)

Returns structured error messages

Examples:

params.errors
  # => {
         :email=>["is missing", "is in invalid format"],
         :name=>["is missing"],
         :tos=>["is missing"],
         :age=>["is missing"],
         :address=>["is missing"]
       }

Returns:

  • (Hash)

Since:

  • 0.7.0



192
193
194
# File 'lib/hanami/action/params.rb', line 192

def errors
  @errors
end

Class Method Details

._base_rulesObject

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

This is a Hanami::Validations extension point

Since:

  • 0.7.0



114
115
116
117
118
# File 'lib/hanami/action/params.rb', line 114

def self._base_rules
  lambda do
    optional(:_csrf_token).filled(:str?)
  end
end

.params(&blk) ⇒ Object

Define params validations

Examples:

class Signup < Hanami::Action
  MEGABYTE = 1024 ** 2

  params do
    required(:first_name).filled(:str?)
    required(:last_name).filled(:str?)
    required(:email).filled?(:str?, format?: /\A.+@.+\z/)
    required(:password).filled(:str?).confirmation
    required(:terms_of_service).filled(:bool?)
    required(:age).filled(:int?, included_in?: 18..99)
    optional(:avatar).filled(size?: 1..(MEGABYTE * 3))
  end

  def handle(req, *)
    halt 400 unless req.params.valid?
    # ...
  end
end

Parameters:

  • blk (Proc)

    the validations definitions

See Also:

Since:

  • 0.7.0



147
148
149
# File 'lib/hanami/action/params.rb', line 147

def self.params(&blk)
  validations(&blk || -> {})
end

Instance Method Details

#deconstruct_keys::Hash

Pattern-matching support

Returns:

  • (::Hash)

Since:

  • 2.0.2



252
253
254
# File 'lib/hanami/action/params.rb', line 252

def deconstruct_keys(*)
  to_hash
end

#error_messages(error_set = errors) ⇒ Array

Returns flat collection of full error messages

Examples:

params.error_messages
  # => [
         "Email is missing",
         "Email is in invalid format",
         "Name is missing",
         "Tos is missing",
         "Age is missing",
         "Address is missing"
       ]

Returns:

  • (Array)

Since:

  • 0.7.0



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/hanami/action/params.rb', line 210

def error_messages(error_set = errors)
  error_set.each_with_object([]) do |(key, messages), result|
    k = Utils::String.titleize(key)

    msgs = if messages.is_a?(::Hash)
             error_messages(messages)
           else
             messages.map { |message| "#{k} #{message}" }
           end

    result.concat(msgs)
  end
end

#rawHash

Returns raw params from Rack env

Returns:

  • (Hash)

Since:

  • 0.3.2



173
174
175
# File 'lib/hanami/action/params.rb', line 173

def raw
  @input
end

#to_h::Hash Also known as: to_hash

Serialize validated params to Hash

Returns:

  • (::Hash)

Since:

  • 0.3.0



242
243
244
# File 'lib/hanami/action/params.rb', line 242

def to_h
  @params
end

#valid?TrueClass, FalseClass

Returns true if no validation errors are found, false otherwise.

Examples:

params.valid? # => true

Returns:

  • (TrueClass, FalseClass)

Since:

  • 0.7.0



233
234
235
# File 'lib/hanami/action/params.rb', line 233

def valid?
  errors.empty?
end