Skip to content

Commit b063315

Browse files
feat: added isCPF validator
1 parent 90c19e8 commit b063315

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ import isStrongPassword from './lib/isStrongPassword';
130130

131131
import isVAT from './lib/isVAT';
132132

133+
import isCPF from './lib/isCPF';
134+
133135
const version = '13.15.0';
134136

135137
const validator = {
@@ -245,6 +247,7 @@ const validator = {
245247
isLicensePlate,
246248
isVAT,
247249
ibanLocales,
250+
isCPF,
248251
};
249252

250253
export default validator;

src/lib/isCPF.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import assertString from './util/assertString';
2+
3+
4+
export default function isCPF(cpf) {
5+
assertString(cpf);
6+
if (cpf.length !== 11) return false;
7+
if (cpf === '00000000000' || cpf === '11111111111' || cpf === '22222222222' || cpf === '33333333333' || cpf === '44444444444' || cpf === '55555555555' || cpf === '66666666666' || cpf === '77777777777' || cpf === '88888888888' || cpf === '99999999999') return false;
8+
9+
const paramD1 = {
10+
10: 0, 9: 1, 8: 2, 7: 3, 6: 4, 5: 5, 4: 6, 3: 7, 2: 8,
11+
};
12+
const paramD2 = {
13+
11: 0, 10: 1, 9: 2, 8: 3, 7: 4, 6: 5, 5: 6, 4: 7, 3: 8, 2: 9,
14+
};
15+
let firstNineCharacters = cpf.slice(0, 9);
16+
let d1 = 0;
17+
let d2 = 0;
18+
let d1AndD2 = '';
19+
20+
for (let i = 10; i >= 2; i--) {
21+
if (Number.isNaN(Number(firstNineCharacters[paramD1[i]]))) return false;
22+
d1 += Number(firstNineCharacters[paramD1[i]]) * i;
23+
}
24+
25+
if (d1 % 11 < 2) {
26+
d1 = 0;
27+
} else {
28+
d1 = 11 - (d1 % 11);
29+
}
30+
31+
firstNineCharacters += String(d1);
32+
33+
for (let i = 11; i >= 2; i--) {
34+
d2 += Number(firstNineCharacters[paramD2[i]]) * i;
35+
}
36+
37+
if (d2 % 11 < 2) {
38+
d2 = 0;
39+
} else {
40+
d2 = 11 - (d2 % 11);
41+
}
42+
43+
d1AndD2 += d1;
44+
d1AndD2 += d2;
45+
46+
if (d1AndD2 === cpf.slice(cpf.length - 2)) {
47+
return true;
48+
}
49+
50+
return false;
51+
}

test/validators.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15440,4 +15440,63 @@ describe('Validators', () => {
1544015440
],
1544115441
});
1544215442
});
15443+
it('should validate a CPF', () => {
15444+
test({
15445+
validator: 'isCPF',
15446+
valid: [
15447+
'15709954861',
15448+
'55734947598',
15449+
'60142555835',
15450+
'54945762139',
15451+
'36718123897',
15452+
'43849941922',
15453+
'97989392984',
15454+
'98273014037',
15455+
'66603094703',
15456+
'40668005130',
15457+
'50699975565',
15458+
'98798876465',
15459+
'81661188907',
15460+
'52991780002',
15461+
'03703980761',
15462+
'62281573370',
15463+
'05575125572',
15464+
'03085770860',
15465+
'28686002129',
15466+
'24234579793',
15467+
],
15468+
invalid: [
15469+
'296.231.440-69',
15470+
'62424843039',
15471+
'477.887.094-96',
15472+
'41526890821',
15473+
'861.803.242-00',
15474+
'57751926579',
15475+
'034.928.141-63',
15476+
'66437339822',
15477+
'178.714.111-57',
15478+
'27507501358',
15479+
'769.259.949-19',
15480+
'43661169739',
15481+
'646.438.057-95',
15482+
'49403410474',
15483+
'113.512.907-25',
15484+
'81322401108',
15485+
'458.825.895-23',
15486+
'92472311425',
15487+
'585.251.576-48',
15488+
'00002283074',
15489+
'796.940.600-52',
15490+
'08228682581',
15491+
'906.765.489-12',
15492+
'31437427245',
15493+
'045.504.051-45',
15494+
'21383151886',
15495+
'519.213.751-90',
15496+
'06712261581',
15497+
'884.593.736-07',
15498+
'42188048376',
15499+
],
15500+
});
15501+
});
1544315502
});

0 commit comments

Comments
 (0)