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

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/entity/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:



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

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.



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

def attribute
  @attribute
end

#option_orchestratorObject (readonly)

Returns the value of attribute option_orchestrator.



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

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)



88
89
90
# File 'lib/treaty/entity/attribute/validation/attribute_validator.rb', line 88

def target_name
  option_orchestrator.target_name
end

#transform_value(value, root_data = {}) ⇒ Object

Transforms attribute value through all modifiers

Parameters:

  • value (Object)

    The value to transform

  • root_data (Hash) (defaults to: {})

    Full raw data from root level (used by computed modifier)

Returns:

  • (Object)

    Transformed value



74
75
76
# File 'lib/treaty/entity/attribute/validation/attribute_validator.rb', line 74

def transform_value(value, root_data = {})
  option_orchestrator.transform_value(value, root_data)
end

#transforms_name?Boolean

Checks if attribute name is transformed

Returns:

  • (Boolean)

    True if name is transformed (as: option)



81
82
83
# File 'lib/treaty/entity/attribute/validation/attribute_validator.rb', line 81

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:



109
110
111
112
# File 'lib/treaty/entity/attribute/validation/attribute_validator.rb', line 109

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:



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

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:



98
99
100
101
# File 'lib/treaty/entity/attribute/validation/attribute_validator.rb', line 98

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:



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

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