No commit activity in last 3 years
No release in over 3 years
A simple ActiveRecord validator based on the NHS Number specifications for healthcare providers in the UK.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

 Project Readme

Quickly and simply add validation of NHS numbers to a Rails ActiveRecord model, or anywhere in Ruby using a simple-to-use class.

Build StatusGem Version

Usage

Install the Gem by adding the following line to your Gemfile

gem 'nhs_number_validator'

Then run bundle install to install the Gem.

Now, in your model, which might be something like app/models/patient.rb, add the line;

validates :nhs_number, nhs_number: true

The first :nhs_number in this example is the name of your database column, the second tells Rails to validate it against this validator.

The format of these columns should be a 10-character string, where all characters are numerical. Values will validate if an integer is given, but otherwise, no alternatives will be accepted.

By default, nil and blank values will be rejected. If you want to accept nil and blank values, please use accept_blank: true, as follows;

validates :nhs_number, nhs_number: true, accept_blank: true

Calling the Class

If you ever need to use this outside of a model, you can pass the string (or an integer) directly to;

NhsNumberValidator.valid?("4010232137")
# => true
NhsNumberValidator.valid?("4010232132")
# => false
NhsNumberValidator.valid?("watermelon")
# => false

Background

The actual process of validating an NHS number is beyond a simple format check, and involves calculating a check digit using a Modulus 11 algorithm. This has apparently been shown to dramatically reduce typing errors. For more information, please see Wikipedia and for a more detailed description of the algorithm, see the NHS Data Dictionary.