Module: Pragma::Operation::Validation::InstanceMethods

Defined in:
lib/pragma/operation/validation.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#after_validation(result) ⇒ Object

Runs after validation is done.

Parameters:

  • result (Boolean)

    the result of the validation



99
100
# File 'lib/pragma/operation/validation.rb', line 99

def after_validation(result)
end

#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.

Parameters:

  • resource (Object)

Returns:

  • (Pragma::Contract::Base)

See Also:

  • contract


47
48
49
50
51
52
# File 'lib/pragma/operation/validation.rb', line 47

def build_contract(resource)
  contract_klass = compute_contract_klass(resource)
  return resource unless contract_klass

  contract_klass.new(resource)
end

#respond_with_validation_errors(validatable) ⇒ Object

Sets a response suitable for reporting validation errors.

The response will be a 422 Unprocessable Entity, contain the error_type, error_message and meta keys. meta.errors will contain the validation errors.

Parameters:

  • validatable (Object)

    a validatable object



108
109
110
# File 'lib/pragma/operation/validation.rb', line 108

def respond_with_validation_errors(validatable)
  respond_with validation_errors_response(validatable)
end

#respond_with_validation_errors!(validatable) ⇒ Object

Same as #respond_with_validation_errors, but also halts the execution of the operation.

Parameters:

  • validatable (Object)

    a validatable object

See Also:



117
118
119
# File 'lib/pragma/operation/validation.rb', line 117

def respond_with_validation_errors!(validatable)
  respond_with! validation_errors_response(validatable)
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.

Parameters:

  • validatable (Object|Pragma::Contract::Base)

    contract or resource

Returns:

  • (Boolean)

    whether the operation is valid



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pragma/operation/validation.rb', line 62

def validate(validatable)
  # rubocop:disable Metrics/LineLength
  contract = if Object.const_defined?('Pragma::Contract::Base') && validatable.is_a?(Pragma::Contract::Base)
    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.tap do |result|
    after_validation(result)
  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.

Parameters:

  • validatable (Object|Pragma::Contract::Base)

    contract or resource



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pragma/operation/validation.rb', line 84

def validate!(validatable)
  # rubocop:disable Metrics/LineLength
  contract = if Object.const_defined?('Pragma::Contract::Base') && validatable.is_a?(Pragma::Contract::Base)
    validatable
  else
    build_contract(validatable)
  end
  # rubocop:enable Metrics/LineLength

  respond_with_validation_errors!(contract) unless validate(contract)
end