Class: Trane::ContractValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/trane/contract_validator.rb

Class Method Summary collapse

Class Method Details

.validate_response!(response_def, result, registry, mode:) ⇒ Object

Validate that a serialized result conforms to the response definition.

Parameters:

  • response_def (ResponseDefinition)
  • result (Hash)

    the serialized output

  • registry (Module)

    Trane::Registry

  • mode (Symbol)

    :raise or :log



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/trane/contract_validator.rb', line 13

def self.validate_response!(response_def, result, registry, mode:)
  violations = collect_violations(response_def.fields, result, registry, prefix: "")
  return if violations.empty?

  message = "Trane contract violations (status #{response_def.status}):\n  #{violations.join("\n  ")}"
  case mode
  when :raise
    raise ContractViolation, message
  when :log
    Trane.log_warning(message)
  end
end