Sha256: 703e5aa6a2e3c4812c0037b285f8e45ab3dbb18eadf8dc554027a5bd17a5cf44

Contents?: true

Size: 983 Bytes

Versions: 11

Compression:

Stored size: 983 Bytes

Contents

module Restspec::Schema::Types
  class DateType < BasicType
    DATE_FORMAT = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/

    # 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.
    def example_for(attribute)
      Faker::Date.between(1.month.ago, Date.today).to_s
    end

    # 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.
    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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
restspec-0.3.2 lib/restspec/schema/types/date_type.rb
restspec-0.3.1 lib/restspec/schema/types/date_type.rb
restspec-0.3.0 lib/restspec/schema/types/date_type.rb
restspec-0.2.6 lib/restspec/schema/types/date_type.rb
restspec-0.2.5 lib/restspec/schema/types/date_type.rb
restspec-0.2.4 lib/restspec/schema/types/date_type.rb
restspec-0.2.3 lib/restspec/schema/types/date_type.rb
restspec-0.2.2 lib/restspec/schema/types/date_type.rb
restspec-0.2.1 lib/restspec/schema/types/date_type.rb
restspec-0.2 lib/restspec/schema/types/date_type.rb
restspec-0.1 lib/restspec/schema/types/date_type.rb