-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pt-BR): tax id, passport and license plates #1613
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
effd4c9
feat(pt-BR): tax id, passport and license plates
mschunke fc51d4d
delete compiled files
mschunke 5da17cd
Merge branch 'master' of https://github.com/validatorjs/validator.js
mschunke 6967456
fix(tests): tests now covering 100% of code
mschunke 23ae508
fix(licenseplate): removed license plate pt-br
mschunke a511310
fix(readme): removed license plate changes
mschunke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -852,6 +852,93 @@ function plPlCheck(tin) { | |
return checksum === parseInt(tin[10], 10); | ||
} | ||
|
||
/* | ||
* pt-BR validation function | ||
* (Cadastro de Pessoas Físicas (CPF, persons) | ||
* Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities) | ||
* Both inputs will be validated | ||
*/ | ||
|
||
function ptBrCheck(tin) { | ||
tin = tin.replace(/[^\d]+/g, ''); | ||
if (tin === '') return false; | ||
|
||
if (tin.length === 11) { | ||
let sum; | ||
let ramainder; | ||
sum = 0; | ||
tin = tin.replace(/[^\d]+/g, ''); | ||
|
||
if ( // Reject known invalid CPFs | ||
tin === '11111111111' || | ||
tin === '22222222222' || | ||
tin === '33333333333' || | ||
tin === '44444444444' || | ||
tin === '55555555555' || | ||
tin === '66666666666' || | ||
tin === '77777777777' || | ||
tin === '88888888888' || | ||
tin === '99999999999' || | ||
tin === '00000000000' | ||
) return false; | ||
|
||
for (let i = 1; i <= 9; i++) sum += parseInt(tin.substring(i - 1, i), 10) * (11 - i); | ||
ramainder = (sum * 10) % 11; | ||
if ((ramainder === 10) || (ramainder === 11)) ramainder = 0; | ||
if (ramainder !== parseInt(tin.substring(9, 10), 10)) return false; | ||
sum = 0; | ||
|
||
for (let i = 1; i <= 10; i++) sum += parseInt(tin.substring(i - 1, i), 10) * (12 - i); | ||
ramainder = (sum * 10) % 11; | ||
if ((ramainder === 10) || (ramainder === 11)) ramainder = 0; | ||
if (ramainder !== parseInt(tin.substring(10, 11), 10)) return false; | ||
|
||
return true; | ||
} | ||
|
||
if (tin.length !== 14) { return false; } | ||
|
||
if ( // Reject know invalid CNPJs | ||
tin === '00000000000000' || | ||
tin === '11111111111111' || | ||
tin === '22222222222222' || | ||
tin === '33333333333333' || | ||
tin === '44444444444444' || | ||
tin === '55555555555555' || | ||
tin === '66666666666666' || | ||
tin === '77777777777777' || | ||
tin === '88888888888888' || | ||
tin === '99999999999999') { return false; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, this condition is not covered, adding a test with an invalid CNPJ should fix it |
||
|
||
let length = tin.length - 2; | ||
let identifiers = tin.substring(0, length); | ||
let verificators = tin.substring(length); | ||
let sum = 0; | ||
let pos = length - 7; | ||
|
||
for (let i = length; i >= 1; i--) { | ||
sum += identifiers.charAt(length - i) * pos; | ||
pos -= 1; | ||
if (pos < 2) { pos = 9; } | ||
} | ||
let result = sum % 11 < 2 ? 0 : 11 - (sum % 11); | ||
if (result !== parseInt(verificators.charAt(0), 10)) { return false; } | ||
|
||
length += 1; | ||
identifiers = tin.substring(0, length); | ||
sum = 0; | ||
pos = length - 7; | ||
for (let i = length; i >= 1; i--) { | ||
sum += identifiers.charAt(length - i) * pos; | ||
pos -= 1; | ||
if (pos < 2) { pos = 9; } | ||
} | ||
result = sum % 11 < 2 ? 0 : 11 - (sum % 11); | ||
if (result !== parseInt(verificators.charAt(1), 10)) { return false; } | ||
|
||
return true; | ||
} | ||
|
||
/* | ||
* pt-PT validation function | ||
* (Número de identificação fiscal (NIF), persons/entities) | ||
|
@@ -1039,6 +1126,7 @@ const taxIdFormat = { | |
'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i, | ||
'nl-NL': /^\d{9}$/, | ||
'pl-PL': /^\d{10,11}$/, | ||
'pt-BR': /^\d{11,14}$/, | ||
'pt-PT': /^\d{9}$/, | ||
'ro-RO': /^\d{13}$/, | ||
'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/, | ||
|
@@ -1076,6 +1164,7 @@ const taxIdCheck = { | |
'mt-MT': mtMtCheck, | ||
'nl-NL': nlNlCheck, | ||
'pl-PL': plPlCheck, | ||
'pt-BR': ptBrCheck, | ||
'pt-PT': ptPtCheck, | ||
'ro-RO': roRoCheck, | ||
'sk-SK': skSkCheck, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is not covered by tests, you probably just need to add a testcase with an invalid CPF