Class: Restspec::Schema::Types::DateTimeType

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

Overview

Manages DateTime

Options: Example: initial_interval: The initial interval (default: 2 months ago) final_interval: The final interval (default: Time.now)

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) ⇒ string

Generates an example time formatted as ISO8601.

Parameters:

Returns:

  • (string)

    A time between 1 month ago and now formatted as ISO8601.



13
14
15
# File 'lib/restspec/schema/types/datetime_type.rb', line 13

def example_for(attribute)
  Faker::Time.between(initial_example_interval, final_example_interval).iso8601
end

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

Validates is the value is a correct time according to ISO8601.

Parameters:

Returns:

  • (true, false)

    If the value is accord to ISO8601.



23
24
25
26
27
28
29
30
# File 'lib/restspec/schema/types/datetime_type.rb', line 23

def valid?(attribute, value)
  return false unless value.present?
  allowed_date_time_formats.any? do |format|
    DateTime.parse(value).strftime(format) == value
  end
rescue ArgumentError
  false
end