Class: Lutaml::Xsd::Validation::Facets::MaxLengthFacetValidator

Inherits:
FacetValidator
  • Object
show all
Defined in:
lib/lutaml/xsd/validation/facets/max_length_facet_validator.rb

Overview

Validates values against XSD maxLength facet

The maxLength facet specifies the maximum length of a value. For strings, it’s the maximum number of characters. For lists, it’s the maximum number of items.

Examples:

Validating maximum length

facet = Lutaml::Xsd::MaxLength.new(value: "5")
validator = MaxLengthFacetValidator.new(facet)
validator.valid?("hi")     # => true (2 <= 5)
validator.valid?("hello!")  # => false (6 > 5)
validator.error_message("hello!")
# => "Value length 6 exceeds maximum length 5"

Instance Attribute Summary

Attributes inherited from FacetValidator

#facet

Instance Method Summary collapse

Methods inherited from FacetValidator

#facet_value, #initialize

Constructor Details

This class inherits a constructor from Lutaml::Xsd::Validation::Facets::FacetValidator

Instance Method Details

#error_message(value) ⇒ String

Generate error message for maximum length violation

Parameters:

  • value (String)

    The invalid value

Returns:

  • (String)

    Error message describing the violation



41
42
43
44
45
# File 'lib/lutaml/xsd/validation/facets/max_length_facet_validator.rb', line 41

def error_message(value)
  actual_length = value.nil? ? 0 : to_string(value).length
  "Value length #{actual_length} exceeds maximum " \
    "length #{facet_value}"
end

#valid?(value) ⇒ Boolean

Validate value meets maximum length

Parameters:

  • value (String)

    The value to validate

Returns:

  • (Boolean)

    true if value length <= maximum, false otherwise



28
29
30
31
32
33
34
35
# File 'lib/lutaml/xsd/validation/facets/max_length_facet_validator.rb', line 28

def valid?(value)
  return false if value.nil?

  max_length = to_integer(facet_value)
  return false unless max_length

  to_string(value).length <= max_length
end