Skip to content

Commit

Permalink
Added error handling returned from the IRS website.
Browse files Browse the repository at this point in the history
Added CPNJ structure validation

Added validation of the legal nature of the CNPJ

Added default value in data entries.

Added year validation

Added month validation

Organization of code in multiple files

added winston logger module
  • Loading branch information
engmsilva committed Mar 19, 2023
1 parent 2d85b08 commit 59362c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function main() {
type: 'input',
name: 'cnpj',
message: 'Informe o CNPJ:',
default: '34295973000117',
default: '63543580000183',
async validate(value) {
const valid = await validateCNPJ(value);
return valid[0] || valid[1];
Expand Down
8 changes: 5 additions & 3 deletions bin/validateCNPJ.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ async function validateCNPJ(cnpj) {
}
}
let digito2 = soma % 11 < 2 ? 0 : 11 - soma % 11;

// Checks if the calculated check digits are equal to the CNPJ digits
if (parseInt(cnpj.charAt(12)) !== digito1 || parseInt(cnpj.charAt(13)) !== digito2) {
return [false, 'Por favor, informe o número do CNPJ válido.'];
}

// // Checks if the CNPJ legal code is 213-5 - Empresário (Individual)
// Checks if the CNPJ legal code is 213-5 - Empresário (Individual)
const url = `https://www.receitaws.com.br/v1/cnpj/${cnpj}`;
const response = await fetch(url);
const data = await response.json();

if(data.status === 'ERROR') return [false, 'CNPJ inválido.'];
const natureza_juridica = data.natureza_juridica.replace(/\D/g, '');

if (natureza_juridica !== '2135') {
return [false, 'Por favor, informe um CNPJ que pertence ao MEI.'];
return [false, 'Por favor, informe um CNPJ válido que pertence ao MEI.'];
}

return [true];
Expand Down

0 comments on commit 59362c6

Please sign in to comment.