Gem Version Build Status Dependency Status Inline docs

A few extra validations for Rails.

We try to do the ‘sane’ thing by not being too strict, when in doubt, we accept input as being valid. We never want to reject valid input as invalid.

For many formats doing a 100% foolproof check is not trivial, email addresses are a famous example, but it also applies to other formats.
Regardless, you can never be sure it’s what the user intended anyway. For example, email validators will accept [email protected] as being ‘valid’, even though my email address is [email protected]..

date

Validate if a column is a valid date, and if it’s before or after another date.

validates :date_column, date: true
validates :date_column, date: { after: Date.today }
validates :date_column, date: { after_or_equal_to: Date.today }
validates :date_column, date: { equal_to: Date.today }
validates :date_column, date: { before: Date.today }
validates :date_column, date: { before_or_equal_to: Date.today }

Check if the column enddate is after the value of the column begindate:

validates :begindate, date: true
validates :enddate, date: { after: :begindate }

domain

Validate if a string looks like a valid domain. This should work with IDN.

This will accept lico.nl, but rejects [email protected]:

validates :domain_column, domain: true

Set a minimum/maximum number of domain parts (aka. labels), this will accept lico.nl, but rejects lico:

validates :domain_column, domain: { min_domain_parts: 2 }

Accept lico.nl, but reject www.lico.nl:

validates :domain_column, domain: { max_domain_parts: 2 }

email

Validate if a string looks liek an email address. This should work with unicode addresses (RFC 6531, IDN).

Accepts [email protected], but rejects martinlico.nl or martin@lico

validates :email_column, email: true

iban

Check if this is a valid IBAN account number. This uses the iban-tools gem.

validates :iban_column, iban: true

phone

Check if this is a valid phone number. This should work with most, if not all, writing conventions. We consider a phone to be valid if it consists of numbers & any number of \-.(); a country code at the start indicated with + is also accepted.

validates :phone_column, phone: true

postal_code

Check if this is a valid postal code (or zip code for the states).

validates :postal_code_column, postal_code: { country: :nl }

# Country defaults to I18n.locale
validates :postal_code_column, postal_code: true

Currently implemented countries:

  • nl - The Netherlands

ChangeLog

Version 1.1.3, 2014-12-03

  • Make sure that the date validator doesn’t throw an exception if 'invalid'.to_date fails.

Version 1.1.2, 2014-12-01

  • Fix typo in Dutch translation.
  • Update some docs.

Version 1.1.1, 2014-10-13

  • Fix i18n key for phone.
  • Allow passing a Proc to date without an argument.

Version 1.1, 2014-10-03

  • Make the date validation work if the column it points to is nil.
  • Add documentation.

Version 1.0, 2014-09-05

  • Initial release.