validator-brazil 1.2.2 → 1.3.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -38
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/package.json +20 -14
- package/.github/workflows/nodejs.yml +0 -24
- package/src/index.d.ts +0 -17
- package/src/index.js +0 -89
- package/src/tests/cep.spec.js +0 -23
- package/src/tests/cnpj.spec.js +0 -23
- package/src/tests/cpf.spec.js +0 -23
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# validator-brazil
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Validate Brazilian CPF, CNPJ and CEP. Lightweight, zero dependencies, written in TypeScript.
|
|
4
|
+
|
|
5
|
+
Supports the **new alphanumeric CNPJ** format (IN RFB 2.229/2024), effective from July 2026.
|
|
4
6
|
|
|
5
7
|
[](https://badge.fury.io/js/validator-brazil)
|
|
6
8
|
|
|
@@ -9,7 +11,7 @@ With this module, you can validate the CPF, CNPJ and CEP numbers. Documents only
|
|
|
9
11
|
#### Install with NPM
|
|
10
12
|
|
|
11
13
|
```
|
|
12
|
-
$ npm install validator-brazil
|
|
14
|
+
$ npm install validator-brazil
|
|
13
15
|
```
|
|
14
16
|
|
|
15
17
|
#### Install with Yarn
|
|
@@ -18,59 +20,77 @@ $ npm install validator-brazil --save
|
|
|
18
20
|
$ yarn add validator-brazil
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
## Usage
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
### ES Modules
|
|
24
26
|
|
|
25
|
-
```
|
|
26
|
-
import { isCpf } from "validator-brazil";
|
|
27
|
+
```ts
|
|
28
|
+
import { isCpf, isCnpj, isCep } from "validator-brazil";
|
|
29
|
+
```
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
isCpf("29018170097"); // false
|
|
30
|
-
isCpf("12312345600"); // false
|
|
31
|
+
### CommonJS
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
isCpf("
|
|
33
|
+
```js
|
|
34
|
+
const { isCpf, isCnpj, isCep } = require("validator-brazil");
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
## API
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
import { isCnpj, isCpf, isCep } from "validator-brazil";
|
|
39
|
+
### `isCpf(cpf: string): boolean`
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
isCnpj("54334068000136"); // true
|
|
43
|
-
isCnpj("00111222000100"); // false
|
|
41
|
+
Validates a Brazilian CPF (Cadastro de Pessoas Fisicas). Accepts input with or without mask.
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
```ts
|
|
44
|
+
isCpf("14552586017"); // true
|
|
45
|
+
isCpf("145.525.860-17"); // true
|
|
46
|
+
isCpf("12312345600"); // false
|
|
47
|
+
isCpf("123.123.456-00"); // false
|
|
47
48
|
```
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
### `isCnpj(cnpj: string): boolean`
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
import { isCep } from "validator-brazil";
|
|
52
|
+
Validates a Brazilian CNPJ (Cadastro Nacional da Pessoa Juridica). Accepts input with or without mask, both traditional numeric and the new alphanumeric format. Case insensitive.
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
isCep("43710130"); // true
|
|
56
|
-
isCep("5471013423"); // false
|
|
54
|
+
#### Traditional (numeric)
|
|
57
55
|
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
```ts
|
|
57
|
+
isCnpj("54334068000136"); // true
|
|
58
|
+
isCnpj("54.334.068/0001-36"); // true
|
|
59
|
+
isCnpj("00111222000100"); // false
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
####
|
|
62
|
+
#### Alphanumeric (new format - July 2026)
|
|
63
63
|
|
|
64
|
-
```
|
|
65
|
-
|
|
64
|
+
```ts
|
|
65
|
+
isCnpj("12ABC34501DE35"); // true
|
|
66
|
+
isCnpj("12.ABC.345/01DE-35"); // true
|
|
67
|
+
isCnpj("Y0W9NJBN000176"); // true
|
|
68
|
+
isCnpj("Y0.W9N.JBN/0001-76"); // true
|
|
69
|
+
isCnpj("12abc34501de35"); // true (case insensitive)
|
|
70
|
+
```
|
|
66
71
|
|
|
67
|
-
|
|
68
|
-
validator.isCnpj("54334068000136"); // true
|
|
69
|
-
validator.isCnpj("00111222000100"); // false
|
|
70
|
-
validator.isCpf("29018170097"); // true
|
|
71
|
-
validator.isCpf("12312345600"); // false
|
|
72
|
+
### `isCep(cep: string): boolean`
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
Validates a Brazilian CEP (Codigo de Enderecamento Postal). Accepts input with or without hyphen.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
isCep("43710130"); // true
|
|
78
|
+
isCep("43710-130"); // true
|
|
79
|
+
isCep("5471013423"); // false
|
|
76
80
|
```
|
|
81
|
+
|
|
82
|
+
## Alphanumeric CNPJ
|
|
83
|
+
|
|
84
|
+
Starting July 2026, the Brazilian Federal Revenue Service (Receita Federal) will issue CNPJs containing letters (A-Z) in addition to digits. The format remains 14 characters:
|
|
85
|
+
|
|
86
|
+
| Positions | Content | Allowed characters |
|
|
87
|
+
|-----------|---------|-------------------|
|
|
88
|
+
| 1-8 | Root | `A-Z`, `0-9` |
|
|
89
|
+
| 9-12 | Order | `A-Z`, `0-9` |
|
|
90
|
+
| 13-14 | Check digits | `0-9` |
|
|
91
|
+
|
|
92
|
+
Existing numeric CNPJs remain valid and unchanged. This library handles both formats transparently.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates both traditional (numeric) and new alphanumeric CNPJ format.
|
|
3
|
+
* Alphanumeric CNPJ: positions 1-12 accept [A-Z0-9], positions 13-14 are numeric check digits.
|
|
4
|
+
* Check digit calculation uses Modulo 11 with ASCII-48 character conversion (SERPRO spec).
|
|
5
|
+
* @param cnpj Cadastro Nacional da Pessoa Jurídica
|
|
6
|
+
*/
|
|
7
|
+
export declare function isCnpj(cnpj: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* @param cpf Cadastro de Pessoas Físicas
|
|
10
|
+
*/
|
|
11
|
+
export declare function isCpf(cpf: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @param cep Código de Endereçamento Postal
|
|
14
|
+
*/
|
|
15
|
+
export declare function isCep(cep: string): boolean;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAsB5C;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA0B1C;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAG1C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isCnpj = isCnpj;
|
|
4
|
+
exports.isCpf = isCpf;
|
|
5
|
+
exports.isCep = isCep;
|
|
6
|
+
const STRIP_REGEX = /[.\-/]+/g;
|
|
7
|
+
const CNPJ_FORMAT = /^[A-Z0-9]{12}\d{2}$/;
|
|
8
|
+
const CNPJ_WEIGHTS_1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
9
|
+
const CNPJ_WEIGHTS_2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
10
|
+
function charValue(c) {
|
|
11
|
+
return c.charCodeAt(0) - 48;
|
|
12
|
+
}
|
|
13
|
+
const INVALID_CPF = new Set([
|
|
14
|
+
"00000000000",
|
|
15
|
+
"11111111111",
|
|
16
|
+
"22222222222",
|
|
17
|
+
"33333333333",
|
|
18
|
+
"44444444444",
|
|
19
|
+
"55555555555",
|
|
20
|
+
"66666666666",
|
|
21
|
+
"77777777777",
|
|
22
|
+
"88888888888",
|
|
23
|
+
"99999999999",
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* Validates both traditional (numeric) and new alphanumeric CNPJ format.
|
|
27
|
+
* Alphanumeric CNPJ: positions 1-12 accept [A-Z0-9], positions 13-14 are numeric check digits.
|
|
28
|
+
* Check digit calculation uses Modulo 11 with ASCII-48 character conversion (SERPRO spec).
|
|
29
|
+
* @param cnpj Cadastro Nacional da Pessoa Jurídica
|
|
30
|
+
*/
|
|
31
|
+
function isCnpj(cnpj) {
|
|
32
|
+
cnpj = cnpj.replace(STRIP_REGEX, "").toUpperCase();
|
|
33
|
+
if (!CNPJ_FORMAT.test(cnpj) || new Set(cnpj).size === 1) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
const base = cnpj.substring(0, 12);
|
|
37
|
+
let sum = 0;
|
|
38
|
+
for (let i = 0; i < 12; i++) {
|
|
39
|
+
sum += charValue(base.charAt(i)) * CNPJ_WEIGHTS_1[i];
|
|
40
|
+
}
|
|
41
|
+
const dv1 = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
42
|
+
const baseWithDv1 = base + String(dv1);
|
|
43
|
+
sum = 0;
|
|
44
|
+
for (let i = 0; i < 13; i++) {
|
|
45
|
+
sum += charValue(baseWithDv1.charAt(i)) * CNPJ_WEIGHTS_2[i];
|
|
46
|
+
}
|
|
47
|
+
const dv2 = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
48
|
+
return cnpj.substring(12) === `${dv1}${dv2}`;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @param cpf Cadastro de Pessoas Físicas
|
|
52
|
+
*/
|
|
53
|
+
function isCpf(cpf) {
|
|
54
|
+
cpf = cpf.replace(STRIP_REGEX, "");
|
|
55
|
+
if (cpf === "" || cpf.length !== 11 || INVALID_CPF.has(cpf)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
let add = 0;
|
|
59
|
+
for (let i = 0; i < 9; i++) {
|
|
60
|
+
add += parseInt(cpf.charAt(i), 10) * (10 - i);
|
|
61
|
+
}
|
|
62
|
+
let rev = 11 - (add % 11);
|
|
63
|
+
if (rev === 10 || rev === 11)
|
|
64
|
+
rev = 0;
|
|
65
|
+
if (rev !== parseInt(cpf.charAt(9), 10))
|
|
66
|
+
return false;
|
|
67
|
+
add = 0;
|
|
68
|
+
for (let i = 0; i < 10; i++) {
|
|
69
|
+
add += parseInt(cpf.charAt(i), 10) * (11 - i);
|
|
70
|
+
}
|
|
71
|
+
rev = 11 - (add % 11);
|
|
72
|
+
if (rev === 10 || rev === 11)
|
|
73
|
+
rev = 0;
|
|
74
|
+
if (rev !== parseInt(cpf.charAt(10), 10))
|
|
75
|
+
return false;
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @param cep Código de Endereçamento Postal
|
|
80
|
+
*/
|
|
81
|
+
function isCep(cep) {
|
|
82
|
+
cep = cep.replace(STRIP_REGEX, "");
|
|
83
|
+
return /^[0-9]{8}$/.test(cep);
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA6BA,wBAsBC;AAKD,sBA0BC;AAKD,sBAGC;AA1FD,MAAM,WAAW,GAAG,UAAU,CAAC;AAE/B,MAAM,WAAW,GAAG,qBAAqB,CAAC;AAC1C,MAAM,cAAc,GAAG,CAAC,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
|
4
|
+
"description": "Validate Brazilian CPF, CNPJ (including alphanumeric) and CEP",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
8
13
|
"test": "jest"
|
|
9
14
|
},
|
|
10
15
|
"repository": {
|
|
11
16
|
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/lourencorodrigo/
|
|
17
|
+
"url": "git+https://github.com/lourencorodrigo/validator-brazil.git"
|
|
13
18
|
},
|
|
14
19
|
"keywords": [
|
|
15
20
|
"cpf",
|
|
@@ -22,19 +27,20 @@
|
|
|
22
27
|
"author": "Rodrigo Lourenço <rsilvape@gmail.com>",
|
|
23
28
|
"license": "MIT",
|
|
24
29
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/lourencorodrigo/
|
|
30
|
+
"url": "https://github.com/lourencorodrigo/validator-brazil/issues"
|
|
26
31
|
},
|
|
27
|
-
"homepage": "https://github.com/lourencorodrigo/
|
|
32
|
+
"homepage": "https://github.com/lourencorodrigo/validator-brazil#readme",
|
|
28
33
|
"devDependencies": {
|
|
29
|
-
"jest": "^
|
|
34
|
+
"@types/jest": "^29.5.14",
|
|
35
|
+
"jest": "^29.7.0",
|
|
36
|
+
"ts-jest": "^29.3.2",
|
|
37
|
+
"typescript": "^5.8.3"
|
|
30
38
|
},
|
|
31
39
|
"jest": {
|
|
40
|
+
"preset": "ts-jest",
|
|
41
|
+
"testEnvironment": "node",
|
|
32
42
|
"testMatch": [
|
|
33
|
-
"<rootDir>/src
|
|
34
|
-
|
|
35
|
-
],
|
|
36
|
-
"transform": {
|
|
37
|
-
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest"
|
|
38
|
-
}
|
|
43
|
+
"<rootDir>/src/**/*.spec.ts"
|
|
44
|
+
]
|
|
39
45
|
}
|
|
40
46
|
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
name: Node.js CI
|
|
2
|
-
|
|
3
|
-
on: [push]
|
|
4
|
-
|
|
5
|
-
jobs:
|
|
6
|
-
build:
|
|
7
|
-
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
|
|
10
|
-
strategy:
|
|
11
|
-
matrix:
|
|
12
|
-
node-version: [8.x, 10.x, 12.x]
|
|
13
|
-
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v2
|
|
16
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
17
|
-
uses: actions/setup-node@v1
|
|
18
|
-
with:
|
|
19
|
-
node-version: ${{ matrix.node-version }}
|
|
20
|
-
- run: npm install
|
|
21
|
-
- run: npm run build --if-present
|
|
22
|
-
- run: npm test
|
|
23
|
-
env:
|
|
24
|
-
CI: true
|
package/src/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retorna true se o CNPJ estiver válido
|
|
3
|
-
* @param cnpj Cadastro Nacional da Pessoa Jurídica
|
|
4
|
-
*/
|
|
5
|
-
export function isCnpj(cnpj: string): boolean;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Retorna true se o CPF estiver válido
|
|
9
|
-
* @param cpf Cadastro de Pessoas Físicas
|
|
10
|
-
*/
|
|
11
|
-
export function isCpf(cpf: string): boolean;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Retorna true se o CEP estiver válido
|
|
15
|
-
* @param cep Código de Endereçamento Postal
|
|
16
|
-
*/
|
|
17
|
-
export function isCep(cep: string): boolean;
|
package/src/index.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
var regex = /[\.\-\/]+/g;
|
|
2
|
-
|
|
3
|
-
module.exports.isCnpj = function(cnpj) {
|
|
4
|
-
cnpj = cnpj.replace(regex, "");
|
|
5
|
-
|
|
6
|
-
if (cnpj == "") return false;
|
|
7
|
-
|
|
8
|
-
if (cnpj.length != 14) return false;
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
cnpj == "00000000000000" ||
|
|
12
|
-
cnpj == "11111111111111" ||
|
|
13
|
-
cnpj == "22222222222222" ||
|
|
14
|
-
cnpj == "33333333333333" ||
|
|
15
|
-
cnpj == "44444444444444" ||
|
|
16
|
-
cnpj == "55555555555555" ||
|
|
17
|
-
cnpj == "66666666666666" ||
|
|
18
|
-
cnpj == "77777777777777" ||
|
|
19
|
-
cnpj == "88888888888888" ||
|
|
20
|
-
cnpj == "99999999999999"
|
|
21
|
-
)
|
|
22
|
-
return false;
|
|
23
|
-
|
|
24
|
-
var size = cnpj.length - 2;
|
|
25
|
-
var numbers = cnpj.substring(0, size);
|
|
26
|
-
var digits = cnpj.substring(size);
|
|
27
|
-
var sum = 0;
|
|
28
|
-
var pos = size - 7;
|
|
29
|
-
for (var i = size; i >= 1; i--) {
|
|
30
|
-
sum += numbers.charAt(size - i) * pos--;
|
|
31
|
-
if (pos < 2) pos = 9;
|
|
32
|
-
}
|
|
33
|
-
var result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
34
|
-
if (result != digits.charAt(0)) return false;
|
|
35
|
-
|
|
36
|
-
size = size + 1;
|
|
37
|
-
numbers = cnpj.substring(0, size);
|
|
38
|
-
sum = 0;
|
|
39
|
-
pos = size - 7;
|
|
40
|
-
for (var i = size; i >= 1; i--) {
|
|
41
|
-
sum += numbers.charAt(size - i) * pos--;
|
|
42
|
-
if (pos < 2) pos = 9;
|
|
43
|
-
}
|
|
44
|
-
result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
45
|
-
if (result != digits.charAt(1)) return false;
|
|
46
|
-
|
|
47
|
-
return true;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
module.exports.isCpf = function(cpf) {
|
|
51
|
-
cpf = cpf.replace(regex, "");
|
|
52
|
-
|
|
53
|
-
if (cpf == "") return false;
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
cpf.length != 11 ||
|
|
57
|
-
cpf == "00000000000" ||
|
|
58
|
-
cpf == "11111111111" ||
|
|
59
|
-
cpf == "22222222222" ||
|
|
60
|
-
cpf == "33333333333" ||
|
|
61
|
-
cpf == "44444444444" ||
|
|
62
|
-
cpf == "55555555555" ||
|
|
63
|
-
cpf == "66666666666" ||
|
|
64
|
-
cpf == "77777777777" ||
|
|
65
|
-
cpf == "88888888888" ||
|
|
66
|
-
cpf == "99999999999"
|
|
67
|
-
)
|
|
68
|
-
return false;
|
|
69
|
-
|
|
70
|
-
var add = 0;
|
|
71
|
-
for (var i = 0; i < 9; i++) add += parseInt(cpf.charAt(i)) * (10 - i);
|
|
72
|
-
var rev = 11 - (add % 11);
|
|
73
|
-
if (rev == 10 || rev == 11) rev = 0;
|
|
74
|
-
if (rev != parseInt(cpf.charAt(9))) return false;
|
|
75
|
-
|
|
76
|
-
add = 0;
|
|
77
|
-
|
|
78
|
-
for (var i = 0; i < 10; i++) add += parseInt(cpf.charAt(i)) * (11 - i);
|
|
79
|
-
rev = 11 - (add % 11);
|
|
80
|
-
if (rev == 10 || rev == 11) rev = 0;
|
|
81
|
-
if (rev != parseInt(cpf.charAt(10))) return false;
|
|
82
|
-
return true;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
module.exports.isCep = function(cep) {
|
|
86
|
-
cep = cep.replace(regex, "");
|
|
87
|
-
var cepRegex = /^[0-9]{8}$/g;
|
|
88
|
-
return cepRegex.test(cep);
|
|
89
|
-
};
|
package/src/tests/cep.spec.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const isCep = require("../index").isCep;
|
|
2
|
-
|
|
3
|
-
describe("cep validation", () => {
|
|
4
|
-
it("should return true to valid cep without hyphen", () => {
|
|
5
|
-
expect(isCep("14710130")).toBeTruthy();
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
it("should return true to invalid cep with 0 in the initial", () => {
|
|
9
|
-
expect(isCep("04710130")).toBeTruthy();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should return true to valid cep WITH hyphen", () => {
|
|
13
|
-
expect(isCep("54710-130")).toBeTruthy();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("should return true to valid cep WITHOUT hyphen", () => {
|
|
17
|
-
expect(isCep("54710130")).toBeTruthy();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("should return false to invalid cep", () => {
|
|
21
|
-
expect(isCep("5471012023")).toBeFalsy()
|
|
22
|
-
});
|
|
23
|
-
});
|
package/src/tests/cnpj.spec.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const isCnpj = require("../index").isCnpj;
|
|
2
|
-
|
|
3
|
-
describe("cnpj validation", () => {
|
|
4
|
-
it("should return true to valid cnpj without hyphen and points", () => {
|
|
5
|
-
expect(isCnpj("00933180000164")).toBeTruthy();
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
it("should return true to valid cnpj with hyphen and points", () => {
|
|
9
|
-
expect(isCnpj("00.933.180/0001-64")).toBeTruthy();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should return false with letters", () => {
|
|
13
|
-
expect(isCnpj("00933180000164a")).toBeFalsy();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("should return false to invalid cnpj with hyphen and points", () => {
|
|
17
|
-
expect(isCnpj("001112220000133")).toBeFalsy();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("should return false to invalid cnpj", () => {
|
|
21
|
-
expect(isCnpj("00000000000000")).toBeFalsy();
|
|
22
|
-
});
|
|
23
|
-
});
|
package/src/tests/cpf.spec.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const isCpf = require("../index").isCpf;
|
|
2
|
-
|
|
3
|
-
describe("cpf validation", () => {
|
|
4
|
-
it("should return true to valid cpf wythout hyphen and points", () => {
|
|
5
|
-
expect(isCpf("14552586017")).toBeTruthy();
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
it("should return true to valid cpf with hyphen and points", () => {
|
|
9
|
-
expect(isCpf("145.525.860-17")).toBeTruthy();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should return false with letters", () => {
|
|
13
|
-
expect(isCpf("14552586017a")).toBeFalsy();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("should return false to invalid cpf wythout hyphen and points", () => {
|
|
17
|
-
expect(isCpf("00011122233")).toBeFalsy();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("should return false to invalid cpf with hyphen and points", () => {
|
|
21
|
-
expect(isCpf("000.111.222-33")).toBeFalsy();
|
|
22
|
-
});
|
|
23
|
-
});
|