Class: ValidateParams::Types::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/validate_params/types/date.rb

Constant Summary collapse

FORMAT =
"%Y-%m-%d"

Class Method Summary collapse

Class Method Details

.cast(raw_value) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/validate_params/types/date.rb', line 23

def self.cast(raw_value, **)
  return raw_value if raw_value.is_a?(::Date)

  ::Date.strptime(raw_value.to_s, FORMAT)
rescue StandardError
  raw_value
end

.valid?(value) ⇒ Boolean

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/validate_params/types/date.rb', line 8

def self.valid?(value)
  value = value.to_s
  return false unless /\d{4}-\d{2}-\d{2}/.match?(value)

  parsed_date = begin
    ::Date.strptime(value, FORMAT)
  rescue StandardError
    nil
  end
  return false if parsed_date.nil?
  return false if parsed_date.year > 9999

  true
end