@x-spacy/class-validator 1.0.9
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/@types/decorators.d.ts +9 -0
- package/@types/enums.d.ts +6 -0
- package/@types/index.d.ts +3 -0
- package/@types/services.d.ts +5 -0
- package/LICENSE +9 -0
- package/README.md +13 -0
- package/dist/decorators/IsDocument.js +38 -0
- package/dist/enums/DocumentTypeEnum.js +8 -0
- package/dist/index.js +21 -0
- package/dist/services/IsCNPJ.js +24 -0
- package/dist/services/IsCPF.js +32 -0
- package/package.json +43 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare module '@x-spacy/validators' {
|
|
2
|
+
export type IsDocumentOptions = {
|
|
3
|
+
type: DocumentTypeEnum;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export declare function IsDocument(isDocumentOptions: IsDocumentOptions, validationOptions?: ValidationOptions): PropertyDecorator;
|
|
7
|
+
|
|
8
|
+
export * from 'class-validator';
|
|
9
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 X SPACY TECNOLOGIA LTDA <https://xspacy.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsDocument = IsDocument;
|
|
4
|
+
const class_validator_1 = require("class-validator");
|
|
5
|
+
const IsCNPJ_1 = require("../services/IsCNPJ");
|
|
6
|
+
const IsCPF_1 = require("../services/IsCPF");
|
|
7
|
+
const DocumentTypeEnum_1 = require("../enums/DocumentTypeEnum");
|
|
8
|
+
function IsDocument(isDocumentOptions, validationOptions) {
|
|
9
|
+
return function (object, propertyName) {
|
|
10
|
+
(0, class_validator_1.registerDecorator)({
|
|
11
|
+
name: 'IsDocument',
|
|
12
|
+
target: object.constructor,
|
|
13
|
+
propertyName: propertyName,
|
|
14
|
+
options: validationOptions,
|
|
15
|
+
validator: {
|
|
16
|
+
validate(document, _args) {
|
|
17
|
+
if (typeof document !== 'string') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
switch (isDocumentOptions.type) {
|
|
21
|
+
case DocumentTypeEnum_1.DocumentTypeEnum.CPF: {
|
|
22
|
+
return (0, IsCPF_1.IsCPF)(document);
|
|
23
|
+
}
|
|
24
|
+
case DocumentTypeEnum_1.DocumentTypeEnum.CNPJ: {
|
|
25
|
+
return (0, IsCNPJ_1.IsCNPJ)(document);
|
|
26
|
+
}
|
|
27
|
+
default: {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
defaultMessage() {
|
|
33
|
+
return 'Informe um documento válido';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentTypeEnum = void 0;
|
|
4
|
+
var DocumentTypeEnum;
|
|
5
|
+
(function (DocumentTypeEnum) {
|
|
6
|
+
DocumentTypeEnum["CPF"] = "CPF";
|
|
7
|
+
DocumentTypeEnum["CNPJ"] = "CNPJ";
|
|
8
|
+
})(DocumentTypeEnum || (exports.DocumentTypeEnum = DocumentTypeEnum = {}));
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./decorators/IsDocument"), exports);
|
|
18
|
+
__exportStar(require("./enums/DocumentTypeEnum"), exports);
|
|
19
|
+
__exportStar(require("./services/IsCNPJ"), exports);
|
|
20
|
+
__exportStar(require("./services/IsCPF"), exports);
|
|
21
|
+
__exportStar(require("class-validator"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsCNPJ = IsCNPJ;
|
|
4
|
+
function IsCNPJ(document) {
|
|
5
|
+
if (document.length !== 14) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
if (/^(\d)\1+$/.test(document)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const weights1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
12
|
+
const weights2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
13
|
+
let sum = 0;
|
|
14
|
+
for (let i = 0; i < 12; i++) {
|
|
15
|
+
sum += parseInt(document.charAt(i)) * weights1[i];
|
|
16
|
+
}
|
|
17
|
+
const digit1 = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
18
|
+
sum = 0;
|
|
19
|
+
for (let i = 0; i < 13; i++) {
|
|
20
|
+
sum += parseInt(document.charAt(i)) * weights2[i];
|
|
21
|
+
}
|
|
22
|
+
const digit2 = sum % 11 < 2 ? 0 : 11 - (sum % 11);
|
|
23
|
+
return digit1 === parseInt(document.charAt(12)) && digit2 === parseInt(document.charAt(13));
|
|
24
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsCPF = IsCPF;
|
|
4
|
+
function IsCPF(document) {
|
|
5
|
+
if (document.length !== 11) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const digits = document.split('').map(Number);
|
|
9
|
+
let sum = 0;
|
|
10
|
+
for (let i = 0; i < 9; i++) {
|
|
11
|
+
sum += digits[i] * (10 - i);
|
|
12
|
+
}
|
|
13
|
+
let firstVerifier = (sum * 10) % 11;
|
|
14
|
+
if (firstVerifier === 10 || firstVerifier === 11) {
|
|
15
|
+
firstVerifier = 0;
|
|
16
|
+
}
|
|
17
|
+
if (firstVerifier !== digits[9]) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
sum = 0;
|
|
21
|
+
for (let i = 0; i < 10; i++) {
|
|
22
|
+
sum += digits[i] * (11 - i);
|
|
23
|
+
}
|
|
24
|
+
let secondVerifier = (sum * 10) % 11;
|
|
25
|
+
if (secondVerifier === 10 || secondVerifier === 11) {
|
|
26
|
+
secondVerifier = 0;
|
|
27
|
+
}
|
|
28
|
+
if (secondVerifier !== digits[10]) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@x-spacy/class-validator",
|
|
3
|
+
"version": "1.0.9",
|
|
4
|
+
"description": "Validators extension for class-validator lib",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"private": false,
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "@types/index.d.ts",
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Vinícius Gutierrez da Silva Rocha",
|
|
11
|
+
"email": "viniciusgutierrez@x-spacy.com",
|
|
12
|
+
"url": "https://github.com/gutyerrez"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/x-spacy/class-validator.git"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"registry": "https://registry.npmjs.org",
|
|
20
|
+
"access": "public",
|
|
21
|
+
"tag": "latest"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": "v22.18.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"class-validator": "^0.14.2",
|
|
28
|
+
"validator": "^13.15.15"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"class-validator": ">=0.14.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@nestjs/cli": "^11.0.10",
|
|
35
|
+
"@types/node": "^22.17.1",
|
|
36
|
+
"@x-spacy/eslint-config": "^1.32.0",
|
|
37
|
+
"eslint": "^9.33.0",
|
|
38
|
+
"typescript": "^5.9.2"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "nest build"
|
|
42
|
+
}
|
|
43
|
+
}
|