Skip to content

Commit 64f7109

Browse files
acostalimafranciscocardoso
authored andcommitted
Add CPF taxpayer identification number assert
1 parent 55efde1 commit 64f7109

File tree

7 files changed

+134
-1
lines changed

7 files changed

+134
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following set of extra asserts are provided by this package:
3030
- [BigNumberLessThanOrEqualTo](#bignumberlessthanorequalto) (requires `bignumber.js`)
3131
- [Boolean](#boolean)
3232
- [Callback](#callback) (requires `callback`)
33+
- [CpfNumber](#cpfnumber) (requires `cpf`)
3334
- [CreditCard](#creditcard) (requires `creditcard`)
3435
- [Date](#date) (requires `moment` for format validation only)
3536
- [DateDiffGreaterThan](#datediffgreaterthan) (requires `moment`)
@@ -106,6 +107,9 @@ Allows you to add custom rules by giving a callback function and a custom class.
106107
- `callback` (required) - the callback function.
107108
- `customClass` (required) - the name of the class.
108109

110+
### CpfNumber
111+
Tests if the value is valid CPF number.
112+
109113
### CreditCard
110114
Tests if the value is a valid credit card number using the Luhn10 algorithm.
111115

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"optionalPeerDependencies": {
6464
"abavalidator": ">=2 <3",
6565
"bignumber.js": ">=7 <=9.0.0",
66+
"cpf": "^2.0.1",
6667
"creditcard": ">=0.0.1 <1.0.0",
6768
"google-libphonenumber": ">=1 <4",
6869
"iban": ">=0.0.6 <1.0.0",

src/asserts/cpf-number-assert.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const _ = require('lodash');
8+
const { Validator, Violation } = require('validator.js');
9+
let cpf;
10+
11+
/**
12+
* Optional peer dependencies.
13+
*/
14+
15+
try {
16+
cpf = require('cpf');
17+
} catch (e) {
18+
// eslint-disable-next-line no-empty
19+
}
20+
21+
/**
22+
* Export `CpfNumber`.
23+
*/
24+
25+
module.exports = function cpfNumberAssert() {
26+
if (!cpf) {
27+
throw new Error('cpf is not installed');
28+
}
29+
30+
/**
31+
* Class name.
32+
*/
33+
34+
this.__class__ = 'CpfNumber';
35+
36+
/**
37+
* Validation algorithm.
38+
*/
39+
40+
this.validate = function (value) {
41+
if (!_.isString(value)) {
42+
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
43+
}
44+
45+
if (!cpf.isValid(value)) {
46+
throw new Violation(this, value, { value: 'must_be_a_valid_cpf_number' });
47+
}
48+
49+
return true;
50+
};
51+
52+
return this;
53+
};

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const BigNumberLessThan = require('./asserts/big-number-less-than-assert.js');
1414
const BigNumberLessThanOrEqualTo = require('./asserts/big-number-less-than-or-equal-to-assert.js');
1515
const Boolean = require('./asserts/boolean-assert.js');
1616
const Callback = require('./asserts/callback-assert');
17+
const CpfNumber = require('./asserts/cpf-number-assert');
1718
const CreditCard = require('./asserts/credit-card-assert.js');
1819
const Date = require('./asserts/date-assert.js');
1920
const DateDiffGreaterThan = require('./asserts/date-diff-greater-than-assert.js');
@@ -56,6 +57,7 @@ module.exports = {
5657
BigNumberLessThanOrEqualTo,
5758
Boolean,
5859
Callback,
60+
CpfNumber,
5961
CreditCard,
6062
Date,
6163
DateDiffGreaterThan,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Assert: BaseAssert, Violation } = require('validator.js');
8+
const CpfNumberAssert = require('../../src/asserts/cpf-number-assert');
9+
10+
/**
11+
* Extend `Assert` with `CpfNumberAssert`.
12+
*/
13+
14+
const Assert = BaseAssert.extend({
15+
CpfNumber: CpfNumberAssert
16+
});
17+
18+
/**
19+
* Test `CpfNumberAssert`.
20+
*/
21+
22+
describe('CpfNumberAssert', () => {
23+
it('should throw an error if the input value is not a string', () => {
24+
try {
25+
Assert.cpfNumber().validate();
26+
27+
fail();
28+
} catch (e) {
29+
expect(e).toBeInstanceOf(Violation);
30+
expect(e.show().assert).toBe('CpfNumber');
31+
expect(e.value).toBeUndefined();
32+
expect(e.violation.value).toBe('must_be_a_string');
33+
}
34+
});
35+
36+
it('should throw an error if `cpf` is invalid', () => {
37+
try {
38+
Assert.cpfNumber().validate('123');
39+
40+
fail();
41+
} catch (e) {
42+
expect(e).toBeInstanceOf(Violation);
43+
expect(e.value).toBe('123');
44+
expect(e.violation.value).toBe('must_be_a_valid_cpf_number');
45+
}
46+
});
47+
48+
it('should throw an error if `cpf` is invalid but correctly formatted', () => {
49+
try {
50+
Assert.cpfNumber().validate('564.232.751-31');
51+
52+
fail();
53+
} catch (e) {
54+
expect(e).toBeInstanceOf(Violation);
55+
expect(e.value).toBe('564.232.751-31');
56+
expect(e.violation.value).toBe('must_be_a_valid_cpf_number');
57+
}
58+
});
59+
60+
it('should accept a valid `cpf` not formatted', () => {
61+
Assert.cpfNumber().validate('43228987264');
62+
});
63+
64+
it('should accept a valid `cpf` correctly formatted', () => {
65+
Assert.cpfNumber().validate('432.289.872-64');
66+
});
67+
});

test/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
1414
it('should export all asserts', () => {
1515
const assertNames = Object.keys(asserts);
1616

17-
expect(assertNames).toHaveLength(36);
17+
expect(assertNames).toHaveLength(37);
1818
expect(assertNames).toEqual(
1919
expect.arrayContaining([
2020
'AbaRoutingNumber',
@@ -27,6 +27,7 @@ describe('validator.js-asserts', () => {
2727
'BigNumberLessThanOrEqualTo',
2828
'Boolean',
2929
'Callback',
30+
'CpfNumber',
3031
'CreditCard',
3132
'Date',
3233
'DateDiffGreaterThan',

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,11 @@ core-util-is@~1.0.0:
10961096
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85"
10971097
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==
10981098

1099+
cpf@^2.0.1:
1100+
version "2.0.1"
1101+
resolved "https://registry.yarnpkg.com/cpf/-/cpf-2.0.1.tgz#7979eb33f0a14e8217292944311faaba82cb7f02"
1102+
integrity sha512-YpRbrpjQCDkZgMUOLxLfme8wE8X56fHXOGMGDsYA48f7UrjaCHU0TP8d+CKmxvNVjBDVtHiCUiBw8vfstdslbw==
1103+
10991104
creditcard@^0.1.2:
11001105
version "0.1.2"
11011106
resolved "https://registry.npmjs.org/creditcard/-/creditcard-0.1.2.tgz"

0 commit comments

Comments
 (0)