Q:

Rails validate type date?

I think i might be dreaming, but i think i read somewhere that you can validate the type of an attribute of an object before you save it? Something
like validates :transaction_date, :type => Date and that will make sure that its a date?
Is this possible in Rails 3.2? i am trying to find evidence of this on the net. i have already looked here at the rails api and i am going through the  ActiveRecord support.

A:

As a complement to the other answers, note that you can define a custom validator to let you use exactly the syntax you proposed:
validates :transaction_date, :type => Date

as follows:
class TypeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add attribute, (options[:message] || "is not of class #{options[:with]}") unless
      value.class == options[:with]
  end
end

Of course, if you want to allow subclasses, you could change the test to use kind_of?.

A:

Rails doesn't support this directly; the closest it comes is probably validates_format_of, or supplying your own custom validator.
I think what you want is the validates_timeliness gem. It not only validates that something is a valid date, but you can specify whether it should be before or after today and various other date range checks.

