Class: FullMetalBody::Internal::ReasonableDateValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/full_metal_body/internal/reasonable_date_validator.rb

Instance Method Summary collapse

Instance Method Details

#date_valid?(str) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
# File 'lib/full_metal_body/internal/reasonable_date_validator.rb', line 14

def date_valid?(str)
  # Allow only slash and hyphen separators
  unless str.match?(%r{^\d{4}/\d{1,2}/\d{1,2}$|^\d{4}-\d{1,2}-\d{1,2}$})
    return false
  end

  !!Date.parse(str)
rescue
  false
end

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/full_metal_body/internal/reasonable_date_validator.rb', line 6

def validate_each(record, attribute, value)
  return if value.blank? || value.is_a?(Date)

  unless date_valid?(value)
    record.errors.add(attribute, :not_reasonable_date, value: value)
  end
end