Class: ActiveRecordDateValidator::DateValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_record_date_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_record_date_validator.rb', line 5

def validate_each(record, attribute, value)
  # Get the raw value before typecasting
  raw_value = record.read_attribute_before_type_cast(attribute)
  
  # Skip validation for nil values
  return if raw_value.nil?
  
  # Only allow Date, Time, or ActiveSupport::TimeWithZone objects
  # Reject ANY string values regardless of format
  if raw_value.is_a?(String) || 
     (!raw_value.is_a?(Date) && !raw_value.is_a?(Time) && !raw_value.is_a?(ActiveSupport::TimeWithZone))
    record.errors.add(attribute, "must be a valid date or datetime")
  end
end