Class: Dipa::DateValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/dipa/date_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid?(value:) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
# File 'app/validators/dipa/date_validator.rb', line 15

def self.valid?(value:)
  return false if value.blank?

  begin
    Date.parse(value.to_s)
    true
  rescue ArgumentError
    false
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'app/validators/dipa/date_validator.rb', line 5

def validate_each(record, attribute, value)
  return if self.class.valid?(value: value)

  record.errors.add(
    attribute,
    :invalid,
    message: (options[:message] || 'is not valid Date')
  )
end