Date validation with Rails 3

Since the first Rails 3 versions came out, we've been playing and using it with new projects. In one of these projectes we needed to validate that a date was before or after, so looking at rails guides and some posts we coded our first Rails 3 validator.

Introducing date_validator

As you can read in the Github repo description, this is a very simple date validator. To install it:

gem sources -a http://rubygems.org # If you haven't done it before
gem install date_validator

Or to make it work with Bundler, in your Gemfile:

gem 'date_validator'

Its only dependency is ActiveModel, so you can use it either in Rails 3 models or in any custom class (after requiring ActiveModel), in a really easy manner:

validates :expiration_date,
  :date => { :after => Time.now, :before => Time.now + 1.year }

For now these are the available options you can use:

:after
:before
:after_or_equal_to
:before_or_equal_to

Pretty much self-explanatory!

Don't forget the tests: remarkable_date_validator

And last but not least, if you use Remarkable (specifically the Rails 3 fork) also install the gem with remarkable matchers:

gem install remarkable_date_validator

And in your model specs:

should_validate_date_of :expiration_date,
  :date => {:after => Time.now, :before => Time.now + 1.year}

Anyone fancies forking the project and adding some shoulda matchers?