Class: Lutaml::Xsd::Validation::Facets::MinLengthFacetValidator

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

Overview

Validates values against XSD minLength facet

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

Examples:

Validating minimum length

facet = Lutaml::Xsd::MinLength.new(value: "3")
validator = MinLengthFacetValidator.new(facet)
validator.valid?("hello")  # => true (5 >= 3)
validator.valid?("hi")     # => false (2 < 3)
validator.error_message("hi")
# => "Value length 2 is less than minimum length 3"

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 minimum 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/min_length_facet_validator.rb', line 41

def error_message(value)
  actual_length = value.nil? ? 0 : to_string(value).length
  "Value length #{actual_length} is less than minimum " \
    "length #{facet_value}"
end

#valid?(value) ⇒ Boolean

Validate value meets minimum length

Parameters:

  • value (String)

    The value to validate

Returns:

  • (Boolean)

    true if value length >= minimum, false otherwise



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

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

  min_length = to_integer(facet_value)
  return false unless min_length

  to_string(value).length >= min_length
end