UK Validators

This library contains Rails 3 custom validators for the following UK-centric data:

  • National Insurance Number
  • Post Code
  • Driving Licence Number
  • Passport Number

Installation

Add this line to your application's Gemfile:

gem 'uk_validators'

And then execute:

$ bundle install

National Insurance Number (Nino)

Validates the format of a UK National Insurance Number.

References: HMRC, uk-osint.net, Wikipedia

Usage

class User < ActiveRecord::Base
  validates :ni_number, nino: true
end

I18n

The default error message is is not a valid National Insurance No.

This can be translated in the same was as any other Rails validation message using the key :nino.

For the example User model above, the customised en.yml would be:

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            ni_number:
              nino: "is not valid"

Post Code

Validates the format of a UK postal code. This does not check whether the post code actually exists as this would require a lookup against approx. 1.8 million postal codes.

References: Royal Mail PAF Document, Wikipedia

Usage

class Address < ActiveRecord::Base
  validates :postal_code, postcode: true
end

I18n

The default error message is is not a valid postcode

This can be translated in the same was as any other Rails validation message using the key :postcode.

For the example Address model above, the customised en.yml would be:

en:
  activerecord:
    errors:
      models:
        address:
          attributes:
            postal_code:
              postcode: "is not valid"

Driving Licence Number

Validates the format of a UK Driving Licence Number covering the format used in England, Scotland and Wales. Northen Ireland has a different format containing 8 random alphanumeric characters.

References: DVLA, uk-osint.net

Usage

class User < ActiveRecord::Base
  validates :licence_number, driving_licence: true
end

I18n

The default error message is is not a valid Driving Licence No.

This can be translated in the same was as any other Rails validation message using the key :driving_licence.

For the example User model above, the customised en.yml would be:

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            licence_number:
              driving_licence: "is not valid"

Passport Number

A UK passport number is simply made up of 9 numeric characters.

References: NHS Data Dictionary, highprogrammer.com

Usage

class HolidayMaker < ActiveRecord::Base
  validates :passport, passport_number: true
end

I18n

The default error message is is not a valid Passport No.

This can be translated in the same was as any other Rails validation message using the key :passport_number.

For the example HolidayMaker model above, the customised en.yml would be:

en:
  activerecord:
    errors:
      models:
        holiday_maker:
          attributes:
            passport:
              passport_number: "is not valid"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request