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
Instead of defining the contract inline, you can alternatively provide a concrete ‘Dry::Validation::Contract` subclass.
158 159 160 161 162 |
# File 'lib/hanami/action/validatable.rb', line 158 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.
Instead of defining the params validation schema inline, you can alternatively provide a concrete ‘Dry::Validation::Contract` subclass.
83 84 85 86 87 |
# File 'lib/hanami/action/validatable.rb', line 83 def params(klass = nil, &block) contract_class = klass || Class.new(Dry::Validation::Contract) { params(&block) } config.contract_class = contract_class end |