Class: Lutaml::Xsd::Validation::BaseTypes::DateValidator
- Inherits:
-
BaseTypeValidator
- Object
- BaseTypeValidator
- Lutaml::Xsd::Validation::BaseTypes::DateValidator
- 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.
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
-
#error_message(value) ⇒ String
Generate error message for invalid date.
-
#valid?(value) ⇒ Boolean
Validate value is a valid date.
Methods inherited from BaseTypeValidator
Instance Method Details
#error_message(value) ⇒ String
Generate error message for invalid date
54 55 56 57 |
# File 'lib/lutaml/xsd/validation/base_types/date_validator.rb', line 54 def (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
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 |