Class: Lutaml::Model::AttributeValidator
- Inherits:
-
Object
- Object
- Lutaml::Model::AttributeValidator
- Defined in:
- lib/lutaml/model/attribute_validator.rb
Overview
Handles validation logic for Lutaml::Model::Attribute.
Extracted from Attribute class to provide focused validation concerns and better separation of responsibilities.
Instance Attribute Summary collapse
-
#attribute ⇒ Attribute
readonly
The attribute being validated.
Instance Method Summary collapse
-
#ensure_required?(value) ⇒ true
Validate that required attributes have values.
-
#initialize(attribute) ⇒ AttributeValidator
constructor
Initialize a new validator for an attribute.
-
#valid_collection?(value, caller) ⇒ true
Validate collection count is within range.
-
#valid_pattern?(value, resolved_type) ⇒ true
Check if value matches the pattern (for String types).
-
#valid_value?(value) ⇒ true
Check if value is in the allowed enum values.
-
#validate!(value, register) ⇒ true
Validate a value for the attribute.
-
#validate_collection_range! ⇒ void
Validate collection range configuration.
-
#validate_polymorphic!(value, resolved_type) ⇒ true
Validate polymorphic type.
Constructor Details
#initialize(attribute) ⇒ AttributeValidator
Initialize a new validator for an attribute
16 17 18 |
# File 'lib/lutaml/model/attribute_validator.rb', line 16 def initialize(attribute) @attribute = attribute end |
Instance Attribute Details
#attribute ⇒ Attribute (readonly)
Returns The attribute being validated.
11 12 13 |
# File 'lib/lutaml/model/attribute_validator.rb', line 11 def attribute @attribute end |
Instance Method Details
#ensure_required?(value) ⇒ true
Validate that required attributes have values
56 57 58 59 60 61 62 |
# File 'lib/lutaml/model/attribute_validator.rb', line 56 def ensure_required?(value) return true unless attribute.[:required] return false if value.nil? return false if value.respond_to?(:empty?) && value.empty? true end |
#valid_collection?(value, caller) ⇒ true
Validate collection count is within range
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/lutaml/model/attribute_validator.rb', line 113 def valid_collection?(value, caller) if attribute.collection_instance?(value) && !attribute.collection? raise Lutaml::Model::CollectionTrueMissingError.new( attribute.name, caller, ) end return true unless attribute.collection? # Allow any value for unbounded collections return true if attribute.collection == true unless (Utils.uninitialized?(value) && attribute.resolved_collection.min.zero?) || attribute.collection_instance?(value) raise Lutaml::Model::CollectionCountOutOfRangeError.new( attribute.name, value, attribute.collection, ) end return true unless attribute.resolved_collection.is_a?(Range) valid_collection_count?(value) end |
#valid_pattern?(value, resolved_type) ⇒ true
Check if value matches the pattern (for String types)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lutaml/model/attribute_validator.rb', line 91 def valid_pattern?(value, resolved_type) return true unless resolved_type == Lutaml::Model::Type::String return true unless attribute.pattern unless attribute.pattern.match?(value) raise Lutaml::Model::PatternNotMatchedError.new( attribute.name, attribute.pattern, value, ) end true end |
#valid_value?(value) ⇒ true
Check if value is in the allowed enum values
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lutaml/model/attribute_validator.rb', line 69 def valid_value?(value) return true if value.nil? && attribute.singular? return true unless attribute.enum? return true if Utils.uninitialized?(value) unless valid_value_check?(value) raise Lutaml::Model::InvalidValueError.new( attribute.name, value, attribute.enum_values, ) end true end |
#validate!(value, register) ⇒ true
Validate a value for the attribute
Performs all validation checks including:
-
Required value validation
-
Enum value validation
-
Collection range validation
-
Pattern validation
-
Polymorphic type validation
-
Custom validations
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lutaml/model/attribute_validator.rb', line 38 def validate!(value, register) ensure_required?(value) value = attribute.default(register) if value.nil? resolved_type = attribute.type(register) valid_value?(value) && valid_collection?(value, attribute) && valid_pattern?(value, resolved_type) && validate_polymorphic!(value, resolved_type) && execute_validations!(value) end |
#validate_collection_range! ⇒ void
This method returns an undefined value.
Validate collection range configuration
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/lutaml/model/attribute_validator.rb', line 157 def validate_collection_range! range = attribute.[:collection] return if range == true return if attribute.custom_collection? unless range.is_a?(Range) raise ArgumentError, "Invalid collection range: #{range}" end validate_range!(range) end |
#validate_polymorphic!(value, resolved_type) ⇒ true
Validate polymorphic type
146 147 148 149 150 151 |
# File 'lib/lutaml/model/attribute_validator.rb', line 146 def validate_polymorphic!(value, resolved_type) return true if validate_polymorphic(value, resolved_type) raise Lutaml::Model::PolymorphicError.new(value, attribute., resolved_type) end |