From b563fd677f89fc095b219f1753058ecb866c66c4 Mon Sep 17 00:00:00 2001 From: Ygor Azambuja Date: Sun, 3 Oct 2021 16:23:43 -0400 Subject: [PATCH 1/4] feat: add brazilian cpf validation --- src/lib/isCpf.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/lib/isCpf.js diff --git a/src/lib/isCpf.js b/src/lib/isCpf.js new file mode 100644 index 000000000..442a96217 --- /dev/null +++ b/src/lib/isCpf.js @@ -0,0 +1,44 @@ +import assertString from './util/assertString'; + +// Brazilian CPF (Cadastro de Pessoas FĂ­sicas) + +export default function isCpf(cpfString) { + assertString(cpfString); + + let sum = 0; + let rest; + + const cpf = cpfString.toString().replace(/[^0-9]+/g, ''); + + if (cpf === '00000000000') { + return false; + } + + for (let i = 1; i <= 9; i++) { + sum += parseInt(cpf.substring(i - 1, i), 10) * (11 - i); + } + rest = (sum * 10) % 11; + + if (rest === 10 || rest === 11) { + rest = 0; + } + if (rest !== parseInt(cpf.substring(9, 10), 10)) { + return false; + } + + sum = 0; + for (let i = 1; i <= 10; i++) { + sum += parseInt(cpf.substring(i - 1, i), 10) * (12 - i); + } + rest = (sum * 10) % 11; + + if (rest === 10 || rest === 11) { + rest = 0; + } + + if (rest !== parseInt(cpf.substring(10, 11), 10)) { + return false; + } + return true; +} + From db12f108754f6ea482848210587728088ddd48cf Mon Sep 17 00:00:00 2001 From: Ygor Azambuja Date: Sun, 3 Oct 2021 16:23:53 -0400 Subject: [PATCH 2/4] feat: add isCpf on index --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index a0e8aa63c..6d6e380eb 100644 --- a/src/index.js +++ b/src/index.js @@ -121,6 +121,8 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; +import isCpf from './lib/isCpf'; + const version = '13.6.0'; const validator = { @@ -225,6 +227,7 @@ const validator = { isLicensePlate, isVAT, ibanLocales, + isCpf, }; export default validator; From 3cc9c0f8a6e4915cc9352a31b2d5ecb2787b2da6 Mon Sep 17 00:00:00 2001 From: Ygor Azambuja Date: Sun, 3 Oct 2021 16:24:08 -0400 Subject: [PATCH 3/4] add isCpf on tests --- test/validators.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/validators.js b/test/validators.js index 91794d521..474e0ac89 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11070,4 +11070,14 @@ describe('Validators', () => { ], }); }); + + it('should validate if has a valid brazilian cpf', () => { + test({ + validator: 'isCpf', + args: ['57170743019', '25997505049', '465.839.300-05', '369.440.070-29', + '918.309.182-03', '12839471041', '84018401229'], + valid: ['57170743019', '25997505049', '465.839.300-05', '369.440.070-29'], + invalid: ['918.309.182-03', '12839471041', '84018401229'], + }); + }); }); From 462c897386939e5f914441b1955c620a0bb2262b Mon Sep 17 00:00:00 2001 From: Ygor Azambuja Date: Sun, 3 Oct 2021 18:18:17 -0400 Subject: [PATCH 4/4] refactor: add more test cases --- test/validators.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/test/validators.js b/test/validators.js index 474e0ac89..6bf6a5dc4 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11074,10 +11074,23 @@ describe('Validators', () => { it('should validate if has a valid brazilian cpf', () => { test({ validator: 'isCpf', - args: ['57170743019', '25997505049', '465.839.300-05', '369.440.070-29', - '918.309.182-03', '12839471041', '84018401229'], + args: ['57170743019', + '25997505049', + '465.839.300-05', + '369.440.070-29', + '918.309.182-03', + '12839471041', + '84018401229', + '1309972150', + '1570758018', + ], valid: ['57170743019', '25997505049', '465.839.300-05', '369.440.070-29'], - invalid: ['918.309.182-03', '12839471041', '84018401229'], + invalid: ['918.309.182-03', + '12839471041', + '84018401229', + '00000000000', + '1309972150', + '1570758018'], }); }); });