@tsed/platform-params 8.11.1 → 8.12.0-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.
Files changed (107) hide show
  1. package/lib/esm/builder/PlatformParams.js +0 -1
  2. package/lib/esm/decorators/bodyParams.js +0 -1
  3. package/lib/esm/decorators/context.js +0 -1
  4. package/lib/esm/decorators/cookies.js +0 -1
  5. package/lib/esm/decorators/headerParams.js +0 -1
  6. package/lib/esm/decorators/locals.js +0 -1
  7. package/lib/esm/decorators/paramFn.js +0 -1
  8. package/lib/esm/decorators/pathParams.js +0 -1
  9. package/lib/esm/decorators/queryParams.js +0 -1
  10. package/lib/esm/decorators/session.js +0 -1
  11. package/lib/esm/decorators/useDeserialization.js +0 -1
  12. package/lib/esm/decorators/useParam.js +0 -1
  13. package/lib/esm/decorators/useParamExpression.js +0 -1
  14. package/lib/esm/decorators/useParamType.js +0 -1
  15. package/lib/esm/decorators/usePipe.js +0 -1
  16. package/lib/esm/decorators/useType.js +0 -1
  17. package/lib/esm/decorators/useValidation.js +0 -1
  18. package/lib/esm/domain/ParamMetadata.js +0 -1
  19. package/lib/esm/domain/ParamOptions.js +0 -1
  20. package/lib/esm/domain/ParamTypes.js +0 -1
  21. package/lib/esm/errors/ParamValidationError.js +0 -1
  22. package/lib/esm/errors/RequiredValidationError.js +0 -1
  23. package/lib/esm/errors/ValidationError.js +0 -1
  24. package/lib/esm/exports.js +0 -1
  25. package/lib/esm/index.js +0 -1
  26. package/lib/esm/pipes/DeserializerPipe.js +0 -1
  27. package/lib/esm/pipes/ParseExpressionPipe.js +0 -1
  28. package/lib/esm/pipes/ValidationPipe.js +0 -1
  29. package/lib/esm/utils/mapParamsOptions.js +0 -1
  30. package/package.json +12 -9
  31. package/lib/esm/builder/PlatformParams.js.map +0 -1
  32. package/lib/esm/decorators/bodyParams.js.map +0 -1
  33. package/lib/esm/decorators/context.js.map +0 -1
  34. package/lib/esm/decorators/cookies.js.map +0 -1
  35. package/lib/esm/decorators/headerParams.js.map +0 -1
  36. package/lib/esm/decorators/locals.js.map +0 -1
  37. package/lib/esm/decorators/paramFn.js.map +0 -1
  38. package/lib/esm/decorators/pathParams.js.map +0 -1
  39. package/lib/esm/decorators/queryParams.js.map +0 -1
  40. package/lib/esm/decorators/session.js.map +0 -1
  41. package/lib/esm/decorators/useDeserialization.js.map +0 -1
  42. package/lib/esm/decorators/useParam.js.map +0 -1
  43. package/lib/esm/decorators/useParamExpression.js.map +0 -1
  44. package/lib/esm/decorators/useParamType.js.map +0 -1
  45. package/lib/esm/decorators/usePipe.js.map +0 -1
  46. package/lib/esm/decorators/useType.js.map +0 -1
  47. package/lib/esm/decorators/useValidation.js.map +0 -1
  48. package/lib/esm/domain/ParamMetadata.js.map +0 -1
  49. package/lib/esm/domain/ParamOptions.js.map +0 -1
  50. package/lib/esm/domain/ParamTypes.js.map +0 -1
  51. package/lib/esm/errors/ParamValidationError.js.map +0 -1
  52. package/lib/esm/errors/RequiredValidationError.js.map +0 -1
  53. package/lib/esm/errors/ValidationError.js.map +0 -1
  54. package/lib/esm/exports.js.map +0 -1
  55. package/lib/esm/index.js.map +0 -1
  56. package/lib/esm/pipes/DeserializerPipe.js.map +0 -1
  57. package/lib/esm/pipes/ParseExpressionPipe.js.map +0 -1
  58. package/lib/esm/pipes/ValidationPipe.js.map +0 -1
  59. package/lib/esm/utils/mapParamsOptions.js.map +0 -1
  60. package/src/builder/PlatformParams.spec.ts +0 -431
  61. package/src/builder/PlatformParams.ts +0 -92
  62. package/src/decorators/__snapshots__/bodyParams.spec.ts.snap +0 -105
  63. package/src/decorators/bodyParams.spec.ts +0 -432
  64. package/src/decorators/bodyParams.ts +0 -93
  65. package/src/decorators/context.spec.ts +0 -16
  66. package/src/decorators/context.ts +0 -70
  67. package/src/decorators/cookies.spec.ts +0 -19
  68. package/src/decorators/cookies.ts +0 -102
  69. package/src/decorators/headerParams.spec.ts +0 -16
  70. package/src/decorators/headerParams.ts +0 -49
  71. package/src/decorators/locals.spec.ts +0 -16
  72. package/src/decorators/locals.ts +0 -57
  73. package/src/decorators/paramFn.ts +0 -14
  74. package/src/decorators/pathParams.spec.ts +0 -58
  75. package/src/decorators/pathParams.ts +0 -97
  76. package/src/decorators/queryParams.spec.ts +0 -58
  77. package/src/decorators/queryParams.ts +0 -102
  78. package/src/decorators/session.spec.ts +0 -47
  79. package/src/decorators/session.ts +0 -51
  80. package/src/decorators/useDeserialization.ts +0 -14
  81. package/src/decorators/useParam.spec.ts +0 -82
  82. package/src/decorators/useParam.ts +0 -38
  83. package/src/decorators/useParamExpression.ts +0 -16
  84. package/src/decorators/useParamType.ts +0 -36
  85. package/src/decorators/usePipe.ts +0 -21
  86. package/src/decorators/useType.ts +0 -35
  87. package/src/decorators/useValidation.ts +0 -18
  88. package/src/domain/ParamMetadata.ts +0 -1
  89. package/src/domain/ParamOptions.ts +0 -12
  90. package/src/domain/ParamTypes.ts +0 -37
  91. package/src/errors/ParamValidationError.spec.ts +0 -152
  92. package/src/errors/ParamValidationError.ts +0 -38
  93. package/src/errors/RequiredValidationError.spec.ts +0 -49
  94. package/src/errors/RequiredValidationError.ts +0 -30
  95. package/src/errors/ValidationError.spec.ts +0 -18
  96. package/src/errors/ValidationError.ts +0 -11
  97. package/src/exports.ts +0 -1
  98. package/src/index.ts +0 -31
  99. package/src/pipes/DeserializerPipe.spec.ts +0 -39
  100. package/src/pipes/DeserializerPipe.ts +0 -15
  101. package/src/pipes/ParseExpressionPipe.spec.ts +0 -242
  102. package/src/pipes/ParseExpressionPipe.ts +0 -33
  103. package/src/pipes/ValidationPipe.spec.ts +0 -645
  104. package/src/pipes/ValidationPipe.ts +0 -112
  105. package/src/utils/mapParamsOptions.spec.ts +0 -28
  106. package/src/utils/mapParamsOptions.ts +0 -34
  107. package/vitest.config.mts +0 -21
@@ -1,645 +0,0 @@
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
- });