@tsed/platform-params 8.0.1 → 8.0.3
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/types/decorators/locals.d.ts +1 -1
- package/package.json +14 -13
- package/src/builder/PlatformParams.spec.ts +431 -0
- package/src/builder/PlatformParams.ts +86 -0
- package/src/decorators/bodyParams.spec.ts +121 -0
- package/src/decorators/bodyParams.ts +93 -0
- package/src/decorators/context.spec.ts +16 -0
- package/src/decorators/context.ts +70 -0
- package/src/decorators/cookies.spec.ts +19 -0
- package/src/decorators/cookies.ts +102 -0
- package/src/decorators/headerParams.spec.ts +16 -0
- package/src/decorators/headerParams.ts +49 -0
- package/src/decorators/locals.spec.ts +16 -0
- package/src/decorators/locals.ts +57 -0
- package/src/decorators/paramFn.ts +14 -0
- package/src/decorators/pathParams.spec.ts +58 -0
- package/src/decorators/pathParams.ts +97 -0
- package/src/decorators/queryParams.spec.ts +58 -0
- package/src/decorators/queryParams.ts +102 -0
- package/src/decorators/session.spec.ts +47 -0
- package/src/decorators/session.ts +51 -0
- package/src/decorators/useDeserialization.ts +14 -0
- package/src/decorators/useParam.spec.ts +82 -0
- package/src/decorators/useParam.ts +38 -0
- package/src/decorators/useParamExpression.ts +16 -0
- package/src/decorators/useParamType.ts +36 -0
- package/src/decorators/usePipe.ts +21 -0
- package/src/decorators/useType.ts +35 -0
- package/src/decorators/useValidation.ts +18 -0
- package/src/domain/ParamMetadata.ts +1 -0
- package/src/domain/ParamOptions.ts +12 -0
- package/src/domain/ParamTypes.ts +37 -0
- package/src/errors/ParamValidationError.spec.ts +152 -0
- package/src/errors/ParamValidationError.ts +38 -0
- package/src/errors/RequiredValidationError.spec.ts +49 -0
- package/src/errors/RequiredValidationError.ts +30 -0
- package/src/errors/ValidationError.spec.ts +18 -0
- package/src/errors/ValidationError.ts +11 -0
- package/src/exports.ts +1 -0
- package/src/index.ts +31 -0
- package/src/pipes/DeserializerPipe.spec.ts +39 -0
- package/src/pipes/DeserializerPipe.ts +15 -0
- package/src/pipes/ParseExpressionPipe.spec.ts +242 -0
- package/src/pipes/ParseExpressionPipe.ts +33 -0
- package/src/pipes/ValidationPipe.spec.ts +645 -0
- package/src/pipes/ValidationPipe.ts +112 -0
- package/src/utils/mapParamsOptions.spec.ts +28 -0
- package/src/utils/mapParamsOptions.ts +34 -0
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import {catchAsyncError} from "@tsed/core";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import {AllOf, AnyOf, CollectionOf, getSpec, JsonParameterStore, OneOf, Post, Property, Required, SpecTypes} from "@tsed/schema";
|
|
4
|
+
|
|
5
|
+
import {BodyParams} from "../decorators/bodyParams.js";
|
|
6
|
+
import {PathParams} from "../decorators/pathParams.js";
|
|
7
|
+
import {QueryParams} from "../decorators/queryParams.js";
|
|
8
|
+
import {ValidationPipe} from "./ValidationPipe.js";
|
|
9
|
+
|
|
10
|
+
describe("ValidationPipe", () => {
|
|
11
|
+
beforeEach(() => PlatformTest.create());
|
|
12
|
+
afterEach(() => PlatformTest.reset());
|
|
13
|
+
|
|
14
|
+
it("should return value (Body)", async () => {
|
|
15
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
validator.validator = undefined;
|
|
18
|
+
|
|
19
|
+
class Test {
|
|
20
|
+
@Post("/")
|
|
21
|
+
test(@BodyParams(String) type: string[]) {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// WHEN
|
|
25
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
26
|
+
const result = await validator.transform("value", param);
|
|
27
|
+
|
|
28
|
+
// THEN
|
|
29
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
30
|
+
paths: {
|
|
31
|
+
"/": {
|
|
32
|
+
post: {
|
|
33
|
+
operationId: "testTest",
|
|
34
|
+
parameters: [],
|
|
35
|
+
requestBody: {
|
|
36
|
+
content: {
|
|
37
|
+
"application/json": {
|
|
38
|
+
schema: {
|
|
39
|
+
items: {
|
|
40
|
+
type: "string"
|
|
41
|
+
},
|
|
42
|
+
type: "array"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
required: false
|
|
47
|
+
},
|
|
48
|
+
responses: {
|
|
49
|
+
"200": {
|
|
50
|
+
description: "Success"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
tags: ["Test"]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
tags: [
|
|
58
|
+
{
|
|
59
|
+
name: "Test"
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
});
|
|
63
|
+
expect(result).toEqual("value");
|
|
64
|
+
});
|
|
65
|
+
it("should return value (Body with array param)", async () => {
|
|
66
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
validator.validator = undefined;
|
|
69
|
+
|
|
70
|
+
class Test {
|
|
71
|
+
@Post("/")
|
|
72
|
+
test(@BodyParams() type: any[]) {}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// WHEN
|
|
76
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
77
|
+
const result = await validator.transform("value", param);
|
|
78
|
+
|
|
79
|
+
// THEN
|
|
80
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
81
|
+
paths: {
|
|
82
|
+
"/": {
|
|
83
|
+
post: {
|
|
84
|
+
operationId: "testTest",
|
|
85
|
+
parameters: [],
|
|
86
|
+
requestBody: {
|
|
87
|
+
content: {
|
|
88
|
+
"application/json": {
|
|
89
|
+
schema: {
|
|
90
|
+
items: {
|
|
91
|
+
anyOf: [
|
|
92
|
+
{
|
|
93
|
+
multipleOf: 1,
|
|
94
|
+
type: "integer"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
type: "number"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
type: "string"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
type: "boolean"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
type: "array"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: "object"
|
|
110
|
+
}
|
|
111
|
+
],
|
|
112
|
+
nullable: true
|
|
113
|
+
},
|
|
114
|
+
type: "array"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
required: false
|
|
119
|
+
},
|
|
120
|
+
responses: {
|
|
121
|
+
"200": {
|
|
122
|
+
description: "Success"
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
tags: ["Test"]
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
tags: [
|
|
130
|
+
{
|
|
131
|
+
name: "Test"
|
|
132
|
+
}
|
|
133
|
+
]
|
|
134
|
+
});
|
|
135
|
+
expect(result).toEqual("value");
|
|
136
|
+
});
|
|
137
|
+
it("should return value (Body with array oneOf)", async () => {
|
|
138
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
validator.validator = undefined;
|
|
141
|
+
|
|
142
|
+
class Model {
|
|
143
|
+
@Property()
|
|
144
|
+
id: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class Model2 {
|
|
148
|
+
@Property()
|
|
149
|
+
id: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
class Test {
|
|
153
|
+
@Post("/")
|
|
154
|
+
test(@BodyParams() @OneOf(Model, Model2) type: any[]) {}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// WHEN
|
|
158
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
159
|
+
const result = await validator.transform("value", param);
|
|
160
|
+
|
|
161
|
+
// THEN
|
|
162
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
163
|
+
components: {
|
|
164
|
+
schemas: {
|
|
165
|
+
Model: {
|
|
166
|
+
properties: {
|
|
167
|
+
id: {
|
|
168
|
+
type: "string"
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
type: "object"
|
|
172
|
+
},
|
|
173
|
+
Model2: {
|
|
174
|
+
properties: {
|
|
175
|
+
id: {
|
|
176
|
+
type: "string"
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
type: "object"
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
paths: {
|
|
184
|
+
"/": {
|
|
185
|
+
post: {
|
|
186
|
+
operationId: "testTest",
|
|
187
|
+
parameters: [],
|
|
188
|
+
requestBody: {
|
|
189
|
+
content: {
|
|
190
|
+
"application/json": {
|
|
191
|
+
schema: {
|
|
192
|
+
items: {
|
|
193
|
+
oneOf: [
|
|
194
|
+
{
|
|
195
|
+
$ref: "#/components/schemas/Model"
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
$ref: "#/components/schemas/Model2"
|
|
199
|
+
}
|
|
200
|
+
]
|
|
201
|
+
},
|
|
202
|
+
type: "array"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
required: false
|
|
207
|
+
},
|
|
208
|
+
responses: {
|
|
209
|
+
"200": {
|
|
210
|
+
description: "Success"
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
tags: ["Test"]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
tags: [
|
|
218
|
+
{
|
|
219
|
+
name: "Test"
|
|
220
|
+
}
|
|
221
|
+
]
|
|
222
|
+
});
|
|
223
|
+
expect(result).toEqual("value");
|
|
224
|
+
});
|
|
225
|
+
it("should return value (Body with array anyOf)", async () => {
|
|
226
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
227
|
+
// @ts-ignore
|
|
228
|
+
validator.validator = undefined;
|
|
229
|
+
|
|
230
|
+
class Model {
|
|
231
|
+
@Property()
|
|
232
|
+
id: string;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
class Model2 {
|
|
236
|
+
@Property()
|
|
237
|
+
id: string;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
class Test {
|
|
241
|
+
@Post("/")
|
|
242
|
+
test(@BodyParams() @AnyOf(Model, Model2) type: any[]) {}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// WHEN
|
|
246
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
247
|
+
const result = await validator.transform("value", param);
|
|
248
|
+
|
|
249
|
+
// THEN
|
|
250
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
251
|
+
components: {
|
|
252
|
+
schemas: {
|
|
253
|
+
Model: {
|
|
254
|
+
properties: {
|
|
255
|
+
id: {
|
|
256
|
+
type: "string"
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
type: "object"
|
|
260
|
+
},
|
|
261
|
+
Model2: {
|
|
262
|
+
properties: {
|
|
263
|
+
id: {
|
|
264
|
+
type: "string"
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
type: "object"
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
paths: {
|
|
272
|
+
"/": {
|
|
273
|
+
post: {
|
|
274
|
+
operationId: "testTest",
|
|
275
|
+
parameters: [],
|
|
276
|
+
requestBody: {
|
|
277
|
+
content: {
|
|
278
|
+
"application/json": {
|
|
279
|
+
schema: {
|
|
280
|
+
items: {
|
|
281
|
+
anyOf: [
|
|
282
|
+
{
|
|
283
|
+
$ref: "#/components/schemas/Model"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
$ref: "#/components/schemas/Model2"
|
|
287
|
+
}
|
|
288
|
+
]
|
|
289
|
+
},
|
|
290
|
+
type: "array"
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
required: false
|
|
295
|
+
},
|
|
296
|
+
responses: {
|
|
297
|
+
"200": {
|
|
298
|
+
description: "Success"
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
tags: ["Test"]
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
tags: [
|
|
306
|
+
{
|
|
307
|
+
name: "Test"
|
|
308
|
+
}
|
|
309
|
+
]
|
|
310
|
+
});
|
|
311
|
+
expect(result).toEqual("value");
|
|
312
|
+
});
|
|
313
|
+
it("should return value (Body with array allOf)", async () => {
|
|
314
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
315
|
+
// @ts-ignore
|
|
316
|
+
validator.validator = undefined;
|
|
317
|
+
|
|
318
|
+
class Model {
|
|
319
|
+
@Property()
|
|
320
|
+
id: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
class Model2 {
|
|
324
|
+
@Property()
|
|
325
|
+
id: string;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
class Test {
|
|
329
|
+
@Post("/")
|
|
330
|
+
test(@BodyParams() @AllOf(Model, Model2) type: any[]) {}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// WHEN
|
|
334
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
335
|
+
const result = await validator.transform("value", param);
|
|
336
|
+
|
|
337
|
+
// THEN
|
|
338
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
339
|
+
components: {
|
|
340
|
+
schemas: {
|
|
341
|
+
Model: {
|
|
342
|
+
properties: {
|
|
343
|
+
id: {
|
|
344
|
+
type: "string"
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
type: "object"
|
|
348
|
+
},
|
|
349
|
+
Model2: {
|
|
350
|
+
properties: {
|
|
351
|
+
id: {
|
|
352
|
+
type: "string"
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
type: "object"
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
paths: {
|
|
360
|
+
"/": {
|
|
361
|
+
post: {
|
|
362
|
+
operationId: "testTest",
|
|
363
|
+
parameters: [],
|
|
364
|
+
requestBody: {
|
|
365
|
+
content: {
|
|
366
|
+
"application/json": {
|
|
367
|
+
schema: {
|
|
368
|
+
items: {
|
|
369
|
+
allOf: [
|
|
370
|
+
{
|
|
371
|
+
$ref: "#/components/schemas/Model"
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
$ref: "#/components/schemas/Model2"
|
|
375
|
+
}
|
|
376
|
+
]
|
|
377
|
+
},
|
|
378
|
+
type: "array"
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
required: false
|
|
383
|
+
},
|
|
384
|
+
responses: {
|
|
385
|
+
"200": {
|
|
386
|
+
description: "Success"
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
tags: ["Test"]
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
tags: [
|
|
394
|
+
{
|
|
395
|
+
name: "Test"
|
|
396
|
+
}
|
|
397
|
+
]
|
|
398
|
+
});
|
|
399
|
+
expect(result).toEqual("value");
|
|
400
|
+
});
|
|
401
|
+
it("should skip validation for array path params", async () => {
|
|
402
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
403
|
+
// @ts-ignore
|
|
404
|
+
validator.validator = {
|
|
405
|
+
validate: vi.fn().mockImplementation((o) => o)
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
class Test {
|
|
409
|
+
@Post("/")
|
|
410
|
+
test(@PathParams() type: Object) {}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// WHEN
|
|
414
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
415
|
+
const result = await validator.transform("value", param);
|
|
416
|
+
|
|
417
|
+
// THEN
|
|
418
|
+
// @ts-ignore
|
|
419
|
+
expect(validator.validator.validate).not.toHaveBeenCalled();
|
|
420
|
+
expect(result).toEqual("value");
|
|
421
|
+
});
|
|
422
|
+
it("should return value (Query required)", async () => {
|
|
423
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
424
|
+
// @ts-ignore
|
|
425
|
+
validator.validator = undefined;
|
|
426
|
+
|
|
427
|
+
class Test {
|
|
428
|
+
@Post("/")
|
|
429
|
+
test(@QueryParams("test") @Required() @CollectionOf(String) type: string[]) {}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// WHEN
|
|
433
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
434
|
+
|
|
435
|
+
const result: any = await validator.transform(["test"], param);
|
|
436
|
+
|
|
437
|
+
// THEN
|
|
438
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
439
|
+
paths: {
|
|
440
|
+
"/": {
|
|
441
|
+
post: {
|
|
442
|
+
operationId: "testTest",
|
|
443
|
+
parameters: [
|
|
444
|
+
{
|
|
445
|
+
in: "query",
|
|
446
|
+
name: "test",
|
|
447
|
+
required: true,
|
|
448
|
+
schema: {
|
|
449
|
+
items: {
|
|
450
|
+
type: "string"
|
|
451
|
+
},
|
|
452
|
+
type: "array"
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
],
|
|
456
|
+
responses: {
|
|
457
|
+
"200": {
|
|
458
|
+
description: "Success"
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
tags: ["Test"]
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
tags: [
|
|
466
|
+
{
|
|
467
|
+
name: "Test"
|
|
468
|
+
}
|
|
469
|
+
]
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
expect(result).toEqual(["test"]);
|
|
473
|
+
});
|
|
474
|
+
it("should throw an error (Query required)", async () => {
|
|
475
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
476
|
+
// @ts-ignore
|
|
477
|
+
validator.validator = undefined;
|
|
478
|
+
|
|
479
|
+
class Test {
|
|
480
|
+
@Post("/")
|
|
481
|
+
test(@QueryParams("test") @Required() @CollectionOf(String) type: string[]) {}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// WHEN
|
|
485
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
486
|
+
|
|
487
|
+
const result: any = await catchAsyncError(() => validator.transform(undefined, param));
|
|
488
|
+
|
|
489
|
+
// THEN
|
|
490
|
+
expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
491
|
+
paths: {
|
|
492
|
+
"/": {
|
|
493
|
+
post: {
|
|
494
|
+
operationId: "testTest",
|
|
495
|
+
parameters: [
|
|
496
|
+
{
|
|
497
|
+
in: "query",
|
|
498
|
+
name: "test",
|
|
499
|
+
required: true,
|
|
500
|
+
schema: {
|
|
501
|
+
items: {
|
|
502
|
+
type: "string"
|
|
503
|
+
},
|
|
504
|
+
type: "array"
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
],
|
|
508
|
+
responses: {
|
|
509
|
+
"200": {
|
|
510
|
+
description: "Success"
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
tags: ["Test"]
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
tags: [
|
|
518
|
+
{
|
|
519
|
+
name: "Test"
|
|
520
|
+
}
|
|
521
|
+
]
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
expect(result.message).toEqual("It should have required parameter 'test'");
|
|
525
|
+
});
|
|
526
|
+
it("should cast data if it's possible", async () => {
|
|
527
|
+
const defaultValidator = {
|
|
528
|
+
validate: vi.fn().mockResolvedValue("1")
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
532
|
+
|
|
533
|
+
(validator as any).validators.set("default", defaultValidator);
|
|
534
|
+
|
|
535
|
+
class Test {
|
|
536
|
+
@Post("/")
|
|
537
|
+
test(@QueryParams("test") @Required() type: string) {}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// WHEN
|
|
541
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
542
|
+
|
|
543
|
+
const result: any = await validator.transform(1, param);
|
|
544
|
+
|
|
545
|
+
// THEN
|
|
546
|
+
expect(result).toEqual("1");
|
|
547
|
+
expect(defaultValidator.validate).toHaveBeenCalledWith("1", {
|
|
548
|
+
collectionType: undefined,
|
|
549
|
+
schema: {type: "string", minLength: 1},
|
|
550
|
+
type: undefined
|
|
551
|
+
});
|
|
552
|
+
});
|
|
553
|
+
it("should cast string to array", async () => {
|
|
554
|
+
const defaultValidator = {
|
|
555
|
+
validate: vi.fn().mockImplementation((o) => o)
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
559
|
+
|
|
560
|
+
(validator as any).validators.set("default", defaultValidator);
|
|
561
|
+
|
|
562
|
+
class Test {
|
|
563
|
+
@Post("/")
|
|
564
|
+
test(@QueryParams("test") @Required() type: string[]) {}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// WHEN
|
|
568
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
569
|
+
|
|
570
|
+
const result: any = await validator.transform(1, param);
|
|
571
|
+
|
|
572
|
+
// THEN
|
|
573
|
+
expect(result).toEqual([1]);
|
|
574
|
+
expect(defaultValidator.validate).toHaveBeenCalledWith([1], expect.any(Object));
|
|
575
|
+
});
|
|
576
|
+
it("shouldn't cast object", async () => {
|
|
577
|
+
const defaultValidator = {
|
|
578
|
+
validate: vi.fn().mockImplementation((o) => o)
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
582
|
+
|
|
583
|
+
(validator as any).validators.set("default", defaultValidator);
|
|
584
|
+
|
|
585
|
+
class Test {
|
|
586
|
+
@Post("/")
|
|
587
|
+
test(@QueryParams("test") @Required() type: any) {}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// WHEN
|
|
591
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
592
|
+
|
|
593
|
+
const result: any = await validator.transform({}, param);
|
|
594
|
+
|
|
595
|
+
// THEN
|
|
596
|
+
expect(result).toEqual({});
|
|
597
|
+
expect(defaultValidator.validate).toHaveBeenCalledWith({}, expect.any(Object));
|
|
598
|
+
});
|
|
599
|
+
it("should cast null string to null", async () => {
|
|
600
|
+
const defaultValidator = {
|
|
601
|
+
validate: vi.fn().mockImplementation((o) => o)
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
605
|
+
|
|
606
|
+
(validator as any).validators.set("default", defaultValidator);
|
|
607
|
+
|
|
608
|
+
class Test {
|
|
609
|
+
@Post("/")
|
|
610
|
+
test(@QueryParams("test") type: string[]) {}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// WHEN
|
|
614
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
615
|
+
|
|
616
|
+
const result: any = await validator.transform("null", param);
|
|
617
|
+
|
|
618
|
+
// THEN
|
|
619
|
+
expect(result).toEqual(null);
|
|
620
|
+
expect(defaultValidator.validate).toHaveBeenCalledWith(null, expect.any(Object));
|
|
621
|
+
});
|
|
622
|
+
it("should not process undefined value", async () => {
|
|
623
|
+
const defaultValidator = {
|
|
624
|
+
validate: vi.fn().mockImplementation((o) => o)
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const validator = await PlatformTest.invoke<ValidationPipe>(ValidationPipe);
|
|
628
|
+
|
|
629
|
+
(validator as any).validators.set("default", defaultValidator);
|
|
630
|
+
|
|
631
|
+
class Test {
|
|
632
|
+
@Post("/")
|
|
633
|
+
test(@QueryParams("test") type: string) {}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// WHEN
|
|
637
|
+
const param = JsonParameterStore.get(Test, "test", 0);
|
|
638
|
+
|
|
639
|
+
const result: any = await validator.transform(undefined, param);
|
|
640
|
+
|
|
641
|
+
// THEN
|
|
642
|
+
expect(result).toEqual(undefined);
|
|
643
|
+
expect(defaultValidator.validate).not.toHaveBeenCalled();
|
|
644
|
+
});
|
|
645
|
+
});
|