Skip to content

Commit

Permalink
Merge pull request #173 from Availity/feature/npi
Browse files Browse the repository at this point in the history
Add NPI Validator
  • Loading branch information
robmcguinness committed Dec 3, 2015
2 parents c09c495 + e73fc39 commit cc48c8d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
1 change: 1 addition & 0 deletions gulp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module.exports = {
'./lib/core/validation/validators/validator-required.js',
'./lib/core/validation/validators/validator-date-range.js',
'./lib/core/validation/validators/validator-date-format.js',
'./lib/core/validation/validators/validator-npi.js',
'./lib/core/validation/validators/validator-phone.js',
'./lib/core/validation/validators/validator-email.js',
'./lib/core/utils/globals.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
describe('avValDate', function () {
'use strict';

var dateForamt;
var avValDate;
var rules;

beforeEach(module('availity'));

beforeEach(inject(function (avValDate, AV_VAL) {
dateForamt = avValDate;
beforeEach(inject(function(_avValDate_, AV_VAL) {
avValDate = _avValDate_;
rules = { format: AV_VAL.DATE_FORMAT.SIMPLE };
}));

it('should be a valid', function () {
expect(dateForamt.validate('02/02/2015', rules)).toBe(true);
expect(avValDate.validate('02/02/2015', rules)).toBe(true);
});

it('should NOT be valid', function() {
expect(dateForamt.validate('20/02/2015', rules)).toBe(false);
expect(avValDate.validate('20/02/2015', rules)).toBe(false);
});

it('should use default date format if one is not provided', function() {
expect(dateForamt.validate('02/02/2015', {})).toBe(true);
expect(avValDate.validate('02/02/2015', {})).toBe(true);
});

});
40 changes: 40 additions & 0 deletions lib/core/validation/validators/tests/validator-npi-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*global describe, it, beforeEach, expect, module, inject*/
describe('avValNpi', function () {
'use strict';

var avValNpi;

beforeEach(module('availity'));

beforeEach(inject(function(_avValNpi_) {
avValNpi = _avValNpi_;
}));

it('should NOT validate if NPI is empty, undefined or null', function () {
expect(avValNpi.validate(undefined)).toBe(false);
expect(avValNpi.validate('')).toBe(false);
expect(avValNpi.validate(null)).toBe(false);
});

it("should NOT validate if NPI contains non-digits", function() {
expect(avValNpi.validate('i2eh56789o')).toBe(false);
});

it("should NOT validate if NPI is not 10 digits in length", function() {
expect(avValNpi.validate('123456789')).toBe(false);
expect(avValNpi.validate('12345678901')).toBe(false);
});

it("should NOT validate if NPI does not start with a 1, 2, 3, or 4", function() {
expect(avValNpi.validate('5678901234')).toBe(false);
});

it("should NOT validate if NPI checksum doesn't match check digit", function() {
expect(avValNpi.validate("1234567890")).toBe(false);
});

it("should validate if NPI is valid", function() {
expect(avValNpi.validate("1234567893")).toBe(false);
});

});
64 changes: 64 additions & 0 deletions lib/core/validation/validators/validator-npi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(function(root) {

'use strict';

var availity = root.availity;

availity.core.factory('avValNpi', function() {


var validator = {

name: 'npi',

INTEGER_REGEX: /^\d*$/,

validate: function(value) {

var npi = value || '';

if (validator.INTEGER_REGEX.test(npi) || npi.length !== 10) {
return false;
}

var firstDigit = npi.charAt(0);
if(!('1' === firstDigit || '2' === firstDigit || '3' === firstDigit || '4' === firstDigit)) {
return false;
}

var digit = parseInt(npi.charAt(9), 10);
npi = npi.substring(0, 9);
npi = "80840" + npi;

var alternate = true;
var total = 0;

for (var i = npi.length; i > 0; i--) {
var next = parseInt(npi.charAt(i-1), 10);
if (alternate) {
next = next*2;
if (next > 9) {
next = (next % 10) + 1;
}
}
total += next;
alternate = !alternate;
}

var roundUp = Math.ceil(total / 10) * 10;
var calculatedCheck = roundUp - total;

if (calculatedCheck !== digit) {
return false;
}

return true;
}

};

return validator;

});

})(window);

0 comments on commit cc48c8d

Please sign in to comment.