@celljs/web 4.0.0 → 4.2.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 +39 -1
- package/cell.yml +0 -3
- package/esm/browser/module.d.ts +1 -1
- package/esm/common/module.d.ts +1 -1
- package/esm/common/pipe/index.d.ts +1 -1
- package/esm/common/pipe/index.js +1 -1
- package/esm/common/pipe/index.js.map +1 -1
- package/esm/common/pipe/type-conversion-pipe.d.ts +12 -0
- package/esm/common/pipe/type-conversion-pipe.js +43 -0
- package/esm/common/pipe/type-conversion-pipe.js.map +1 -0
- package/esm/common/pipe/validation-errors.d.ts +11 -2
- package/esm/common/pipe/validation-errors.js +2 -0
- package/esm/common/pipe/validation-errors.js.map +1 -1
- package/esm/node/body/body-middleware.d.ts +5 -4
- package/esm/node/body/body-middleware.js +14 -15
- package/esm/node/body/body-middleware.js.map +1 -1
- package/esm/node/body/body-parser.d.ts +32 -0
- package/esm/node/body/body-parser.js +205 -0
- package/esm/node/body/body-parser.js.map +1 -0
- package/esm/node/body/body-protocol.d.ts +15 -0
- package/esm/node/body/form-util.d.ts +15 -0
- package/esm/node/body/form-util.js +120 -0
- package/esm/node/body/form-util.js.map +1 -0
- package/esm/node/body/index.d.ts +2 -0
- package/esm/node/body/index.js +2 -0
- package/esm/node/body/index.js.map +1 -1
- package/esm/node/context.js +1 -1
- package/esm/node/context.js.map +1 -1
- package/esm/node/cookies/cookies-factory.js +2 -6
- package/esm/node/cookies/cookies-factory.js.map +1 -1
- package/esm/node/cookies/cookies-protocol.d.ts +25 -2
- package/esm/node/cookies/cookies.d.ts +17 -0
- package/esm/node/cookies/cookies.js +137 -0
- package/esm/node/cookies/cookies.js.map +1 -0
- package/esm/node/cookies/index.d.ts +1 -0
- package/esm/node/cookies/index.js +1 -0
- package/esm/node/cookies/index.js.map +1 -1
- package/esm/node/cors/cors-middleware.d.ts +11 -3
- package/esm/node/cors/cors-middleware.js +108 -14
- package/esm/node/cors/cors-middleware.js.map +1 -1
- package/esm/node/cors/cors-protocol.d.ts +23 -0
- package/esm/node/error/error-handler-provider.js +3 -7
- package/esm/node/error/error-handler-provider.js.map +1 -1
- package/esm/node/health/health-middleware.js +1 -1
- package/esm/node/health/health-middleware.js.map +1 -1
- package/esm/node/http/http-middleware.d.ts +1 -0
- package/esm/node/http/http-middleware.js +10 -5
- package/esm/node/http/http-middleware.js.map +1 -1
- package/esm/node/http/http-protocol.d.ts +29 -5
- package/esm/node/http/http-protocol.js.map +1 -1
- package/esm/node/http/http-request.d.ts +41 -0
- package/esm/node/http/http-request.js +106 -0
- package/esm/node/http/http-request.js.map +1 -0
- package/esm/node/http/index.d.ts +1 -0
- package/esm/node/http/index.js +1 -0
- package/esm/node/http/index.js.map +1 -1
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/index.js.map +1 -1
- package/esm/node/matcher/index.d.ts +1 -0
- package/esm/node/matcher/index.js +1 -0
- package/esm/node/matcher/index.js.map +1 -1
- package/esm/node/matcher/path-pattern.d.ts +23 -0
- package/esm/node/matcher/path-pattern.js +115 -0
- package/esm/node/matcher/path-pattern.js.map +1 -0
- package/esm/node/matcher/request-matcher.d.ts +2 -2
- package/esm/node/matcher/request-matcher.js +8 -9
- package/esm/node/matcher/request-matcher.js.map +1 -1
- package/esm/node/module.d.ts +1 -1
- package/esm/node/server/http-server.d.ts +13 -0
- package/esm/node/server/http-server.js +32 -0
- package/esm/node/server/http-server.js.map +1 -0
- package/esm/node/server/index.d.ts +1 -0
- package/esm/node/server/index.js +2 -0
- package/esm/node/server/index.js.map +1 -0
- package/esm/node/session/session-manager.js +1 -2
- package/esm/node/session/session-manager.js.map +1 -1
- package/esm/node/session/session-store.js +5 -5
- package/esm/node/session/session-store.js.map +1 -1
- package/esm/node/session/session.d.ts +3 -2
- package/esm/node/session/session.js +4 -10
- package/esm/node/session/session.js.map +1 -1
- package/lib/browser/module.d.ts +1 -1
- package/lib/common/module.d.ts +1 -1
- package/lib/common/pipe/index.d.ts +1 -1
- package/lib/common/pipe/index.d.ts.map +1 -1
- package/lib/common/pipe/index.js +1 -1
- package/lib/common/pipe/index.js.map +1 -1
- package/lib/common/pipe/type-conversion-pipe.d.ts +13 -0
- package/lib/common/pipe/type-conversion-pipe.d.ts.map +1 -0
- package/lib/common/pipe/type-conversion-pipe.js +46 -0
- package/lib/common/pipe/type-conversion-pipe.js.map +1 -0
- package/lib/common/pipe/type-conversion-pipe.spec.d.ts +2 -0
- package/lib/common/pipe/type-conversion-pipe.spec.d.ts.map +1 -0
- package/lib/common/pipe/type-conversion-pipe.spec.js +36 -0
- package/lib/common/pipe/type-conversion-pipe.spec.js.map +1 -0
- package/lib/common/pipe/validation-errors.d.ts +11 -2
- package/lib/common/pipe/validation-errors.d.ts.map +1 -1
- package/lib/common/pipe/validation-errors.js +2 -0
- package/lib/common/pipe/validation-errors.js.map +1 -1
- package/lib/node/body/body-middleware.d.ts +5 -4
- package/lib/node/body/body-middleware.d.ts.map +1 -1
- package/lib/node/body/body-middleware.js +12 -12
- package/lib/node/body/body-middleware.js.map +1 -1
- package/lib/node/body/body-middleware.spec.d.ts +2 -0
- package/lib/node/body/body-middleware.spec.d.ts.map +1 -0
- package/lib/node/body/body-middleware.spec.js +85 -0
- package/lib/node/body/body-middleware.spec.js.map +1 -0
- package/lib/node/body/body-parser.d.ts +33 -0
- package/lib/node/body/body-parser.d.ts.map +1 -0
- package/lib/node/body/body-parser.js +210 -0
- package/lib/node/body/body-parser.js.map +1 -0
- package/lib/node/body/body-protocol.d.ts +15 -0
- package/lib/node/body/body-protocol.d.ts.map +1 -1
- package/lib/node/body/form-util.d.ts +16 -0
- package/lib/node/body/form-util.d.ts.map +1 -0
- package/lib/node/body/form-util.js +123 -0
- package/lib/node/body/form-util.js.map +1 -0
- package/lib/node/body/form-util.spec.d.ts +2 -0
- package/lib/node/body/form-util.spec.d.ts.map +1 -0
- package/lib/node/body/form-util.spec.js +44 -0
- package/lib/node/body/form-util.spec.js.map +1 -0
- package/lib/node/body/index.d.ts +2 -0
- package/lib/node/body/index.d.ts.map +1 -1
- package/lib/node/body/index.js +2 -0
- package/lib/node/body/index.js.map +1 -1
- package/lib/node/context.js +1 -1
- package/lib/node/context.js.map +1 -1
- package/lib/node/cookies/cookies-factory.d.ts.map +1 -1
- package/lib/node/cookies/cookies-factory.js +2 -9
- package/lib/node/cookies/cookies-factory.js.map +1 -1
- package/lib/node/cookies/cookies-protocol.d.ts +25 -2
- package/lib/node/cookies/cookies-protocol.d.ts.map +1 -1
- package/lib/node/cookies/cookies.d.ts +18 -0
- package/lib/node/cookies/cookies.d.ts.map +1 -0
- package/lib/node/cookies/cookies.js +141 -0
- package/lib/node/cookies/cookies.js.map +1 -0
- package/lib/node/cookies/cookies.spec.d.ts +2 -0
- package/lib/node/cookies/cookies.spec.d.ts.map +1 -0
- package/lib/node/cookies/cookies.spec.js +73 -0
- package/lib/node/cookies/cookies.spec.js.map +1 -0
- package/lib/node/cookies/index.d.ts +1 -0
- package/lib/node/cookies/index.d.ts.map +1 -1
- package/lib/node/cookies/index.js +1 -0
- package/lib/node/cookies/index.js.map +1 -1
- package/lib/node/cors/cors-middleware.d.ts +11 -3
- package/lib/node/cors/cors-middleware.d.ts.map +1 -1
- package/lib/node/cors/cors-middleware.js +106 -15
- package/lib/node/cors/cors-middleware.js.map +1 -1
- package/lib/node/cors/cors-middleware.spec.d.ts +2 -0
- package/lib/node/cors/cors-middleware.spec.d.ts.map +1 -0
- package/lib/node/cors/cors-middleware.spec.js +56 -0
- package/lib/node/cors/cors-middleware.spec.js.map +1 -0
- package/lib/node/cors/cors-protocol.d.ts +23 -0
- package/lib/node/cors/cors-protocol.d.ts.map +1 -1
- package/lib/node/error/error-handler-provider.d.ts.map +1 -1
- package/lib/node/error/error-handler-provider.js +2 -5
- package/lib/node/error/error-handler-provider.js.map +1 -1
- package/lib/node/health/health-middleware.js +1 -1
- package/lib/node/health/health-middleware.js.map +1 -1
- package/lib/node/http/http-middleware.d.ts +1 -0
- package/lib/node/http/http-middleware.d.ts.map +1 -1
- package/lib/node/http/http-middleware.js +10 -5
- package/lib/node/http/http-middleware.js.map +1 -1
- package/lib/node/http/http-protocol.d.ts +29 -5
- package/lib/node/http/http-protocol.d.ts.map +1 -1
- package/lib/node/http/http-protocol.js.map +1 -1
- package/lib/node/http/http-request.d.ts +42 -0
- package/lib/node/http/http-request.d.ts.map +1 -0
- package/lib/node/http/http-request.js +110 -0
- package/lib/node/http/http-request.js.map +1 -0
- package/lib/node/http/http-request.spec.d.ts +2 -0
- package/lib/node/http/http-request.spec.d.ts.map +1 -0
- package/lib/node/http/http-request.spec.js +65 -0
- package/lib/node/http/http-request.spec.js.map +1 -0
- package/lib/node/http/index.d.ts +1 -0
- package/lib/node/http/index.d.ts.map +1 -1
- package/lib/node/http/index.js +1 -0
- package/lib/node/http/index.js.map +1 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/matcher/index.d.ts +1 -0
- package/lib/node/matcher/index.d.ts.map +1 -1
- package/lib/node/matcher/index.js +1 -0
- package/lib/node/matcher/index.js.map +1 -1
- package/lib/node/matcher/path-pattern.d.ts +24 -0
- package/lib/node/matcher/path-pattern.d.ts.map +1 -0
- package/lib/node/matcher/path-pattern.js +119 -0
- package/lib/node/matcher/path-pattern.js.map +1 -0
- package/lib/node/matcher/path-pattern.spec.d.ts +2 -0
- package/lib/node/matcher/path-pattern.spec.d.ts.map +1 -0
- package/lib/node/matcher/path-pattern.spec.js +43 -0
- package/lib/node/matcher/path-pattern.spec.js.map +1 -0
- package/lib/node/matcher/request-matcher.d.ts +2 -2
- package/lib/node/matcher/request-matcher.d.ts.map +1 -1
- package/lib/node/matcher/request-matcher.js +8 -12
- package/lib/node/matcher/request-matcher.js.map +1 -1
- package/lib/node/module.d.ts +1 -1
- package/lib/node/server/http-server.d.ts +14 -0
- package/lib/node/server/http-server.d.ts.map +1 -0
- package/lib/node/server/http-server.js +35 -0
- package/lib/node/server/http-server.js.map +1 -0
- package/lib/node/server/http-server.spec.d.ts +2 -0
- package/lib/node/server/http-server.spec.d.ts.map +1 -0
- package/lib/node/server/http-server.spec.js +41 -0
- package/lib/node/server/http-server.spec.js.map +1 -0
- package/lib/node/server/index.d.ts +2 -0
- package/lib/node/server/index.d.ts.map +1 -0
- package/lib/node/server/index.js +18 -0
- package/lib/node/server/index.js.map +1 -0
- package/lib/node/session/session-manager.d.ts.map +1 -1
- package/lib/node/session/session-manager.js +1 -2
- package/lib/node/session/session-manager.js.map +1 -1
- package/lib/node/session/session-store.d.ts.map +1 -1
- package/lib/node/session/session-store.js +5 -3
- package/lib/node/session/session-store.js.map +1 -1
- package/lib/node/session/session.d.ts +3 -2
- package/lib/node/session/session.d.ts.map +1 -1
- package/lib/node/session/session.js +4 -8
- package/lib/node/session/session.js.map +1 -1
- package/lib/node/session/session.spec.d.ts +2 -0
- package/lib/node/session/session.spec.d.ts.map +1 -0
- package/lib/node/session/session.spec.js +84 -0
- package/lib/node/session/session.spec.js.map +1 -0
- package/package.json +10 -18
- package/src/common/pipe/index.ts +1 -1
- package/src/common/pipe/type-conversion-pipe.spec.ts +38 -0
- package/src/common/pipe/type-conversion-pipe.ts +36 -0
- package/src/common/pipe/validation-errors.ts +11 -2
- package/src/node/body/body-middleware.spec.ts +96 -0
- package/src/node/body/body-middleware.ts +14 -15
- package/src/node/body/body-parser.ts +216 -0
- package/src/node/body/body-protocol.ts +15 -0
- package/src/node/body/form-util.spec.ts +45 -0
- package/src/node/body/form-util.ts +132 -0
- package/src/node/body/index.ts +2 -0
- package/src/node/context.ts +1 -1
- package/src/node/cookies/cookies-factory.ts +2 -6
- package/src/node/cookies/cookies-protocol.ts +28 -3
- package/src/node/cookies/cookies.spec.ts +80 -0
- package/src/node/cookies/cookies.ts +138 -0
- package/src/node/cookies/index.ts +1 -0
- package/src/node/cors/cors-middleware.spec.ts +59 -0
- package/src/node/cors/cors-middleware.ts +112 -15
- package/src/node/cors/cors-protocol.ts +25 -0
- package/src/node/error/error-handler-provider.ts +1 -2
- package/src/node/health/health-middleware.ts +1 -1
- package/src/node/http/http-middleware.ts +10 -5
- package/src/node/http/http-protocol.ts +29 -5
- package/src/node/http/http-request.spec.ts +68 -0
- package/src/node/http/http-request.ts +132 -0
- package/src/node/http/index.ts +1 -0
- package/src/node/index.ts +1 -0
- package/src/node/matcher/index.ts +1 -0
- package/src/node/matcher/path-pattern.spec.ts +43 -0
- package/src/node/matcher/path-pattern.ts +113 -0
- package/src/node/matcher/request-matcher.ts +9 -10
- package/src/node/server/http-server.spec.ts +45 -0
- package/src/node/server/http-server.ts +35 -0
- package/src/node/server/index.ts +1 -0
- package/src/node/session/session-manager.ts +1 -2
- package/src/node/session/session-store.ts +6 -3
- package/src/node/session/session.spec.ts +88 -0
- package/src/node/session/session.ts +6 -9
- package/esm/common/pipe/pipe-transtorm.d.ts +0 -17
- package/esm/common/pipe/pipe-transtorm.js +0 -110
- package/esm/common/pipe/pipe-transtorm.js.map +0 -1
- package/lib/common/pipe/pipe-transtorm.d.ts +0 -18
- package/lib/common/pipe/pipe-transtorm.d.ts.map +0 -1
- package/lib/common/pipe/pipe-transtorm.js +0 -113
- package/lib/common/pipe/pipe-transtorm.js.map +0 -1
- package/lib/common/pipe/pipe-transtorm.spec.d.ts +0 -2
- package/lib/common/pipe/pipe-transtorm.spec.d.ts.map +0 -1
- package/lib/common/pipe/pipe-transtorm.spec.js +0 -69
- package/lib/common/pipe/pipe-transtorm.spec.js.map +0 -1
- package/src/common/pipe/pipe-transtorm.spec.ts +0 -62
- package/src/common/pipe/pipe-transtorm.ts +0 -113
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { PipeTransform, ArgumentMetadata } from './pipe-protocol.js';
|
|
2
|
-
export declare class ValidationPipe implements PipeTransform<any> {
|
|
3
|
-
protected options: any;
|
|
4
|
-
readonly priority = 1000;
|
|
5
|
-
transform(value: any, metadata: ArgumentMetadata): Promise<any>;
|
|
6
|
-
private toValidate;
|
|
7
|
-
private toEmptyIfNil;
|
|
8
|
-
private stripProtoKeys;
|
|
9
|
-
/**
|
|
10
|
-
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
11
|
-
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
12
|
-
* class instances and pass through: converting them would copy them property by property, which
|
|
13
|
-
* never ends for the circular request and response.
|
|
14
|
-
*/
|
|
15
|
-
private isData;
|
|
16
|
-
private isPrimitive;
|
|
17
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { Component, Value } from '@celljs/core';
|
|
11
|
-
import { PipeTransform } from './pipe-protocol.js';
|
|
12
|
-
import { ValidationErrors } from './validation-errors.js';
|
|
13
|
-
import { validate } from 'class-validator';
|
|
14
|
-
import { instanceToPlain, plainToInstance } from 'class-transformer';
|
|
15
|
-
let ValidationPipe = class ValidationPipe {
|
|
16
|
-
constructor() {
|
|
17
|
-
this.priority = 1000;
|
|
18
|
-
}
|
|
19
|
-
async transform(value, metadata) {
|
|
20
|
-
const opts = this.options || {};
|
|
21
|
-
const { argType } = metadata;
|
|
22
|
-
if (!argType || !this.isData(value)) {
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
if (!this.toValidate(metadata)) {
|
|
26
|
-
return plainToInstance(argType, value, opts.transformOptions);
|
|
27
|
-
}
|
|
28
|
-
const originalValue = value;
|
|
29
|
-
value = this.toEmptyIfNil(value);
|
|
30
|
-
const isNil = value !== originalValue;
|
|
31
|
-
const isPrimitive = this.isPrimitive(value);
|
|
32
|
-
this.stripProtoKeys(value);
|
|
33
|
-
let entity = plainToInstance(argType, value, opts.transformOptions);
|
|
34
|
-
const originalEntity = entity;
|
|
35
|
-
const isCtorNotEqual = entity.constructor !== argType;
|
|
36
|
-
if (isCtorNotEqual && !isPrimitive) {
|
|
37
|
-
entity.constructor = argType;
|
|
38
|
-
}
|
|
39
|
-
else if (isCtorNotEqual) {
|
|
40
|
-
// when "entity" is a primitive value, we have to temporarily
|
|
41
|
-
// replace the entity to perform the validation against the original
|
|
42
|
-
// metatype defined inside the handler
|
|
43
|
-
entity = { constructor: argType };
|
|
44
|
-
}
|
|
45
|
-
const errors = await validate(entity, opts.validatorOptions);
|
|
46
|
-
if (errors.length > 0) {
|
|
47
|
-
throw new ValidationErrors(opts.detailedOutputDisabled ? undefined : errors);
|
|
48
|
-
}
|
|
49
|
-
if (isPrimitive) {
|
|
50
|
-
// if the value is a primitive value and the validation process has been successfully completed
|
|
51
|
-
// we have to revert the original value passed through the pipe
|
|
52
|
-
entity = originalEntity;
|
|
53
|
-
}
|
|
54
|
-
if (opts.transformEnabled) {
|
|
55
|
-
return entity;
|
|
56
|
-
}
|
|
57
|
-
if (isNil) {
|
|
58
|
-
// if the value was originally undefined or null, revert it back
|
|
59
|
-
return originalValue;
|
|
60
|
-
}
|
|
61
|
-
return Object.keys(opts.validatorOptions).length > 0
|
|
62
|
-
? instanceToPlain(entity, opts.transformOptions)
|
|
63
|
-
: value;
|
|
64
|
-
}
|
|
65
|
-
toValidate(metadata) {
|
|
66
|
-
const { argType } = metadata;
|
|
67
|
-
const types = [String, Boolean, Number, Array, Object];
|
|
68
|
-
// eslint-disable-next-line no-null/no-null
|
|
69
|
-
return argType !== null && !types.some(t => argType === t);
|
|
70
|
-
}
|
|
71
|
-
toEmptyIfNil(value) {
|
|
72
|
-
// eslint-disable-next-line no-null/no-null
|
|
73
|
-
return value === null ? {} : value;
|
|
74
|
-
}
|
|
75
|
-
stripProtoKeys(value) {
|
|
76
|
-
delete value.__proto__;
|
|
77
|
-
const keys = Object.keys(value);
|
|
78
|
-
keys
|
|
79
|
-
.filter(key => typeof value[key] === 'object' && value[key])
|
|
80
|
-
.forEach(key => this.stripProtoKeys(value[key]));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
84
|
-
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
85
|
-
* class instances and pass through: converting them would copy them property by property, which
|
|
86
|
-
* never ends for the circular request and response.
|
|
87
|
-
*/
|
|
88
|
-
isData(value) {
|
|
89
|
-
if (typeof value === 'function') {
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
const proto = Object.getPrototypeOf(value);
|
|
96
|
-
return proto === Object.prototype || proto === null;
|
|
97
|
-
}
|
|
98
|
-
isPrimitive(value) {
|
|
99
|
-
return ['number', 'boolean', 'string'].indexOf(typeof value) !== -1;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
__decorate([
|
|
103
|
-
Value('cell.web.validationPipeOptions'),
|
|
104
|
-
__metadata("design:type", Object)
|
|
105
|
-
], ValidationPipe.prototype, "options", void 0);
|
|
106
|
-
ValidationPipe = __decorate([
|
|
107
|
-
Component(PipeTransform)
|
|
108
|
-
], ValidationPipe);
|
|
109
|
-
export { ValidationPipe };
|
|
110
|
-
//# sourceMappingURL=pipe-transtorm.js.map
|
|
@@ -1 +0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OAAO,CAAC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;IACxE,CAAC;CACJ,CAAA;AAtGa;IADT,KAAK,CAAC,gCAAgC,CAAC;;+CACjB;AAHd,cAAc;IAD1B,SAAS,CAAC,aAAa,CAAC;GACZ,cAAc,CAyG1B"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { PipeTransform, ArgumentMetadata } from './pipe-protocol';
|
|
2
|
-
export declare class ValidationPipe implements PipeTransform<any> {
|
|
3
|
-
protected options: any;
|
|
4
|
-
readonly priority = 1000;
|
|
5
|
-
transform(value: any, metadata: ArgumentMetadata): Promise<any>;
|
|
6
|
-
private toValidate;
|
|
7
|
-
private toEmptyIfNil;
|
|
8
|
-
private stripProtoKeys;
|
|
9
|
-
/**
|
|
10
|
-
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
11
|
-
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
12
|
-
* class instances and pass through: converting them would copy them property by property, which
|
|
13
|
-
* never ends for the circular request and response.
|
|
14
|
-
*/
|
|
15
|
-
private isData;
|
|
16
|
-
private isPrimitive;
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=pipe-transtorm.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pipe-transtorm.d.ts","sourceRoot":"","sources":["../../../src/common/pipe/pipe-transtorm.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAKlE,qBACa,cAAe,YAAW,aAAa,CAAC,GAAG,CAAC;IAGrD,SAAS,CAAC,OAAO,EAAE,GAAG,CAAC;IAEvB,QAAQ,CAAC,QAAQ,QAAQ;IAEZ,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,CAwD3E;IAED,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,cAAc;IAQtB;;;;;OAKG;IACH,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,WAAW;CAGtB"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ValidationPipe = void 0;
|
|
13
|
-
const core_1 = require("@celljs/core");
|
|
14
|
-
const pipe_protocol_1 = require("./pipe-protocol");
|
|
15
|
-
const validation_errors_1 = require("./validation-errors");
|
|
16
|
-
const class_validator_1 = require("class-validator");
|
|
17
|
-
const class_transformer_1 = require("class-transformer");
|
|
18
|
-
let ValidationPipe = class ValidationPipe {
|
|
19
|
-
constructor() {
|
|
20
|
-
this.priority = 1000;
|
|
21
|
-
}
|
|
22
|
-
async transform(value, metadata) {
|
|
23
|
-
const opts = this.options || {};
|
|
24
|
-
const { argType } = metadata;
|
|
25
|
-
if (!argType || !this.isData(value)) {
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
if (!this.toValidate(metadata)) {
|
|
29
|
-
return (0, class_transformer_1.plainToInstance)(argType, value, opts.transformOptions);
|
|
30
|
-
}
|
|
31
|
-
const originalValue = value;
|
|
32
|
-
value = this.toEmptyIfNil(value);
|
|
33
|
-
const isNil = value !== originalValue;
|
|
34
|
-
const isPrimitive = this.isPrimitive(value);
|
|
35
|
-
this.stripProtoKeys(value);
|
|
36
|
-
let entity = (0, class_transformer_1.plainToInstance)(argType, value, opts.transformOptions);
|
|
37
|
-
const originalEntity = entity;
|
|
38
|
-
const isCtorNotEqual = entity.constructor !== argType;
|
|
39
|
-
if (isCtorNotEqual && !isPrimitive) {
|
|
40
|
-
entity.constructor = argType;
|
|
41
|
-
}
|
|
42
|
-
else if (isCtorNotEqual) {
|
|
43
|
-
// when "entity" is a primitive value, we have to temporarily
|
|
44
|
-
// replace the entity to perform the validation against the original
|
|
45
|
-
// metatype defined inside the handler
|
|
46
|
-
entity = { constructor: argType };
|
|
47
|
-
}
|
|
48
|
-
const errors = await (0, class_validator_1.validate)(entity, opts.validatorOptions);
|
|
49
|
-
if (errors.length > 0) {
|
|
50
|
-
throw new validation_errors_1.ValidationErrors(opts.detailedOutputDisabled ? undefined : errors);
|
|
51
|
-
}
|
|
52
|
-
if (isPrimitive) {
|
|
53
|
-
// if the value is a primitive value and the validation process has been successfully completed
|
|
54
|
-
// we have to revert the original value passed through the pipe
|
|
55
|
-
entity = originalEntity;
|
|
56
|
-
}
|
|
57
|
-
if (opts.transformEnabled) {
|
|
58
|
-
return entity;
|
|
59
|
-
}
|
|
60
|
-
if (isNil) {
|
|
61
|
-
// if the value was originally undefined or null, revert it back
|
|
62
|
-
return originalValue;
|
|
63
|
-
}
|
|
64
|
-
return Object.keys(opts.validatorOptions).length > 0
|
|
65
|
-
? (0, class_transformer_1.instanceToPlain)(entity, opts.transformOptions)
|
|
66
|
-
: value;
|
|
67
|
-
}
|
|
68
|
-
toValidate(metadata) {
|
|
69
|
-
const { argType } = metadata;
|
|
70
|
-
const types = [String, Boolean, Number, Array, Object];
|
|
71
|
-
// eslint-disable-next-line no-null/no-null
|
|
72
|
-
return argType !== null && !types.some(t => argType === t);
|
|
73
|
-
}
|
|
74
|
-
toEmptyIfNil(value) {
|
|
75
|
-
// eslint-disable-next-line no-null/no-null
|
|
76
|
-
return value === null ? {} : value;
|
|
77
|
-
}
|
|
78
|
-
stripProtoKeys(value) {
|
|
79
|
-
delete value.__proto__;
|
|
80
|
-
const keys = Object.keys(value);
|
|
81
|
-
keys
|
|
82
|
-
.filter(key => typeof value[key] === 'object' && value[key])
|
|
83
|
-
.forEach(key => this.stripProtoKeys(value[key]));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
87
|
-
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
88
|
-
* class instances and pass through: converting them would copy them property by property, which
|
|
89
|
-
* never ends for the circular request and response.
|
|
90
|
-
*/
|
|
91
|
-
isData(value) {
|
|
92
|
-
if (typeof value === 'function') {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
const proto = Object.getPrototypeOf(value);
|
|
99
|
-
return proto === Object.prototype || proto === null;
|
|
100
|
-
}
|
|
101
|
-
isPrimitive(value) {
|
|
102
|
-
return ['number', 'boolean', 'string'].indexOf(typeof value) !== -1;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
exports.ValidationPipe = ValidationPipe;
|
|
106
|
-
__decorate([
|
|
107
|
-
(0, core_1.Value)('cell.web.validationPipeOptions'),
|
|
108
|
-
__metadata("design:type", Object)
|
|
109
|
-
], ValidationPipe.prototype, "options", void 0);
|
|
110
|
-
exports.ValidationPipe = ValidationPipe = __decorate([
|
|
111
|
-
(0, core_1.Component)(pipe_protocol_1.PipeTransform)
|
|
112
|
-
], ValidationPipe);
|
|
113
|
-
//# sourceMappingURL=pipe-transtorm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pipe-transtorm.js","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0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pipe-transtorm.spec.d.ts","sourceRoot":"","sources":["../../../src/common/pipe/pipe-transtorm.spec.ts"],"names":[],"mappings":""}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const bun_test_1 = require("bun:test");
|
|
13
|
-
const chai_1 = require("chai");
|
|
14
|
-
const class_validator_1 = require("class-validator");
|
|
15
|
-
const node_http_1 = require("node:http");
|
|
16
|
-
const node_net_1 = require("node:net");
|
|
17
|
-
const pipe_transtorm_1 = require("./pipe-transtorm");
|
|
18
|
-
const validation_errors_1 = require("./validation-errors");
|
|
19
|
-
class CreateUser {
|
|
20
|
-
}
|
|
21
|
-
__decorate([
|
|
22
|
-
(0, class_validator_1.IsString)(),
|
|
23
|
-
__metadata("design:type", String)
|
|
24
|
-
], CreateUser.prototype, "name", void 0);
|
|
25
|
-
class Cookies {
|
|
26
|
-
constructor(values) {
|
|
27
|
-
this.values = values;
|
|
28
|
-
}
|
|
29
|
-
get(name) {
|
|
30
|
-
return this.values[name];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function createPipe() {
|
|
34
|
-
const pipe = new pipe_transtorm_1.ValidationPipe();
|
|
35
|
-
pipe.options = { detailedOutputDisabled: false, transformEnabled: true };
|
|
36
|
-
return pipe;
|
|
37
|
-
}
|
|
38
|
-
(0, bun_test_1.describe)('ValidationPipe', () => {
|
|
39
|
-
(0, bun_test_1.it)('passes the request and response through, whatever the parameter type', async () => {
|
|
40
|
-
const pipe = createPipe();
|
|
41
|
-
const request = new node_http_1.IncomingMessage(new node_net_1.Socket());
|
|
42
|
-
const response = new node_http_1.ServerResponse(request);
|
|
43
|
-
(0, chai_1.expect)(await pipe.transform(response, { argType: Object })).to.equal(response);
|
|
44
|
-
(0, chai_1.expect)(await pipe.transform(response, { argType: node_http_1.ServerResponse })).to.equal(response);
|
|
45
|
-
(0, chai_1.expect)(await pipe.transform(request, { argType: Object })).to.equal(request);
|
|
46
|
-
});
|
|
47
|
-
(0, bun_test_1.it)('keeps other framework objects with their methods', async () => {
|
|
48
|
-
const cookies = new Cookies({ token: 'abc' });
|
|
49
|
-
const result = await createPipe().transform(cookies, { argType: Object });
|
|
50
|
-
(0, chai_1.expect)(result).to.equal(cookies);
|
|
51
|
-
(0, chai_1.expect)(result.get('token')).to.equal('abc');
|
|
52
|
-
});
|
|
53
|
-
(0, bun_test_1.it)('turns request data into the parameter class and validates it', async () => {
|
|
54
|
-
const pipe = createPipe();
|
|
55
|
-
const user = await pipe.transform({ name: 'Tom' }, { argType: CreateUser });
|
|
56
|
-
(0, chai_1.expect)(user).to.be.instanceOf(CreateUser);
|
|
57
|
-
(0, chai_1.expect)(user.name).to.equal('Tom');
|
|
58
|
-
let error;
|
|
59
|
-
await pipe.transform({ name: 1 }, { argType: CreateUser }).catch(e => error = e);
|
|
60
|
-
(0, chai_1.expect)(error).to.be.instanceOf(validation_errors_1.ValidationErrors);
|
|
61
|
-
});
|
|
62
|
-
(0, bun_test_1.it)('converts plain objects, arrays and primitives as before', async () => {
|
|
63
|
-
const pipe = createPipe();
|
|
64
|
-
(0, chai_1.expect)(await pipe.transform({ a: { b: 1 } }, { argType: Object })).to.deep.equal({ a: { b: 1 } });
|
|
65
|
-
(0, chai_1.expect)(await pipe.transform([1, 2], { argType: Array })).to.deep.equal([1, 2]);
|
|
66
|
-
(0, chai_1.expect)(await pipe.transform('5', { argType: String })).to.equal('5');
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
//# sourceMappingURL=pipe-transtorm.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pipe-transtorm.spec.js","sourceRoot":"","sources":["../../../src/common/pipe/pipe-transtorm.spec.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,uCAAwC;AACxC,+BAA8B;AAC9B,qDAA2C;AAC3C,yCAA4D;AAC5D,uCAAkC;AAClC,qDAAkD;AAClD,2DAAuD;AAEvD,MAAM,UAAU;CAGf;AADG;IADC,IAAA,0BAAQ,GAAE;;wCACE;AAGjB,MAAM,OAAO;IACT,YAA+B,MAA8B;sBAA9B,MAAM;IAA4B,CAAC;IAElE,GAAG,CAAC,IAAY;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;CACJ;AAED,SAAS,UAAU;IACf,MAAM,IAAI,GAAG,IAAI,+BAAc,EAAE,CAAC;IACjC,IAAY,CAAC,OAAO,GAAG,EAAE,sBAAsB,EAAE,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC;IAClF,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,IAAA,mBAAQ,EAAC,gBAAgB,EAAE,GAAG,EAAE;IAE5B,IAAA,aAAE,EAAC,sEAAsE,EAAE,KAAK,IAAI,EAAE;QAClF,MAAM,IAAI,GAAG,UAAU,EAAE,CAAC;QAC1B,MAAM,OAAO,GAAG,IAAI,2BAAe,CAAC,IAAI,iBAAM,EAAE,CAAC,CAAC;QAClD,MAAM,QAAQ,GAAG,IAAI,0BAAc,CAAC,OAAO,CAAC,CAAC;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CAAC,CAAC;IACzE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'bun:test';
|
|
2
|
-
import { expect } from 'chai';
|
|
3
|
-
import { IsString } from 'class-validator';
|
|
4
|
-
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
-
import { Socket } from 'node:net';
|
|
6
|
-
import { ValidationPipe } from './pipe-transtorm';
|
|
7
|
-
import { ValidationErrors } from './validation-errors';
|
|
8
|
-
|
|
9
|
-
class CreateUser {
|
|
10
|
-
@IsString()
|
|
11
|
-
name: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
class Cookies {
|
|
15
|
-
constructor(protected readonly values: Record<string, string>) { }
|
|
16
|
-
|
|
17
|
-
get(name: string): string | undefined {
|
|
18
|
-
return this.values[name];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function createPipe(): ValidationPipe {
|
|
23
|
-
const pipe = new ValidationPipe();
|
|
24
|
-
(pipe as any).options = { detailedOutputDisabled: false, transformEnabled: true };
|
|
25
|
-
return pipe;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
describe('ValidationPipe', () => {
|
|
29
|
-
|
|
30
|
-
it('passes the request and response through, whatever the parameter type', async () => {
|
|
31
|
-
const pipe = createPipe();
|
|
32
|
-
const request = new IncomingMessage(new Socket());
|
|
33
|
-
const response = new ServerResponse(request);
|
|
34
|
-
expect(await pipe.transform(response, { argType: Object })).to.equal(response);
|
|
35
|
-
expect(await pipe.transform(response, { argType: ServerResponse })).to.equal(response);
|
|
36
|
-
expect(await pipe.transform(request, { argType: Object })).to.equal(request);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('keeps other framework objects with their methods', async () => {
|
|
40
|
-
const cookies = new Cookies({ token: 'abc' });
|
|
41
|
-
const result = await createPipe().transform(cookies, { argType: Object });
|
|
42
|
-
expect(result).to.equal(cookies);
|
|
43
|
-
expect(result.get('token')).to.equal('abc');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('turns request data into the parameter class and validates it', async () => {
|
|
47
|
-
const pipe = createPipe();
|
|
48
|
-
const user = await pipe.transform({ name: 'Tom' }, { argType: CreateUser });
|
|
49
|
-
expect(user).to.be.instanceOf(CreateUser);
|
|
50
|
-
expect(user.name).to.equal('Tom');
|
|
51
|
-
let error: unknown;
|
|
52
|
-
await pipe.transform({ name: 1 }, { argType: CreateUser }).catch(e => error = e);
|
|
53
|
-
expect(error).to.be.instanceOf(ValidationErrors);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('converts plain objects, arrays and primitives as before', async () => {
|
|
57
|
-
const pipe = createPipe();
|
|
58
|
-
expect(await pipe.transform({ a: { b: 1 } }, { argType: Object })).to.deep.equal({ a: { b: 1 } });
|
|
59
|
-
expect(await pipe.transform([ 1, 2 ], { argType: Array })).to.deep.equal([ 1, 2 ]);
|
|
60
|
-
expect(await pipe.transform('5', { argType: String })).to.equal('5');
|
|
61
|
-
});
|
|
62
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Component, Value } from '@celljs/core';
|
|
2
|
-
import { PipeTransform, ArgumentMetadata } from './pipe-protocol';
|
|
3
|
-
import { ValidationErrors } from './validation-errors';
|
|
4
|
-
import { validate } from 'class-validator';
|
|
5
|
-
import { instanceToPlain, plainToInstance } from 'class-transformer';
|
|
6
|
-
|
|
7
|
-
@Component(PipeTransform)
|
|
8
|
-
export class ValidationPipe implements PipeTransform<any> {
|
|
9
|
-
|
|
10
|
-
@Value('cell.web.validationPipeOptions')
|
|
11
|
-
protected options: any;
|
|
12
|
-
|
|
13
|
-
readonly priority = 1000;
|
|
14
|
-
|
|
15
|
-
public async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
|
|
16
|
-
const opts = this.options || {};
|
|
17
|
-
const { argType } = metadata;
|
|
18
|
-
if (!argType || !this.isData(value)) {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
if (!this.toValidate(metadata)) {
|
|
22
|
-
return plainToInstance(
|
|
23
|
-
argType,
|
|
24
|
-
value,
|
|
25
|
-
opts.transformOptions,
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
const originalValue = value;
|
|
29
|
-
value = this.toEmptyIfNil(value);
|
|
30
|
-
|
|
31
|
-
const isNil = value !== originalValue;
|
|
32
|
-
const isPrimitive = this.isPrimitive(value);
|
|
33
|
-
this.stripProtoKeys(value);
|
|
34
|
-
let entity = plainToInstance(
|
|
35
|
-
argType,
|
|
36
|
-
value,
|
|
37
|
-
opts.transformOptions,
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const originalEntity = entity;
|
|
41
|
-
const isCtorNotEqual = entity.constructor !== argType;
|
|
42
|
-
|
|
43
|
-
if (isCtorNotEqual && !isPrimitive) {
|
|
44
|
-
entity.constructor = argType;
|
|
45
|
-
} else if (isCtorNotEqual) {
|
|
46
|
-
// when "entity" is a primitive value, we have to temporarily
|
|
47
|
-
// replace the entity to perform the validation against the original
|
|
48
|
-
// metatype defined inside the handler
|
|
49
|
-
entity = { constructor: argType } as any;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const errors = await validate(entity, opts.validatorOptions);
|
|
53
|
-
if (errors.length > 0) {
|
|
54
|
-
throw new ValidationErrors(opts.detailedOutputDisabled ? undefined : errors);
|
|
55
|
-
}
|
|
56
|
-
if (isPrimitive) {
|
|
57
|
-
// if the value is a primitive value and the validation process has been successfully completed
|
|
58
|
-
// we have to revert the original value passed through the pipe
|
|
59
|
-
entity = originalEntity;
|
|
60
|
-
}
|
|
61
|
-
if (opts.transformEnabled) {
|
|
62
|
-
return entity;
|
|
63
|
-
}
|
|
64
|
-
if (isNil) {
|
|
65
|
-
// if the value was originally undefined or null, revert it back
|
|
66
|
-
return originalValue;
|
|
67
|
-
}
|
|
68
|
-
return Object.keys(opts.validatorOptions).length > 0
|
|
69
|
-
? instanceToPlain(entity, opts.transformOptions)
|
|
70
|
-
: value;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private toValidate(metadata: ArgumentMetadata): boolean {
|
|
74
|
-
const { argType } = metadata;
|
|
75
|
-
const types = [String, Boolean, Number, Array, Object];
|
|
76
|
-
// eslint-disable-next-line no-null/no-null
|
|
77
|
-
return argType !== null && !types.some(t => argType === t);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private toEmptyIfNil<T = any, R = any>(value: T): R | {} | undefined {
|
|
81
|
-
// eslint-disable-next-line no-null/no-null
|
|
82
|
-
return value === null ? {} : value;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private stripProtoKeys(value: Record<string, any>): void {
|
|
86
|
-
delete value.__proto__;
|
|
87
|
-
const keys = Object.keys(value);
|
|
88
|
-
keys
|
|
89
|
-
.filter(key => typeof value[key] === 'object' && value[key])
|
|
90
|
-
.forEach(key => this.stripProtoKeys(value[key]));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
95
|
-
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
96
|
-
* class instances and pass through: converting them would copy them property by property, which
|
|
97
|
-
* never ends for the circular request and response.
|
|
98
|
-
*/
|
|
99
|
-
private isData(value: unknown): boolean {
|
|
100
|
-
if (typeof value === 'function') {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
const proto = Object.getPrototypeOf(value);
|
|
107
|
-
return proto === Object.prototype || proto === null;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private isPrimitive(value: unknown): boolean {
|
|
111
|
-
return ['number', 'boolean', 'string'].indexOf(typeof value) !== -1;
|
|
112
|
-
}
|
|
113
|
-
}
|