Class: Treaty::Attribute::Validation::AttributeValidator
- Inherits:
-
Object
- Object
- Treaty::Attribute::Validation::AttributeValidator
- 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
-
**Schema Validation** - Validates DSL definition correctness
-
**Value Validation** - Validates runtime data values
-
**Value Transformation** - Transforms values (defaults, etc.)
-
**Name Transformation** - Provides target name (for ‘as:` option)
-
**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
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#option_orchestrator ⇒ Object
readonly
Returns the value of attribute option_orchestrator.
Instance Method Summary collapse
-
#initialize(attribute) ⇒ AttributeValidator
constructor
Creates a new attribute validator instance.
-
#target_name ⇒ Symbol
Gets the target attribute name.
-
#transform_value(value) ⇒ Object
Transforms attribute value through all modifiers.
-
#transforms_name? ⇒ Boolean
Checks if attribute name is transformed.
-
#validate_required!(value) ⇒ void
Validates only the required constraint Used by nested transformers to validate presence before nested validation.
-
#validate_schema! ⇒ void
Validates the attribute schema (DSL definition).
-
#validate_type!(value) ⇒ void
Validates only the type constraint Used by nested transformers to validate types before nested validation.
-
#validate_value!(value) ⇒ void
Validates attribute value against all constraints.
Constructor Details
#initialize(attribute) ⇒ AttributeValidator
Creates a new attribute validator instance
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
#attribute ⇒ Object (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_orchestrator ⇒ Object (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_name ⇒ Symbol
Gets the target attribute name
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
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
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
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..key?(:required) end |
#validate_schema! ⇒ void
This method returns an undefined value.
Validates the attribute schema (DSL definition)
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
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
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 |