Class: Treaty::Attribute::Validation::Orchestrator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/attribute/validation/orchestrator/base.rb

Overview

Base orchestrator for validating and transforming data according to treaty definitions.

## Purpose

Coordinates the validation and transformation of request/response data for a specific API version. Processes all attributes, applying validations and transformations defined in the treaty DSL.

## Responsibilities

  1. **Attribute Processing** - Iterates through all defined attributes

  2. **Attribute Validation** - Validates each attribute’s value

  3. **Data Transformation** - Transforms values (defaults, renaming)

  4. **Nested Handling** - Delegates nested structures to NestedTransformer

  5. **Result Assembly** - Builds final transformed data structure

## Usage

Subclasses must implement:

  • ‘collection_of_attributes` - Returns attributes for this context (request/response)

Example:

orchestrator = Request::Orchestrator.new(version_factory: factory, data: params)
validated_data = orchestrator.validate!

## Special Case: object :_self

  • Normal object: ‘{ object_name: { … } }`

  • Self object (‘:_self`): Attributes merged directly into parent

## Architecture

Uses:

  • ‘AttributeValidator` - Validates individual attributes

  • ‘NestedTransformer` - Handles nested objects and arrays

The design separates concerns:

  • Orchestrator: High-level flow and attribute iteration

  • Validator: Individual attribute validation

  • Transformer: Nested structure transformation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_factory:, data: {}) ⇒ Base

Creates a new orchestrator instance



66
67
68
69
# File 'lib/treaty/attribute/validation/orchestrator/base.rb', line 66

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



51
52
53
# File 'lib/treaty/attribute/validation/orchestrator/base.rb', line 51

def data
  @data
end

#version_factoryObject (readonly)

Returns the value of attribute version_factory.



51
52
53
# File 'lib/treaty/attribute/validation/orchestrator/base.rb', line 51

def version_factory
  @version_factory
end

Class Method Details

.validate!Hash

Class-level factory method for validation Creates instance and calls validate!



58
59
60
# File 'lib/treaty/attribute/validation/orchestrator/base.rb', line 58

def self.validate!(...)
  new(...).validate!
end

Instance Method Details

#validate!Hash

Validates and transforms all attributes Iterates through attributes, processes them, handles :_self objects Skips attributes with false conditional (if/unless option)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/treaty/attribute/validation/orchestrator/base.rb', line 76

def validate! # rubocop:disable Metrics/MethodLength
  transformed_data = {}

  collection_of_attributes.each do |attribute|
    # Check if conditional (if/unless option) - skip attribute if condition evaluates to skip
    next unless should_process_attribute?(attribute)

    transformed_value = validate_and_transform_attribute!(attribute)

    if attribute.name == SELF_OBJECT && attribute.type == :object
      # For object :_self, merge nested attributes to root
      transformed_data.merge!(transformed_value)
    else
      transformed_data[attribute.name] = transformed_value
    end
  end

  transformed_data
end