Class: Lutaml::Xsd::Validation::Facets::TotalDigitsFacetValidator

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

Overview

Validates values against XSD totalDigits facet

The totalDigits facet specifies the maximum number of digits (excluding leading zeros, trailing zeros after decimal point, and decimal point itself) for decimal and integer types.

Examples:

Validating total digits

facet = Lutaml::Xsd::TotalDigits.new(value: "5")
validator = TotalDigitsFacetValidator.new(facet)
validator.valid?("12345")    # => true (5 digits)
validator.valid?("123.45")   # => true (5 digits)
validator.valid?("123456")   # => false (6 digits)
validator.error_message("123456")
# => "Value has 6 digits, exceeds maximum of 5 total digits"

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 total digits violation

Parameters:

  • value (String, Numeric)

    The invalid value

Returns:

  • (String)

    Error message describing the violation



43
44
45
46
47
# File 'lib/lutaml/xsd/validation/facets/total_digits_facet_validator.rb', line 43

def error_message(value)
  actual_digits = count_digits(value)
  "Value has #{actual_digits} digits, exceeds maximum of " \
    "#{facet_value} total digits"
end

#valid?(value) ⇒ Boolean

Validate value has correct total digits

Parameters:

  • value (String, Numeric)

    The value to validate

Returns:

  • (Boolean)

    true if total digits <= maximum, false otherwise



30
31
32
33
34
35
36
37
# File 'lib/lutaml/xsd/validation/facets/total_digits_facet_validator.rb', line 30

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

  max_digits = to_integer(facet_value)
  return false unless max_digits

  count_digits(value) <= max_digits
end