Class: Lutaml::Xsd::Validation::BaseTypes::TimeValidator

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

Overview

Validates XSD time type

The time type represents a time of day in the format: HH:MM:SS with optional fractional seconds and timezone.

Examples:

Validating time values

validator = TimeValidator.new
validator.valid?("14:30:00")         # => true
validator.valid?("14:30:00Z")        # => true
validator.valid?("14:30:00.123")     # => true
validator.valid?("14:30:00+05:30")   # => true
validator.valid?("2:30:00")          # => false (must be padded)
validator.valid?("25:00:00")         # => false (invalid hour)

Constant Summary collapse

TIME_PATTERN =

ISO 8601 time pattern

/^
  \d{2}:\d{2}:\d{2}       # Time part: HH:MM:SS
  (\.\d+)?                 # Optional fractional seconds
  (Z|[+-]\d{2}:\d{2})?    # Optional timezone
$/x

Instance Method Summary collapse

Methods inherited from BaseTypeValidator

for, registered?, #type_name

Instance Method Details

#error_message(value) ⇒ String

Generate error message for invalid time

Parameters:

  • value (Object)

    The invalid value

Returns:

  • (String)

    Error message



61
62
63
64
# File 'lib/lutaml/xsd/validation/base_types/time_validator.rb', line 61

def error_message(value)
  "Value '#{value}' is not a valid time. " \
    "Expected format: HH:MM:SS[.sss][Z|(+|-)HH:MM]"
end

#valid?(value) ⇒ Boolean

Validate value is a valid time

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

    true if value is a valid ISO 8601 time



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/xsd/validation/base_types/time_validator.rb', line 35

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

  str = to_string(value).strip
  return false unless str.match?(TIME_PATTERN)

  # Extract time part (before timezone and fractional seconds)
  time_part = str.split(/[Z+.-]/)[0]
  parts = time_part.split(":")

  # Validate ranges
  hour = parts[0].to_i
  minute = parts[1].to_i
  second = parts[2].to_i

  hour.between?(0, 23) &&
    minute >= 0 && minute <= 59 &&
    second >= 0 && second <= 59
rescue StandardError
  false
end