Class: Treaty::Action::Response::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/action/response/validator.rb

Overview

Validates service response data against schema.

Purpose

Validates response data from service execution against the response schema defined in the treaty version. Ensures service output matches the documented API contract.

Usage

Called internally by:

  • Versions::Execution::Response (after service execution)

Validation Flow

  1. Check if response schema is defined
  2. Create dynamic Orchestrator with version's response attributes
  3. Run validation pipeline (validate + transform)
  4. Return validated/transformed data or raise error

Error Handling

Raises Treaty::Exceptions::Validation with detailed messages including attribute path and specific validation failures.

Difference from Request::Validator

  • Validates service output, not controller params
  • Response attributes are optional by default
  • Used after service execution, not before

Example

# Typically called via class method:
validated = Response::Validator.validate!(
version_factory: version_factory,
response_data: service_result
)

# validated contains transformed response for client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_factory:, response_data: {}) ⇒ Validator

Creates new validator instance

Parameters:



63
64
65
66
# File 'lib/treaty/action/response/validator.rb', line 63

def initialize(version_factory:, response_data: {})
  @version_factory = version_factory
  @response_data = response_data
end

Class Method Details

.validate!(version_factory:, response_data: {}) ⇒ Hash

Validates response data

Parameters:

Returns:

  • (Hash)

    Validated and transformed response

Raises:



54
55
56
# File 'lib/treaty/action/response/validator.rb', line 54

def validate!(version_factory:, response_data: {})
  new(version_factory:, response_data:).validate!
end

Instance Method Details

#validate!Hash

Runs validation pipeline

Returns:

  • (Hash)

    Validated and transformed response

Raises:



72
73
74
# File 'lib/treaty/action/response/validator.rb', line 72

def validate!
  validate_response_attributes!
end