Skip to content

Commit 8667234

Browse files
committed
Merge pull request #66 from seegno/enhancement/add-taxpayer-identification-number-asse
Add Taxpayer Identification Number assert
2 parents a14724e + 62727e4 commit 8667234

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The following set of extra asserts are provided by this package:
4545
- [NullOrString](#nullorstring)
4646
- [Phone](#phone) (requires `google-libphonenumber`)
4747
- [PlainObject](#plainobject)
48+
- [TaxpayerIdentificationNumber](#taxpayeridentificationnumber) (_TIN_, requires `tin-validator`)
4849
- [UkModulusChecking](#ukmoduluschecking) (requires `uk-modulus-checking`)
4950
- [Uri](#uri) (requires `urijs`)
5051
- [UsSubdivision](#ussubdivision)
@@ -166,6 +167,9 @@ Tests if the phone is valid and optionally if it belongs to the given country. T
166167
### PlainObject
167168
Tests if the value is a plain object.
168169

170+
### TaxpayerIdentificationNumber
171+
Tests if the value is a valid Taxpayer Identification Number (_TIN_) as defined by the [U.S. IRS](http://www.irs.gov/Individuals/International-Taxpayers/Taxpayer-Identification-Numbers-TIN).
172+
169173
### UkModulusChecking
170174
Tests if the given `accountNumber` and `sortCode` represent a valid `Faster Payment Account`.
171175

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"moment": "^2.12.0",
7171
"should": "^8.3.0",
7272
"sinon": "^1.15.4",
73+
"tin-validator": "^1.0.0",
7374
"uk-modulus-checking": "0.0.2",
7475
"urijs": "^1.17.1",
7576
"validator": "^5.2.0"
@@ -82,6 +83,7 @@
8283
"iban": ">=0.0.6 <1.0.0",
8384
"isoc": ">=0.0.1 <1.0.0",
8485
"moment": ">=2 <3",
86+
"tin-validator": ">=1.0.0 <2.0.0",
8587
"uk-modulus-checking": "0.0.2",
8688
"urijs": ">=1 <2",
8789
"validator": ">=3 <6"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
import { Validator, Violation } from 'validator.js';
8+
9+
/**
10+
* Export `TaxpayerIdentificationNumberAssert`.
11+
*/
12+
13+
export default function() {
14+
/**
15+
* Optional peer dependency.
16+
*/
17+
18+
const tin = require('tin-validator');
19+
20+
/**
21+
* Class name.
22+
*/
23+
24+
this.__class__ = 'TaxpayerIdentificationNumber';
25+
26+
/**
27+
* Validation algorithm.
28+
*/
29+
30+
this.validate = function(value) {
31+
if (typeof value !== 'string') {
32+
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
33+
}
34+
35+
if (!tin.isValid(value)) {
36+
throw new Violation(this, value);
37+
}
38+
39+
return true;
40+
};
41+
42+
return this;
43+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import NullOrString from './asserts/null-or-string-assert.js';
2929
import Phone from './asserts/phone-assert.js';
3030
import PlainObject from './asserts/plain-object-assert.js';
3131
import UkModulusChecking from './asserts/uk-modulus-checking-assert.js';
32+
import TaxpayerIdentificationNumber from './asserts/taxpayer-identification-number-assert.js';
3233
import Uri from './asserts/uri-assert.js';
3334
import UsSubdivision from './asserts/us-subdivision-assert.js';
3435
import UsZipCode from './asserts/us-zip-code-assert.js';
@@ -65,6 +66,7 @@ export default {
6566
Phone,
6667
PlainObject,
6768
UkModulusChecking,
69+
TaxpayerIdentificationNumber,
6870
Uri,
6971
UsSubdivision,
7072
UsZipCode,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
import { Assert as BaseAssert, Violation } from 'validator.js';
7+
import TaxpayerIdentificationNumberAssert from '../../src/asserts/taxpayer-identification-number-assert';
8+
import should from 'should';
9+
10+
/**
11+
* Extend `Assert` with `TaxpayerIdentificationNumberAssert`.
12+
*/
13+
14+
const Assert = BaseAssert.extend({
15+
TaxpayerIdentificationNumber: TaxpayerIdentificationNumberAssert
16+
});
17+
18+
/**
19+
* Test `TaxpayerIdentificationNumberAssert`.
20+
*/
21+
22+
describe('TaxpayerIdentificationNumberAssert', () => {
23+
it('should throw an error if the input value is not a string', () => {
24+
[{}, []].forEach((choice) => {
25+
try {
26+
new Assert().TaxpayerIdentificationNumber().validate(choice);
27+
28+
should.fail();
29+
} catch (e) {
30+
e.should.be.instanceOf(Violation);
31+
e.violation.value.should.equal('must_be_a_string');
32+
}
33+
});
34+
});
35+
36+
it('should throw an error if the input value is not a valid `tin`', () => {
37+
try {
38+
new Assert().TaxpayerIdentificationNumber().validate('foobar');
39+
40+
should.fail();
41+
} catch (e) {
42+
e.should.be.instanceOf(Violation);
43+
e.show().value.should.equal('foobar');
44+
}
45+
});
46+
47+
it('should throw an error if the input value is not a correctly formatted `tin`', () => {
48+
try {
49+
new Assert().TaxpayerIdentificationNumber().validate('1-2-3456 789');
50+
51+
should.fail();
52+
} catch (e) {
53+
e.should.be.instanceOf(Violation);
54+
e.show().value.should.equal('1-2-3456 789');
55+
}
56+
});
57+
58+
it('should expose `assert` equal to `TaxpayerIdentificationNumber`', () => {
59+
try {
60+
new Assert().TaxpayerIdentificationNumber().validate('1-2-3456 789');
61+
62+
should.fail();
63+
} catch (e) {
64+
e.show().assert.should.equal('TaxpayerIdentificationNumber');
65+
}
66+
});
67+
68+
it('should accept a valid `tin`', () => {
69+
new Assert().TaxpayerIdentificationNumber().validate('123456789');
70+
});
71+
72+
it('should accept a correctly formatted `tin`', () => {
73+
new Assert().TaxpayerIdentificationNumber().validate('123-45-6789');
74+
});
75+
});

test/index_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('validator.js-asserts', () => {
3838
'Phone',
3939
'PlainObject',
4040
'UkModulusChecking',
41+
'TaxpayerIdentificationNumber',
4142
'Uri',
4243
'UsSubdivision',
4344
'UsZipCode',

0 commit comments

Comments
 (0)