Class: Lutaml::Xsd::Validation::BaseTypes::DateValidator

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

Overview

Validates XSD date type

The date type represents a calendar date in the format: YYYY-MM-DD with optional timezone.

Examples:

Validating date values

validator = DateValidator.new
validator.valid?("2024-01-15")       # => true
validator.valid?("2024-01-15Z")      # => true
validator.valid?("2024-01-15+05:30") # => true
validator.valid?("2024-1-5")         # => false (must be padded)
validator.valid?("01-15-2024")       # => false (wrong format)

Constant Summary collapse

DATE_PATTERN =

ISO 8601 date pattern

/^
  -?\d{4}-\d{2}-\d{2}     # Date part: YYYY-MM-DD
  (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 date

Parameters:

  • The invalid value

Returns:

  • Error message



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

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

#valid?(value) ⇒ Boolean

Validate value is a valid date

Parameters:

  • The value to validate

Returns:

  • true if value is a valid ISO 8601 date



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

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

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

  # Extract date part (before timezone)
  date_part = str.split(/[Z+-]/)[0]

  # Attempt to parse with Ruby's Date
  Date.parse(date_part)
  true
rescue ArgumentError, TypeError
  false
end