Class: Treaty::Attribute::Validation::AttributeValidator

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

Overview

Validates and transforms individual attributes.

## Purpose

Acts as the main interface for attribute validation and transformation. Delegates option processing to OptionOrchestrator and handles nested validation.

## Responsibilities

  1. **Schema Validation** - Validates DSL definition correctness

  2. **Value Validation** - Validates runtime data values

  3. **Value Transformation** - Transforms values (defaults, etc.)

  4. **Name Transformation** - Provides target name (for ‘as:` option)

  5. **Nested Validation** - Delegates to NestedObjectValidator/NestedArrayValidator

## Usage

Used by Orchestrator to validate each attribute:

validator = AttributeValidator.new(attribute)
validator.validate_schema!
validator.validate_value!(value)
transformed = validator.transform_value(value)
target_name = validator.target_name

## Architecture

Delegates to:

  • ‘OptionOrchestrator` - Coordinates all option processors

  • ‘NestedObjectValidator` - Validates nested object structures

  • ‘NestedArrayValidator` - Validates nested array structures

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AttributeValidator

Creates a new attribute validator instance

Parameters:



43
44
45
46
47
48
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 43

def initialize(attribute)
  @attribute = attribute
  @option_orchestrator = OptionOrchestrator.new(attribute)
  @nested_object_validator = nil
  @nested_array_validator = nil
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



38
39
40
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 38

def attribute
  @attribute
end

#option_orchestratorObject (readonly)

Returns the value of attribute option_orchestrator.



38
39
40
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 38

def option_orchestrator
  @option_orchestrator
end

Instance Method Details

#target_nameSymbol

Gets the target attribute name

Returns:

  • (Symbol)

    The target name (or original if not transformed)



86
87
88
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 86

def target_name
  option_orchestrator.target_name
end

#transform_value(value) ⇒ Object

Transforms attribute value through all modifiers

Parameters:

  • value (Object)

    The value to transform

Returns:

  • (Object)

    Transformed value



72
73
74
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 72

def transform_value(value)
  option_orchestrator.transform_value(value)
end

#transforms_name?Boolean

Checks if attribute name is transformed

Returns:

  • (Boolean)

    True if name is transformed (as: option)



79
80
81
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 79

def transforms_name?
  option_orchestrator.transforms_name?
end

#validate_required!(value) ⇒ void

This method returns an undefined value.

Validates only the required constraint Used by nested transformers to validate presence before nested validation

Parameters:

  • value (Object)

    The value to validate

Raises:



107
108
109
110
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 107

def validate_required!(value)
  required_processor = option_orchestrator.processor_for(:required)
  required_processor&.validate_value!(value) if attribute.options.key?(:required)
end

#validate_schema!void

This method returns an undefined value.

Validates the attribute schema (DSL definition)

Raises:



54
55
56
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 54

def validate_schema!
  option_orchestrator.validate_schema!
end

#validate_type!(value) ⇒ void

This method returns an undefined value.

Validates only the type constraint Used by nested transformers to validate types before nested validation

Parameters:

  • value (Object)

    The value to validate

Raises:



96
97
98
99
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 96

def validate_type!(value)
  type_processor = option_orchestrator.processor_for(:type)
  type_processor&.validate_value!(value)
end

#validate_value!(value) ⇒ void

This method returns an undefined value.

Validates attribute value against all constraints

Parameters:

  • value (Object)

    The value to validate

Raises:



63
64
65
66
# File 'lib/treaty/attribute/validation/attribute_validator.rb', line 63

def validate_value!(value)
  option_orchestrator.validate_value!(value)
  validate_nested!(value) if attribute.nested? && !value.nil?
end