Class: Lutaml::Xsd::Validation::Facets::LengthFacetValidator

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

Overview

Validates values against XSD length facet

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

Examples:

Validating exact length

facet = Lutaml::Xsd::Length.new(value: "5")
validator = LengthFacetValidator.new(facet)
validator.valid?("hello")  # => true (5 chars)
validator.valid?("hi")     # => false (2 chars)
validator.error_message("hi")
# => "Value length 2 does not equal required 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 length mismatch

Parameters:

  • value (String)

    The invalid value

Returns:

  • (String)

    Error message describing the length violation



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

def error_message(value)
  actual_length = value.nil? ? 0 : to_string(value).length
  "Value length #{actual_length} does not equal required " \
    "length #{facet_value}"
end

#valid?(value) ⇒ Boolean

Validate value has exact length

Parameters:

  • value (String)

    The value to validate

Returns:

  • (Boolean)

    true if value length matches, false otherwise



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

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

  required_length = to_integer(facet_value)
  return false unless required_length

  to_string(value).length == required_length
end