Class: Cocina::Models::Validators::W3cdtfValidator
- Inherits:
-
Object
- Object
- Cocina::Models::Validators::W3cdtfValidator
- Defined in:
- lib/cocina/models/validators/w3cdtf_validator.rb
Overview
Validates w3cdtf date
Constant Summary collapse
- REGEX =
/\A(?<year>\d{4})(?:-(?<month>\d\d)(?:-(?<day>\d\d)(?<time>T\d\d:\d\d(?::\d\d(?:.\d+)?)?(?:Z|[+-]\d\d:\d\d))?)?)?\z/ix
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(date) ⇒ W3cdtfValidator
constructor
A new instance of W3cdtfValidator.
-
#validate ⇒ Object
The W3CDTF format is defined here: www.w3.org/TR/NOTE-datetime.
Constructor Details
#initialize(date) ⇒ W3cdtfValidator
Returns a new instance of W3cdtfValidator.
14 15 16 |
# File 'lib/cocina/models/validators/w3cdtf_validator.rb', line 14 def initialize(date) @date = date end |
Class Method Details
.validate(date) ⇒ Object
10 11 12 |
# File 'lib/cocina/models/validators/w3cdtf_validator.rb', line 10 def self.validate(date) new(date).validate end |
Instance Method Details
#validate ⇒ Object
The W3CDTF format is defined here: www.w3.org/TR/NOTE-datetime
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cocina/models/validators/w3cdtf_validator.rb', line 32 def validate return false unless (matches = @date.match(REGEX)) return true unless matches[:month] return (1..12).include? matches[:month].to_i unless matches[:day] Date.parse(@date) true rescue Date::Error false end |