class Restspec::Schema::Types::DateType

Constants

DATE_FORMAT

Public Instance Methods

example_for(attribute) click to toggle source

Generates an example date.

@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @return [Date] A random date between one month ago and today.

# 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) click to toggle source

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

@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @param value [Object] the value of the attribute.

@return [true, false] If the value is a date with the correct format.

# 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