A few extra validations for rails.
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 is a valid domain. This should work with IDN.
validates :domain_column, domain: true
# Set a minimum/maximum number of domain parts (aka. labels)
validates :domain_column, domain: { min_domain_parts: 2 }
validates :domain_column, domain: { max_domain_parts: 2 }
Do a basic checks for emails. Not that this check is not very strict, it is nigh-impossible to check if an email address is valid (and even if it is, it's no guarantee if emails actually gets delivered to this address). This should work with unicode addresses (RFC 6531, IDN).
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. As with the Email check, this isn't particularly strict; conventions for writing phone numbers vary a lot.
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.1, 20141013
- Fix i18n key for
phone - Allow passing a Proc to
datewithout an argument
version 1.1, 20141003
- Make the date validation work if the column it points to is
nil. - Add documentation.
version 1.0, 20140905
- Initial release.