@tsed/platform-params 8.11.0 → 8.11.2-rc.1
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/lib/esm/builder/PlatformParams.js +0 -1
- package/lib/esm/decorators/bodyParams.js +0 -1
- package/lib/esm/decorators/context.js +0 -1
- package/lib/esm/decorators/cookies.js +0 -1
- package/lib/esm/decorators/headerParams.js +0 -1
- package/lib/esm/decorators/locals.js +0 -1
- package/lib/esm/decorators/paramFn.js +0 -1
- package/lib/esm/decorators/pathParams.js +0 -1
- package/lib/esm/decorators/queryParams.js +0 -1
- package/lib/esm/decorators/session.js +0 -1
- package/lib/esm/decorators/useDeserialization.js +0 -1
- package/lib/esm/decorators/useParam.js +0 -1
- package/lib/esm/decorators/useParamExpression.js +0 -1
- package/lib/esm/decorators/useParamType.js +0 -1
- package/lib/esm/decorators/usePipe.js +0 -1
- package/lib/esm/decorators/useType.js +0 -1
- package/lib/esm/decorators/useValidation.js +0 -1
- package/lib/esm/domain/ParamMetadata.js +0 -1
- package/lib/esm/domain/ParamOptions.js +0 -1
- package/lib/esm/domain/ParamTypes.js +0 -1
- package/lib/esm/errors/ParamValidationError.js +0 -1
- package/lib/esm/errors/RequiredValidationError.js +0 -1
- package/lib/esm/errors/ValidationError.js +0 -1
- package/lib/esm/exports.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/pipes/DeserializerPipe.js +0 -1
- package/lib/esm/pipes/ParseExpressionPipe.js +0 -1
- package/lib/esm/pipes/ValidationPipe.js +0 -1
- package/lib/esm/utils/mapParamsOptions.js +0 -1
- package/package.json +12 -9
- package/lib/esm/builder/PlatformParams.js.map +0 -1
- package/lib/esm/decorators/bodyParams.js.map +0 -1
- package/lib/esm/decorators/context.js.map +0 -1
- package/lib/esm/decorators/cookies.js.map +0 -1
- package/lib/esm/decorators/headerParams.js.map +0 -1
- package/lib/esm/decorators/locals.js.map +0 -1
- package/lib/esm/decorators/paramFn.js.map +0 -1
- package/lib/esm/decorators/pathParams.js.map +0 -1
- package/lib/esm/decorators/queryParams.js.map +0 -1
- package/lib/esm/decorators/session.js.map +0 -1
- package/lib/esm/decorators/useDeserialization.js.map +0 -1
- package/lib/esm/decorators/useParam.js.map +0 -1
- package/lib/esm/decorators/useParamExpression.js.map +0 -1
- package/lib/esm/decorators/useParamType.js.map +0 -1
- package/lib/esm/decorators/usePipe.js.map +0 -1
- package/lib/esm/decorators/useType.js.map +0 -1
- package/lib/esm/decorators/useValidation.js.map +0 -1
- package/lib/esm/domain/ParamMetadata.js.map +0 -1
- package/lib/esm/domain/ParamOptions.js.map +0 -1
- package/lib/esm/domain/ParamTypes.js.map +0 -1
- package/lib/esm/errors/ParamValidationError.js.map +0 -1
- package/lib/esm/errors/RequiredValidationError.js.map +0 -1
- package/lib/esm/errors/ValidationError.js.map +0 -1
- package/lib/esm/exports.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/pipes/DeserializerPipe.js.map +0 -1
- package/lib/esm/pipes/ParseExpressionPipe.js.map +0 -1
- package/lib/esm/pipes/ValidationPipe.js.map +0 -1
- package/lib/esm/utils/mapParamsOptions.js.map +0 -1
- package/src/builder/PlatformParams.spec.ts +0 -431
- package/src/builder/PlatformParams.ts +0 -92
- package/src/decorators/__snapshots__/bodyParams.spec.ts.snap +0 -105
- package/src/decorators/bodyParams.spec.ts +0 -432
- package/src/decorators/bodyParams.ts +0 -93
- package/src/decorators/context.spec.ts +0 -16
- package/src/decorators/context.ts +0 -70
- package/src/decorators/cookies.spec.ts +0 -19
- package/src/decorators/cookies.ts +0 -102
- package/src/decorators/headerParams.spec.ts +0 -16
- package/src/decorators/headerParams.ts +0 -49
- package/src/decorators/locals.spec.ts +0 -16
- package/src/decorators/locals.ts +0 -57
- package/src/decorators/paramFn.ts +0 -14
- package/src/decorators/pathParams.spec.ts +0 -58
- package/src/decorators/pathParams.ts +0 -97
- package/src/decorators/queryParams.spec.ts +0 -58
- package/src/decorators/queryParams.ts +0 -102
- package/src/decorators/session.spec.ts +0 -47
- package/src/decorators/session.ts +0 -51
- package/src/decorators/useDeserialization.ts +0 -14
- package/src/decorators/useParam.spec.ts +0 -82
- package/src/decorators/useParam.ts +0 -38
- package/src/decorators/useParamExpression.ts +0 -16
- package/src/decorators/useParamType.ts +0 -36
- package/src/decorators/usePipe.ts +0 -21
- package/src/decorators/useType.ts +0 -35
- package/src/decorators/useValidation.ts +0 -18
- package/src/domain/ParamMetadata.ts +0 -1
- package/src/domain/ParamOptions.ts +0 -12
- package/src/domain/ParamTypes.ts +0 -37
- package/src/errors/ParamValidationError.spec.ts +0 -152
- package/src/errors/ParamValidationError.ts +0 -38
- package/src/errors/RequiredValidationError.spec.ts +0 -49
- package/src/errors/RequiredValidationError.ts +0 -30
- package/src/errors/ValidationError.spec.ts +0 -18
- package/src/errors/ValidationError.ts +0 -11
- package/src/exports.ts +0 -1
- package/src/index.ts +0 -31
- package/src/pipes/DeserializerPipe.spec.ts +0 -39
- package/src/pipes/DeserializerPipe.ts +0 -15
- package/src/pipes/ParseExpressionPipe.spec.ts +0 -242
- package/src/pipes/ParseExpressionPipe.ts +0 -33
- package/src/pipes/ValidationPipe.spec.ts +0 -645
- package/src/pipes/ValidationPipe.ts +0 -112
- package/src/utils/mapParamsOptions.spec.ts +0 -28
- package/src/utils/mapParamsOptions.ts +0 -34
- package/vitest.config.mts +0 -21
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import {constant, injectable, injectMany} from "@tsed/di";
|
|
2
|
-
import {deserialize} from "@tsed/json-mapper";
|
|
3
|
-
import {getJsonSchema, JsonParameterStore, PipeMethods} from "@tsed/schema";
|
|
4
|
-
|
|
5
|
-
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
6
|
-
import {RequiredValidationError} from "../errors/RequiredValidationError.js";
|
|
7
|
-
|
|
8
|
-
function cast(value: any, metadata: JsonParameterStore) {
|
|
9
|
-
try {
|
|
10
|
-
return deserialize(value, {
|
|
11
|
-
type: metadata.type
|
|
12
|
-
});
|
|
13
|
-
} catch (er) {
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface ValidatorServiceMethods {
|
|
19
|
-
readonly name: string;
|
|
20
|
-
|
|
21
|
-
validate(value: any, options: any): Promise<any>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class ValidationPipe implements PipeMethods {
|
|
25
|
-
private validators: Map<string, ValidatorServiceMethods> = new Map();
|
|
26
|
-
|
|
27
|
-
constructor() {
|
|
28
|
-
const validators = injectMany<ValidatorServiceMethods>("validator:service");
|
|
29
|
-
const defaultValidator = constant("validators.default");
|
|
30
|
-
|
|
31
|
-
validators.length && this.validators.set("default", validators[0]);
|
|
32
|
-
|
|
33
|
-
validators.map((service) => {
|
|
34
|
-
this.validators.set(service.name, service);
|
|
35
|
-
|
|
36
|
-
if (service.name === defaultValidator) {
|
|
37
|
-
this.validators.set("default", service);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
coerceTypes(value: any, metadata: JsonParameterStore) {
|
|
43
|
-
if (value === undefined) {
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (value === "null") {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (metadata.isArray) {
|
|
52
|
-
return [].concat(value);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (metadata.isPrimitive) {
|
|
56
|
-
return cast(value, metadata);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
skip(value: any, metadata: JsonParameterStore) {
|
|
63
|
-
return metadata.paramType === ParamTypes.PATH && !metadata.isPrimitive;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
transform(value: any, metadata: JsonParameterStore): Promise<any> {
|
|
67
|
-
if (!this.validators.size) {
|
|
68
|
-
this.checkIsRequired(value, metadata);
|
|
69
|
-
return value;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// FIXME See if it's necessary ?
|
|
73
|
-
if (this.skip(value, metadata)) {
|
|
74
|
-
return value;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
value = this.coerceTypes(value, metadata);
|
|
78
|
-
|
|
79
|
-
this.checkIsRequired(value, metadata);
|
|
80
|
-
|
|
81
|
-
if (value === undefined) {
|
|
82
|
-
return value;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const schema = getJsonSchema(metadata, {
|
|
86
|
-
customKeys: true
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// TODO retrieve the right validator from metadata
|
|
90
|
-
const validator = this.validators.get("default");
|
|
91
|
-
|
|
92
|
-
if (validator) {
|
|
93
|
-
return validator.validate(value, {
|
|
94
|
-
schema,
|
|
95
|
-
type: metadata.isClass ? metadata.type : undefined,
|
|
96
|
-
collectionType: metadata.collectionType
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return value;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
protected checkIsRequired(value: any, metadata: JsonParameterStore) {
|
|
104
|
-
if (metadata.isRequired(value)) {
|
|
105
|
-
throw RequiredValidationError.from(metadata);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
injectable(ValidationPipe).type("validator");
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {mapParamsOptions} from "./mapParamsOptions.js";
|
|
2
|
-
|
|
3
|
-
class Test {}
|
|
4
|
-
|
|
5
|
-
describe("mapParamsOptions", () => {
|
|
6
|
-
it("should return default params when params is empty", () => {
|
|
7
|
-
expect(mapParamsOptions([])).toEqual({useType: undefined, expression: undefined});
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("should return default params when params have an expression", () => {
|
|
11
|
-
expect(mapParamsOptions(["expression"])).toEqual({expression: "expression"});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("should return default params when params have an expression and usetype", () => {
|
|
15
|
-
expect(mapParamsOptions(["expression", Test])).toEqual({expression: "expression", useType: Test});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("should return default params when params have an useType", () => {
|
|
19
|
-
expect(mapParamsOptions([Test])).toEqual({useType: Test});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should return default params when params have an object", () => {
|
|
23
|
-
expect(mapParamsOptions([{expression: "expression", useType: Test}])).toEqual({
|
|
24
|
-
expression: "expression",
|
|
25
|
-
useType: Test
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import {isObject} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {ParamOptions} from "../domain/ParamOptions.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @ignore
|
|
7
|
-
*/
|
|
8
|
-
export function mapParamsOptions(args: any[]): Partial<ParamOptions<any>> {
|
|
9
|
-
if (args.length === 1) {
|
|
10
|
-
if (typeof args[0] === "string") {
|
|
11
|
-
return {
|
|
12
|
-
expression: args[0]
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!isObject(args[0])) {
|
|
17
|
-
return {
|
|
18
|
-
useType: args[0]
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const opts = args[0] as ParamOptions;
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
...opts,
|
|
26
|
-
useMapper: opts.useMapper
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
expression: args[0],
|
|
32
|
-
useType: args[1]
|
|
33
|
-
};
|
|
34
|
-
}
|
package/vitest.config.mts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import {presets} from "@tsed/vitest/presets";
|
|
3
|
-
import {defineConfig} from "vitest/config";
|
|
4
|
-
|
|
5
|
-
export default defineConfig(
|
|
6
|
-
{
|
|
7
|
-
...presets,
|
|
8
|
-
test: {
|
|
9
|
-
...presets.test,
|
|
10
|
-
coverage: {
|
|
11
|
-
...presets.test.coverage,
|
|
12
|
-
thresholds: {
|
|
13
|
-
statements: 0,
|
|
14
|
-
branches: 0,
|
|
15
|
-
functions: 0,
|
|
16
|
-
lines: 0
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
);
|