@pristine-ts/validation 0.0.276 → 0.0.278
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/dist/lib/cjs/decorators/body-validation.decorator.js +5 -17
- package/dist/lib/cjs/decorators/body-validation.decorator.js.map +1 -1
- package/dist/lib/cjs/interceptors/body-validation.request-interceptor.js +4 -2
- package/dist/lib/cjs/interceptors/body-validation.request-interceptor.js.map +1 -1
- package/dist/lib/esm/decorators/body-validation.decorator.js +4 -16
- package/dist/lib/esm/decorators/body-validation.decorator.js.map +1 -1
- package/dist/lib/esm/interceptors/body-validation.request-interceptor.js +4 -2
- package/dist/lib/esm/interceptors/body-validation.request-interceptor.js.map +1 -1
- package/dist/types/decorators/body-validation.decorator.d.ts +1 -0
- package/package.json +6 -6
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bodyValidation = void 0;
|
|
3
|
+
exports.bodyValidation = exports.bodyValidationMetadataKeyname = void 0;
|
|
4
|
+
const common_1 = require("@pristine-ts/common");
|
|
5
|
+
exports.bodyValidationMetadataKeyname = "@bodyValidation";
|
|
4
6
|
/**
|
|
5
7
|
* The bodyValidation decorator can be used to validate the body of a request.
|
|
6
8
|
* @param classType The class that the request body is expected to fit.
|
|
@@ -19,23 +21,9 @@ const bodyValidation = (classType) => {
|
|
|
19
21
|
* The descriptor of the property.
|
|
20
22
|
*/
|
|
21
23
|
descriptor) => {
|
|
22
|
-
|
|
23
|
-
if (target.constructor.prototype.hasOwnProperty("__metadata__") === false) {
|
|
24
|
-
target.constructor.prototype["__metadata__"] = {};
|
|
25
|
-
}
|
|
26
|
-
if (target.constructor.prototype["__metadata__"].hasOwnProperty("methods") === false) {
|
|
27
|
-
target.constructor.prototype["__metadata__"]["methods"] = {};
|
|
28
|
-
}
|
|
29
|
-
if (target.constructor.prototype["__metadata__"]["methods"].hasOwnProperty(propertyKey) === false) {
|
|
30
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey] = {};
|
|
31
|
-
}
|
|
32
|
-
if (target.constructor.prototype["__metadata__"]["methods"][propertyKey].hasOwnProperty("__routeContext__") === false) {
|
|
33
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey]["__routeContext__"] = {};
|
|
34
|
-
}
|
|
35
|
-
// Adds the validation to the route context.
|
|
36
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey]["__routeContext__"]["bodyValidator"] = {
|
|
24
|
+
common_1.MetadataUtil.setToRouteContext(exports.bodyValidationMetadataKeyname, {
|
|
37
25
|
classType,
|
|
38
|
-
};
|
|
26
|
+
}, target, propertyKey);
|
|
39
27
|
};
|
|
40
28
|
};
|
|
41
29
|
exports.bodyValidation = bodyValidation;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"body-validation.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/body-validation.decorator.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACI,MAAM,cAAc,GAAG,CAAC,SAAmB,EAAE,EAAE;IAClD,OAAO;IACH;;OAEG;IACH,MAAW;IAEX;;OAEG;IACH,WAA4B;IAE5B;;OAEG;IACH,UAA8B,EAChC,EAAE;
|
|
1
|
+
{"version":3,"file":"body-validation.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/body-validation.decorator.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAEpC,QAAA,6BAA6B,GAAG,iBAAiB,CAAC;AAE/D;;;GAGG;AACI,MAAM,cAAc,GAAG,CAAC,SAAmB,EAAE,EAAE;IAClD,OAAO;IACH;;OAEG;IACH,MAAW;IAEX;;OAEG;IACH,WAA4B;IAE5B;;OAEG;IACH,UAA8B,EAChC,EAAE;QACA,qBAAY,CAAC,iBAAiB,CAAC,qCAA6B,EAAE;YAC1D,SAAS;SACZ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC5B,CAAC,CAAA;AACL,CAAC,CAAA;AArBY,QAAA,cAAc,kBAqB1B"}
|
|
@@ -28,6 +28,7 @@ const common_1 = require("@pristine-ts/common");
|
|
|
28
28
|
const validation_module_keyname_1 = require("../validation.module.keyname");
|
|
29
29
|
const tsyringe_1 = require("tsyringe");
|
|
30
30
|
const class_transformer_1 = require("class-transformer");
|
|
31
|
+
const body_validation_decorator_1 = require("../decorators/body-validation.decorator");
|
|
31
32
|
/**
|
|
32
33
|
* This class is an interceptor to validate the body of an incoming request.
|
|
33
34
|
* It is tagged as an RequestInterceptor so it can be automatically injected with the all the other RequestInterceptors.
|
|
@@ -53,7 +54,8 @@ let BodyValidationRequestInterceptor = class BodyValidationRequestInterceptor {
|
|
|
53
54
|
*/
|
|
54
55
|
interceptRequest(request, methodNode) {
|
|
55
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
|
|
57
|
+
const bodyValidator = methodNode.route.context[body_validation_decorator_1.bodyValidationMetadataKeyname];
|
|
58
|
+
if (bodyValidator === undefined || bodyValidator.classType === undefined) {
|
|
57
59
|
return request;
|
|
58
60
|
}
|
|
59
61
|
this.loghandler.debug("BodyValidationRequestEnricher", {
|
|
@@ -62,7 +64,7 @@ let BodyValidationRequestInterceptor = class BodyValidationRequestInterceptor {
|
|
|
62
64
|
routeContext: methodNode.route.context,
|
|
63
65
|
}, validation_module_keyname_1.ValidationModuleKeyname);
|
|
64
66
|
// Validates that the body can be mapped to the expected type
|
|
65
|
-
const mappedBody = (0, class_transformer_1.plainToInstance)(
|
|
67
|
+
const mappedBody = (0, class_transformer_1.plainToInstance)(bodyValidator.classType, request.body);
|
|
66
68
|
// Validates if all the conditions are respected in the expected type.
|
|
67
69
|
const errors = yield this.validator.validate(mappedBody);
|
|
68
70
|
if (errors.length == 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"body-validation.request-interceptor.js","sourceRoot":"","sources":["../../../../src/interceptors/body-validation.request-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AACA,kEAAuD;AACvD,wDAA4D;AAC5D,gDAAyF;AACzF,4EAAqE;AACrE,uCAA4C;AAC5C,yDAAoD;
|
|
1
|
+
{"version":3,"file":"body-validation.request-interceptor.js","sourceRoot":"","sources":["../../../../src/interceptors/body-validation.request-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AACA,kEAAuD;AACvD,wDAA4D;AAC5D,gDAAyF;AACzF,4EAAqE;AACrE,uCAA4C;AAC5C,yDAAoD;AAEpD,uFAAsF;AAEtF;;;;GAIG;AAII,IAAM,gCAAgC,GAAtC,MAAM,gCAAgC;IAEzC;;;;;;OAMG;IACH,YAA4D,UAA+B,EAC9D,SAAoB;QADW,eAAU,GAAV,UAAU,CAAqB;QAC9D,cAAS,GAAT,SAAS,CAAW;IACjD,CAAC;IAED;;;;;OAKG;IACG,gBAAgB,CAAC,OAAgB,EAAE,UAA4B;;YACjE,MAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,yDAA6B,CAAC,CAAC;YAE9E,IAAG,aAAa,KAAK,SAAS,IAAI,aAAa,CAAC,SAAS,KAAK,SAAS,EAAE;gBACrE,OAAO,OAAO,CAAC;aAClB;YAED,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,+BAA+B,EAAE;gBACnD,OAAO;gBACP,UAAU;gBACV,YAAY,EAAE,UAAU,CAAC,KAAK,CAAC,OAAO;aACzC,EAAE,mDAAuB,CAAC,CAAA;YAE3B,6DAA6D;YAC7D,MAAM,UAAU,GAAG,IAAA,mCAAe,EAAC,aAAa,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;YAE1E,sEAAsE;YACtE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YAEzD,IAAG,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE;gBACnB,OAAO,OAAO,CAAC;aAClB;YAED,6EAA6E;YAC7E,MAAM,IAAI,gCAAmB,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC;QAC9D,CAAC;KAAA;CACJ,CAAA;AA7CY,4EAAgC;2CAAhC,gCAAgC;IAH5C,IAAA,qBAAY,EAAC,mDAAuB,CAAC;IACrC,IAAA,YAAG,EAAC,iCAAwB,CAAC,kBAAkB,CAAC;IAChD,IAAA,qBAAU,GAAE;IAUI,WAAA,IAAA,iBAAM,EAAC,qBAAqB,CAAC,CAAA;6CACF,2BAAS;GAVxC,gCAAgC,CA6C5C"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { MetadataUtil } from "@pristine-ts/common";
|
|
2
|
+
export const bodyValidationMetadataKeyname = "@bodyValidation";
|
|
1
3
|
/**
|
|
2
4
|
* The bodyValidation decorator can be used to validate the body of a request.
|
|
3
5
|
* @param classType The class that the request body is expected to fit.
|
|
@@ -16,23 +18,9 @@ export const bodyValidation = (classType) => {
|
|
|
16
18
|
* The descriptor of the property.
|
|
17
19
|
*/
|
|
18
20
|
descriptor) => {
|
|
19
|
-
|
|
20
|
-
if (target.constructor.prototype.hasOwnProperty("__metadata__") === false) {
|
|
21
|
-
target.constructor.prototype["__metadata__"] = {};
|
|
22
|
-
}
|
|
23
|
-
if (target.constructor.prototype["__metadata__"].hasOwnProperty("methods") === false) {
|
|
24
|
-
target.constructor.prototype["__metadata__"]["methods"] = {};
|
|
25
|
-
}
|
|
26
|
-
if (target.constructor.prototype["__metadata__"]["methods"].hasOwnProperty(propertyKey) === false) {
|
|
27
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey] = {};
|
|
28
|
-
}
|
|
29
|
-
if (target.constructor.prototype["__metadata__"]["methods"][propertyKey].hasOwnProperty("__routeContext__") === false) {
|
|
30
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey]["__routeContext__"] = {};
|
|
31
|
-
}
|
|
32
|
-
// Adds the validation to the route context.
|
|
33
|
-
target.constructor.prototype["__metadata__"]["methods"][propertyKey]["__routeContext__"]["bodyValidator"] = {
|
|
21
|
+
MetadataUtil.setToRouteContext(bodyValidationMetadataKeyname, {
|
|
34
22
|
classType,
|
|
35
|
-
};
|
|
23
|
+
}, target, propertyKey);
|
|
36
24
|
};
|
|
37
25
|
};
|
|
38
26
|
//# sourceMappingURL=body-validation.decorator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"body-validation.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/body-validation.decorator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,SAAmB,EAAE,EAAE;IAClD,OAAO;IACH;;OAEG;IACH,MAAW;IAEX;;OAEG;IACH,WAA4B;IAE5B;;OAEG;IACH,UAA8B,EAChC,EAAE;
|
|
1
|
+
{"version":3,"file":"body-validation.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/body-validation.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAEjD,MAAM,CAAC,MAAM,6BAA6B,GAAG,iBAAiB,CAAC;AAE/D;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,SAAmB,EAAE,EAAE;IAClD,OAAO;IACH;;OAEG;IACH,MAAW;IAEX;;OAEG;IACH,WAA4B;IAE5B;;OAEG;IACH,UAA8B,EAChC,EAAE;QACA,YAAY,CAAC,iBAAiB,CAAC,6BAA6B,EAAE;YAC1D,SAAS;SACZ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC5B,CAAC,CAAA;AACL,CAAC,CAAA"}
|
|
@@ -25,6 +25,7 @@ import { moduleScoped, ServiceDefinitionTagEnum, tag } from "@pristine-ts/common
|
|
|
25
25
|
import { ValidationModuleKeyname } from "../validation.module.keyname";
|
|
26
26
|
import { injectable, inject } from "tsyringe";
|
|
27
27
|
import { plainToInstance } from 'class-transformer';
|
|
28
|
+
import { bodyValidationMetadataKeyname } from "../decorators/body-validation.decorator";
|
|
28
29
|
/**
|
|
29
30
|
* This class is an interceptor to validate the body of an incoming request.
|
|
30
31
|
* It is tagged as an RequestInterceptor so it can be automatically injected with the all the other RequestInterceptors.
|
|
@@ -50,7 +51,8 @@ let BodyValidationRequestInterceptor = class BodyValidationRequestInterceptor {
|
|
|
50
51
|
*/
|
|
51
52
|
interceptRequest(request, methodNode) {
|
|
52
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
|
|
54
|
+
const bodyValidator = methodNode.route.context[bodyValidationMetadataKeyname];
|
|
55
|
+
if (bodyValidator === undefined || bodyValidator.classType === undefined) {
|
|
54
56
|
return request;
|
|
55
57
|
}
|
|
56
58
|
this.loghandler.debug("BodyValidationRequestEnricher", {
|
|
@@ -59,7 +61,7 @@ let BodyValidationRequestInterceptor = class BodyValidationRequestInterceptor {
|
|
|
59
61
|
routeContext: methodNode.route.context,
|
|
60
62
|
}, ValidationModuleKeyname);
|
|
61
63
|
// Validates that the body can be mapped to the expected type
|
|
62
|
-
const mappedBody = plainToInstance(
|
|
64
|
+
const mappedBody = plainToInstance(bodyValidator.classType, request.body);
|
|
63
65
|
// Validates if all the conditions are respected in the expected type.
|
|
64
66
|
const errors = yield this.validator.validate(mappedBody);
|
|
65
67
|
if (errors.length == 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"body-validation.request-interceptor.js","sourceRoot":"","sources":["../../../../src/interceptors/body-validation.request-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAC,SAAS,EAAC,MAAM,8BAA8B,CAAC;AACvD,OAAO,EAAC,mBAAmB,EAAC,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAC,YAAY,EAAE,wBAAwB,EAAE,GAAG,EAAU,MAAM,qBAAqB,CAAC;AACzF,OAAO,EAAC,uBAAuB,EAAC,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAC,UAAU,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"body-validation.request-interceptor.js","sourceRoot":"","sources":["../../../../src/interceptors/body-validation.request-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAC,SAAS,EAAC,MAAM,8BAA8B,CAAC;AACvD,OAAO,EAAC,mBAAmB,EAAC,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAC,YAAY,EAAE,wBAAwB,EAAE,GAAG,EAAU,MAAM,qBAAqB,CAAC;AACzF,OAAO,EAAC,uBAAuB,EAAC,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAC,UAAU,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAC,6BAA6B,EAAC,MAAM,yCAAyC,CAAC;AAEtF;;;;GAIG;AAII,IAAM,gCAAgC,GAAtC,MAAM,gCAAgC;IAEzC;;;;;;OAMG;IACH,YAA4D,UAA+B,EAC9D,SAAoB;QADW,eAAU,GAAV,UAAU,CAAqB;QAC9D,cAAS,GAAT,SAAS,CAAW;IACjD,CAAC;IAED;;;;;OAKG;IACG,gBAAgB,CAAC,OAAgB,EAAE,UAA4B;;YACjE,MAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,6BAA6B,CAAC,CAAC;YAE9E,IAAG,aAAa,KAAK,SAAS,IAAI,aAAa,CAAC,SAAS,KAAK,SAAS,EAAE;gBACrE,OAAO,OAAO,CAAC;aAClB;YAED,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,+BAA+B,EAAE;gBACnD,OAAO;gBACP,UAAU;gBACV,YAAY,EAAE,UAAU,CAAC,KAAK,CAAC,OAAO;aACzC,EAAE,uBAAuB,CAAC,CAAA;YAE3B,6DAA6D;YAC7D,MAAM,UAAU,GAAG,eAAe,CAAC,aAAa,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;YAE1E,sEAAsE;YACtE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YAEzD,IAAG,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE;gBACnB,OAAO,OAAO,CAAC;aAClB;YAED,6EAA6E;YAC7E,MAAM,IAAI,mBAAmB,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC;QAC9D,CAAC;KAAA;CACJ,CAAA;AA7CY,gCAAgC;IAH5C,YAAY,CAAC,uBAAuB,CAAC;IACrC,GAAG,CAAC,wBAAwB,CAAC,kBAAkB,CAAC;IAChD,UAAU,EAAE;IAUI,WAAA,MAAM,CAAC,qBAAqB,CAAC,CAAA;6CACF,SAAS;GAVxC,gCAAgC,CA6C5C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pristine-ts/validation",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.278",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/lib/esm/validation.module.js",
|
|
6
6
|
"main": "dist/lib/cjs/validation.module.js",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"test:cov": "jest --coverage"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@pristine-ts/class-validator": "^1.0.
|
|
16
|
-
"@pristine-ts/common": "^0.0.
|
|
17
|
-
"@pristine-ts/core": "^0.0.
|
|
18
|
-
"@pristine-ts/networking": "^0.0.
|
|
15
|
+
"@pristine-ts/class-validator": "^1.0.22",
|
|
16
|
+
"@pristine-ts/common": "^0.0.278",
|
|
17
|
+
"@pristine-ts/core": "^0.0.278",
|
|
18
|
+
"@pristine-ts/networking": "^0.0.278",
|
|
19
19
|
"class-transformer": "^0.5.1"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"src/*.{js,ts}"
|
|
61
61
|
]
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "919af75933ff21c315d4e2d0aa6f5a2be98b6752"
|
|
64
64
|
}
|