Class: Restspec::Schema::Types::DateType

Inherits:
BasicType
  • Object
show all
Defined in:
lib/restspec/schema/types/date_type.rb

Constant Summary collapse

DATE_FORMAT =
/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/

Instance Method Summary collapse

Methods inherited from BasicType

#initialize, #of, #to_s, #totally_valid?, #|

Constructor Details

This class inherits a constructor from Restspec::Schema::Types::BasicType

Instance Method Details

#example_for(attribute) ⇒ Date

Generates an example date.


9
10
11
# File 'lib/restspec/schema/types/date_type.rb', line 9

def example_for(attribute)
  Faker::Date.between(1.month.ago, Date.today).to_s
end

#valid?(attribute, value) ⇒ true, false

Validates if the value is a date. It basically checks if the date is according to yyyy-mm-dd format


21
22
23
24
25
26
# File 'lib/restspec/schema/types/date_type.rb', line 21

def valid?(attribute, value)
  return false unless value.present?
  return false unless value.match(DATE_FORMAT).present?
  year, month, day = value.split('-').map &:to_i
  Date.valid_date?(year, month, day)
end