Class: EasyJSONMatcher::DateValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/easy_json_matcher/date_validator.rb

Constant Summary collapse

DEFAULT_DATE_FORMAT =
"%Y-%m-%d"

Instance Attribute Summary collapse

Attributes inherited from Validator

#content, #errors, #key, #required

Instance Method Summary collapse

Methods inherited from Validator

#_check_content_type, #_check_required?, #_create_validator, #_no_errors?, #_post_initialise, #_set_content, #get_errors, #reset!, #valid?

Constructor Details

#initialize(opts = {}) ⇒ DateValidator

Returns a new instance of DateValidator.



8
9
10
11
12
# File 'lib/easy_json_matcher/date_validator.rb', line 8

def initialize(opts = {})
  super(opts)
  @date_format = opts[:format]
  @string_validator = _create_validator(type: :string)
end

Instance Attribute Details

#date_formatObject (readonly)

Returns the value of attribute date_format.



5
6
7
# File 'lib/easy_json_matcher/date_validator.rb', line 5

def date_format
  @date_format
end

#string_validatorObject (readonly)

Returns the value of attribute string_validator.



5
6
7
# File 'lib/easy_json_matcher/date_validator.rb', line 5

def string_validator
  @string_validator
end

Instance Method Details

#_content_is_a_date?Boolean

Returns:



23
24
25
26
27
28
29
30
# File 'lib/easy_json_matcher/date_validator.rb', line 23

def _content_is_a_date?
  require 'date'
  begin
    Date.strptime(content, date_format)
  rescue ArgumentError
    false
  end
end

#_content_is_a_string?Boolean

Returns:



19
20
21
# File 'lib/easy_json_matcher/date_validator.rb', line 19

def _content_is_a_string?
  string_validator.valid? content
end

#_validateObject



14
15
16
17
# File 'lib/easy_json_matcher/date_validator.rb', line 14

def _validate
  _validate_string
  _validate_date
end

#_validate_dateObject



40
41
42
# File 'lib/easy_json_matcher/date_validator.rb', line 40

def _validate_date
  errors << "#{content} is not a Date" unless _content_is_a_date?
end

#_validate_stringObject



36
37
38
# File 'lib/easy_json_matcher/date_validator.rb', line 36

def _validate_string
  errors << "#{content} must be provided as a String for Date validation" unless _content_is_a_string?
end