Class: Lutaml::Model::AttributeValidator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AttributeValidator

Initialize a new validator for an attribute

Parameters:

  • attribute (Attribute)

    The attribute to validate



16
17
18
# File 'lib/lutaml/model/attribute_validator.rb', line 16

def initialize(attribute)
  @attribute = attribute
end

Instance Attribute Details

#attributeAttribute (readonly)

Returns The attribute being validated.

Returns:

  • (Attribute)

    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

Parameters:

  • value (Object)

    The value to check

Returns:

  • (true)

    if validation passes

Raises:



56
57
58
59
60
61
62
# File 'lib/lutaml/model/attribute_validator.rb', line 56

def ensure_required?(value)
  return true unless attribute.options[: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

Parameters:

  • value (Object)

    The value to check

  • caller (Object)

    The calling context (usually the attribute)

Returns:

  • (true)

    if validation passes

Raises:



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)

Parameters:

  • value (Object)

    The value to check

  • resolved_type (Class)

    The resolved type

Returns:

  • (true)

    if value matches pattern or pattern not applicable

Raises:



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

Parameters:

  • value (Object)

    The value to check

Returns:

  • (true)

    if value is valid or not an enum

Raises:



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

Parameters:

  • value (Object)

    The value to validate

  • register (Symbol, Register, nil)

    The register for type resolution

Returns:

  • (true)

    if validation passes

Raises:



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

Raises:

  • (ArgumentError)

    if collection range is invalid



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.options[: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

Parameters:

  • value (Object)

    The value to check

  • resolved_type (Class)

    The resolved type

Returns:

  • (true)

    if validation passes

Raises:



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.options,
                                            resolved_type)
end