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
Instance Method Summary collapse
-
#contract(klass = nil, &block) ⇒ Object
Defines a validation contract for the params passed to Hanami::Action#call.
-
#params(klass = nil, &block) ⇒ Object
Defines a validation schema for the params passed to Hanami::Action#call.
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.
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.
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 |