Class: Lutaml::Xsd::Validation::BaseTypes::DecimalValidator

Inherits:
BaseTypeValidator show all
Defined in:
lib/lutaml/xsd/validation/base_types/decimal_validator.rb

Overview

Validates XSD decimal type

The decimal type represents arbitrary-precision decimal numbers. Valid values include integers and decimals with optional sign.

Examples:

Validating decimal values

validator = DecimalValidator.new
validator.valid?("123.45")    # => true
validator.valid?("-0.5")      # => true
validator.valid?("+3.14")     # => true
validator.valid?("100")       # => true
validator.valid?(".5")        # => true
validator.valid?("5.")        # => true
validator.valid?("abc")       # => false

Constant Summary collapse

DECIMAL_PATTERN =

Pattern for valid decimal format Using possessive quantifiers to prevent polynomial backtracking

/^[+-]?(?>\d++\.?\d*|\.\d+)$/

Instance Method Summary collapse

Methods inherited from BaseTypeValidator

for, registered?, #type_name

Instance Method Details

#error_message(value) ⇒ String

Generate error message for invalid decimal

Parameters:

  • value (Object)

    The invalid value

Returns:

  • (String)

    Error message



54
55
56
# File 'lib/lutaml/xsd/validation/base_types/decimal_validator.rb', line 54

def error_message(value)
  "Value '#{value}' is not a valid decimal number"
end

#valid?(value) ⇒ Boolean

Validate value is a valid decimal

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

    true if value is a valid decimal



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lutaml/xsd/validation/base_types/decimal_validator.rb', line 33

def valid?(value)
  return false if value.nil?
  return true if value.is_a?(Numeric)

  str = to_string(value).strip
  return false if str.empty?

  # Check pattern first for performance
  return false unless str.match?(DECIMAL_PATTERN)

  # Verify it can be parsed as Float
  Float(str)
  true
rescue ArgumentError, TypeError
  false
end