Module: Pragma::Operation::Validation::InstanceMethods
- Defined in:
- lib/pragma/operation/validation.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#build_contract(resource) ⇒ Pragma::Contract::Base
Builds the contract for the given resource, using the previously defined contract class.
-
#validate(validatable) ⇒ Boolean
Validates this operation on the provided contract or resource.
-
#validate!(validatable) ⇒ Object
Validates this operation on the provided contract or resource.
Instance Method Details
#build_contract(resource) ⇒ Pragma::Contract::Base
Builds the contract for the given resource, using the previously defined contract class.
This is just an instance-level alias of #build_contract. You should use this from inside the operation.
55 56 57 |
# File 'lib/pragma/operation/validation.rb', line 55 def build_contract(resource) self.class.build_contract(resource) end |
#validate(validatable) ⇒ Boolean
Validates this operation on the provided contract or resource.
If no contract has been defined for this operation, tries to call #validate on the resource. If the resource does not respond to #validate, returns true.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pragma/operation/validation.rb', line 67 def validate(validatable) contract = if self.class.contract_klass && validatable.is_a?(self.class.contract_klass) validatable else build_contract(validatable) end # rubocop:enable Metrics/LineLength if contract.is_a?(Pragma::Contract::Base) contract.validate(params) else contract.respond_to?(:validate) ? contract.validate : true end end |
#validate!(validatable) ⇒ Object
Validates this operation on the provided contract or resource. If the operation is not valid, responds with 422 Unprocessable Entity and an error body and halts the execution.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pragma/operation/validation.rb', line 86 def validate!(validatable) contract = if self.class.contract_klass && validatable.is_a?(self.class.contract_klass) validatable else build_contract(validatable) end # rubocop:enable Metrics/LineLength return if validate(contract) respond_with!( status: :unprocessable_entity, resource: { error_type: :contract_not_respected, error_message: 'The contract for this operation was not respected.', meta: { errors: contract.errors. } } ) end |