Skip to content

Commit 7d04726

Browse files
committed
refactor: streamline CNPJ validation logic and update error messages
1 parent 4001d85 commit 7d04726

File tree

3 files changed

+33
-42
lines changed

3 files changed

+33
-42
lines changed

src/cnpjValidator/cnpjValidator1.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,6 @@ function cnpjIsValid(
5151
cnpj: string,
5252
errorMsg: (string | null)[] | null = defaultErrorMsg,
5353
): ValidateFunctions {
54-
if (typeof cnpj !== "string") {
55-
throw new TypeError("The input should be a string.");
56-
}
57-
// Check para saber se as mensagens que sao passadas sao validas
58-
// caso contrario retorna um ERRO
59-
if (errorMsg) {
60-
if (!Array.isArray(errorMsg)) throw new Error("Must be an Array");
61-
for (const element of errorMsg) {
62-
if (element != null && typeof element !== "string") {
63-
throw new TypeError(
64-
"All values within the array must be strings or null/undefined.",
65-
);
66-
}
67-
}
68-
}
69-
7054
// Função interna para obter a mensagem de erro
7155
function getErrorMessage(index: number): string {
7256
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;
@@ -108,4 +92,5 @@ function cnpjIsValid(
10892
errorMsg: getErrorMessage(2), // 'CNPJ is not valid'
10993
};
11094
}
95+
11196
export default cnpjIsValid;

src/cnpjValidator/cnpjValidator2.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ValidateFunctions } from "../types";
22

33
const defaultErrorMsg: string[] = [
44
"CNPJ invalid",
5-
"CNPJ must have 14 numerical digits",
5+
"CNPJ must have 14 alphanumerical digits",
66
"CNPJ is not valid",
77
];
88

@@ -73,22 +73,6 @@ function cnpjIsValid(
7373
cnpj: string,
7474
errorMsg: (string | null)[] | null = defaultErrorMsg,
7575
): ValidateFunctions {
76-
if (typeof cnpj !== "string") {
77-
throw new TypeError("The input should be a string.");
78-
}
79-
// Check para saber se as mensagens que sao passadas sao validas
80-
// caso contrario retorna um ERRO
81-
if (errorMsg) {
82-
if (!Array.isArray(errorMsg)) throw new Error("Must be an Array");
83-
for (const element of errorMsg) {
84-
if (element != null && typeof element !== "string") {
85-
throw new TypeError(
86-
"All values within the array must be strings or null/undefined.",
87-
);
88-
}
89-
}
90-
}
91-
9276
// Função interna para obter a mensagem de erro
9377
function getErrorMessage(index: number): string {
9478
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;

src/cnpjValidator/index.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,44 @@ import type { ValidateFunctions } from "../types";
22
import cnpjIsValid1 from "./cnpjValidator1";
33
import cnpjIsValid2 from "./cnpjValidator2";
44

5-
const defaultErrorMsg: string[] = [
6-
"CNPJ invalid",
7-
"CNPJ must have 14 numerical digits",
8-
"CNPJ is not valid",
9-
];
10-
115
function cnpjIsValid(
126
cnpj: string,
13-
errorMsg: (string | null)[] | null = defaultErrorMsg,
14-
cnpjVersion: "v1" | "v2" = "v1",
7+
errorMsg: (string | null)[] | null = null,
8+
cnpjVersion: "v1" | "v2" | null = null,
159
): ValidateFunctions {
10+
if (typeof cnpj !== "string") {
11+
throw new TypeError("The input should be a string.");
12+
}
13+
14+
// Check para saber se as mensagens que sao passadas sao validas
15+
// caso contrario retorna um ERRO
16+
if (errorMsg) {
17+
if (!Array.isArray(errorMsg)) throw new Error("Must be an Array");
18+
19+
for (const element of errorMsg) {
20+
if (element != null && typeof element !== "string") {
21+
throw new TypeError(
22+
"All values within the array must be strings or null/undefined.",
23+
);
24+
}
25+
}
26+
}
27+
28+
if (cnpjVersion === "v1") {
29+
return cnpjIsValid1(cnpj, errorMsg);
30+
}
31+
1632
if (cnpjVersion === "v2") {
1733
return cnpjIsValid2(cnpj, errorMsg);
1834
}
1935

20-
return cnpjIsValid1(cnpj, errorMsg);
36+
const isOnlyNumbers: boolean = /^\d+$/.test(cnpj.replace(/\D/g, ""));
37+
38+
if (isOnlyNumbers) {
39+
return cnpjIsValid1(cnpj, errorMsg);
40+
}
41+
42+
return cnpjIsValid2(cnpj, errorMsg);
2143
}
2244

2345
export default cnpjIsValid;

0 commit comments

Comments
 (0)