@tsed/platform-params 8.9.1 → 8.9.2

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/platform-params",
3
3
  "description": "Params module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.9.1",
5
+ "version": "8.9.2",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -26,23 +26,23 @@
26
26
  "tslib": "2.7.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@tsed/barrels": "8.9.1",
30
- "@tsed/core": "8.9.1",
31
- "@tsed/di": "8.9.1",
32
- "@tsed/exceptions": "8.9.1",
33
- "@tsed/json-mapper": "8.9.1",
34
- "@tsed/schema": "8.9.1",
35
- "@tsed/typescript": "8.9.1",
29
+ "@tsed/barrels": "8.9.2",
30
+ "@tsed/core": "8.9.2",
31
+ "@tsed/di": "8.9.2",
32
+ "@tsed/exceptions": "8.9.2",
33
+ "@tsed/json-mapper": "8.9.2",
34
+ "@tsed/schema": "8.9.2",
35
+ "@tsed/typescript": "8.9.2",
36
36
  "eslint": "9.12.0",
37
37
  "typescript": "5.4.5",
38
38
  "vitest": "3.1.1"
39
39
  },
40
40
  "peerDependencies": {
41
- "@tsed/core": ">=8.9.1",
42
- "@tsed/di": ">=8.9.1",
43
- "@tsed/exceptions": ">=8.9.1",
44
- "@tsed/json-mapper": ">=8.9.1",
45
- "@tsed/schema": ">=8.9.1"
41
+ "@tsed/core": ">=8.9.2",
42
+ "@tsed/di": ">=8.9.2",
43
+ "@tsed/exceptions": ">=8.9.2",
44
+ "@tsed/json-mapper": ">=8.9.2",
45
+ "@tsed/schema": ">=8.9.2"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "@tsed/core": {
@@ -0,0 +1,105 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`@BodyParams > should generate the correct spec for BodyParams from given arg 1`] = `
4
+ {
5
+ "components": {
6
+ "schemas": {
7
+ "TestModel": {
8
+ "properties": {
9
+ "name": {
10
+ "minLength": 1,
11
+ "type": "string",
12
+ },
13
+ },
14
+ "required": [
15
+ "name",
16
+ ],
17
+ "type": "object",
18
+ },
19
+ },
20
+ },
21
+ "paths": {
22
+ "/": {
23
+ "post": {
24
+ "operationId": "myCtrlTest",
25
+ "parameters": [],
26
+ "requestBody": {
27
+ "content": {
28
+ "application/json": {
29
+ "schema": {
30
+ "$ref": "#/components/schemas/TestModel",
31
+ },
32
+ },
33
+ },
34
+ "required": false,
35
+ },
36
+ "responses": {
37
+ "200": {
38
+ "description": "Success",
39
+ },
40
+ },
41
+ "tags": [
42
+ "MyCtrl",
43
+ ],
44
+ },
45
+ },
46
+ },
47
+ "tags": [
48
+ {
49
+ "name": "MyCtrl",
50
+ },
51
+ ],
52
+ }
53
+ `;
54
+
55
+ exports[`@BodyParams > should generate the correct spec for BodyParams from metadata 1`] = `
56
+ {
57
+ "components": {
58
+ "schemas": {
59
+ "TestModel": {
60
+ "properties": {
61
+ "name": {
62
+ "minLength": 1,
63
+ "type": "string",
64
+ },
65
+ },
66
+ "required": [
67
+ "name",
68
+ ],
69
+ "type": "object",
70
+ },
71
+ },
72
+ },
73
+ "paths": {
74
+ "/": {
75
+ "post": {
76
+ "operationId": "myCtrlTest",
77
+ "parameters": [],
78
+ "requestBody": {
79
+ "content": {
80
+ "application/json": {
81
+ "schema": {
82
+ "$ref": "#/components/schemas/TestModel",
83
+ },
84
+ },
85
+ },
86
+ "required": false,
87
+ },
88
+ "responses": {
89
+ "200": {
90
+ "description": "Success",
91
+ },
92
+ },
93
+ "tags": [
94
+ "MyCtrl",
95
+ ],
96
+ },
97
+ },
98
+ },
99
+ "tags": [
100
+ {
101
+ "name": "MyCtrl",
102
+ },
103
+ ],
104
+ }
105
+ `;
@@ -1,5 +1,5 @@
1
1
  import {Controller} from "@tsed/di";
2
- import {getSpec, JsonParameterStore, Post, SpecTypes} from "@tsed/schema";
2
+ import {Description, getJsonSchema, getSpec, JsonParameterStore, MaxLength, MinItems, Post, Required, SpecTypes} from "@tsed/schema";
3
3
 
4
4
  import {ParamTypes} from "../domain/ParamTypes.js";
5
5
  import {BodyParams, RawBodyParams} from "./bodyParams.js";
@@ -58,6 +58,317 @@ describe("@BodyParams", () => {
58
58
  ]
59
59
  });
60
60
  });
61
+ it("should generate the correct spec for BodyParams from metadata (string)", () => {
62
+ class TestModel {
63
+ @Required()
64
+ name: string;
65
+ }
66
+
67
+ @Controller("/")
68
+ class MyCtrl {
69
+ @Post()
70
+ test(@BodyParams() @Required() @MaxLength(10) @Description("Description") body: string) {}
71
+ }
72
+
73
+ const spec = getSpec(MyCtrl, {specType: SpecTypes.OPENAPI});
74
+
75
+ expect(spec).toEqual({
76
+ paths: {
77
+ "/": {
78
+ post: {
79
+ operationId: "myCtrlTest",
80
+ parameters: [],
81
+ requestBody: {
82
+ description: "Description",
83
+ content: {
84
+ "application/json": {
85
+ schema: {
86
+ minLength: 1,
87
+ maxLength: 10,
88
+ type: "string"
89
+ }
90
+ }
91
+ },
92
+ required: true
93
+ },
94
+ responses: {
95
+ "200": {
96
+ description: "Success"
97
+ }
98
+ },
99
+ tags: ["MyCtrl"]
100
+ }
101
+ }
102
+ },
103
+ tags: [
104
+ {
105
+ name: "MyCtrl"
106
+ }
107
+ ]
108
+ });
109
+ });
110
+ it("should generate the correct spec for BodyParams from metadata (string[])", () => {
111
+ class TestModel {
112
+ @Required()
113
+ name: string;
114
+ }
115
+
116
+ @Controller("/")
117
+ class MyCtrl {
118
+ @Post()
119
+ test(@BodyParams(String) @Required() @MaxLength(10) @MinItems(3) @Description("Description") body: string[]) {}
120
+ }
121
+
122
+ const spec = getSpec(MyCtrl, {specType: SpecTypes.OPENAPI});
123
+
124
+ expect(spec).toEqual({
125
+ paths: {
126
+ "/": {
127
+ post: {
128
+ operationId: "myCtrlTest",
129
+ parameters: [],
130
+ requestBody: {
131
+ description: "Description",
132
+ content: {
133
+ "application/json": {
134
+ schema: {
135
+ items: {
136
+ maxLength: 10,
137
+ type: "string"
138
+ },
139
+ minItems: 3,
140
+ type: "array"
141
+ }
142
+ }
143
+ },
144
+ required: true
145
+ },
146
+ responses: {
147
+ "200": {
148
+ description: "Success"
149
+ }
150
+ },
151
+ tags: ["MyCtrl"]
152
+ }
153
+ }
154
+ },
155
+ tags: [
156
+ {
157
+ name: "MyCtrl"
158
+ }
159
+ ]
160
+ });
161
+ });
162
+ it("should generate the correct spec for BodyParams from metadata (TestModel)", () => {
163
+ class TestModel {
164
+ @Required()
165
+ name: string;
166
+ }
167
+
168
+ @Controller("/")
169
+ class MyCtrl {
170
+ @Post()
171
+ test(@BodyParams() @Required() @Description("Description") body: TestModel) {}
172
+ }
173
+
174
+ const spec = getSpec(MyCtrl, {specType: SpecTypes.OPENAPI});
175
+
176
+ expect(spec).toEqual({
177
+ components: {
178
+ schemas: {
179
+ TestModel: {
180
+ properties: {
181
+ name: {
182
+ minLength: 1,
183
+ type: "string"
184
+ }
185
+ },
186
+ required: ["name"],
187
+ type: "object"
188
+ }
189
+ }
190
+ },
191
+ paths: {
192
+ "/": {
193
+ post: {
194
+ operationId: "myCtrlTest",
195
+ parameters: [],
196
+ requestBody: {
197
+ description: "Description",
198
+ content: {
199
+ "application/json": {
200
+ schema: {$ref: "#/components/schemas/TestModel"}
201
+ }
202
+ },
203
+ required: true
204
+ },
205
+ responses: {
206
+ "200": {
207
+ description: "Success"
208
+ }
209
+ },
210
+ tags: ["MyCtrl"]
211
+ }
212
+ }
213
+ },
214
+ tags: [
215
+ {
216
+ name: "MyCtrl"
217
+ }
218
+ ]
219
+ });
220
+ const param = JsonParameterStore.get(MyCtrl, "test", 0);
221
+ const schema = getJsonSchema(param);
222
+
223
+ expect(schema).toEqual({
224
+ properties: {
225
+ name: {
226
+ minLength: 1,
227
+ type: "string"
228
+ }
229
+ },
230
+ required: ["name"],
231
+ type: "object"
232
+ });
233
+ });
234
+ it("should generate the correct spec for BodyParams from metadata (TestModel[])", () => {
235
+ class TestModel {
236
+ @Required()
237
+ name: string;
238
+ }
239
+
240
+ @Controller("/")
241
+ class MyCtrl {
242
+ @Post()
243
+ test(@BodyParams(TestModel) @MinItems(2) @Required() @Description("Description") body: TestModel[]) {}
244
+ }
245
+
246
+ const spec = getSpec(MyCtrl, {specType: SpecTypes.OPENAPI});
247
+
248
+ expect(spec).toEqual({
249
+ components: {
250
+ schemas: {
251
+ TestModel: {
252
+ properties: {
253
+ name: {
254
+ minLength: 1,
255
+ type: "string"
256
+ }
257
+ },
258
+ required: ["name"],
259
+ type: "object"
260
+ }
261
+ }
262
+ },
263
+ paths: {
264
+ "/": {
265
+ post: {
266
+ operationId: "myCtrlTest",
267
+ parameters: [],
268
+ requestBody: {
269
+ content: {
270
+ "application/json": {
271
+ schema: {
272
+ items: {
273
+ $ref: "#/components/schemas/TestModel"
274
+ },
275
+ minItems: 2,
276
+ type: "array"
277
+ }
278
+ }
279
+ },
280
+ description: "Description",
281
+ required: true
282
+ },
283
+ responses: {
284
+ "200": {
285
+ description: "Success"
286
+ }
287
+ },
288
+ tags: ["MyCtrl"]
289
+ }
290
+ }
291
+ },
292
+ tags: [
293
+ {
294
+ name: "MyCtrl"
295
+ }
296
+ ]
297
+ });
298
+ });
299
+ it("should generate the correct spec for BodyParams from given type (TestModel)", () => {
300
+ class TestModel {
301
+ @Required()
302
+ name: string;
303
+ }
304
+
305
+ @Controller("/")
306
+ class MyCtrl {
307
+ @Post()
308
+ test(@BodyParams(TestModel) @Required() @Description("Description") body: any) {}
309
+ }
310
+
311
+ const spec = getSpec(MyCtrl, {specType: SpecTypes.OPENAPI});
312
+
313
+ expect(spec).toEqual({
314
+ components: {
315
+ schemas: {
316
+ TestModel: {
317
+ properties: {
318
+ name: {
319
+ minLength: 1,
320
+ type: "string"
321
+ }
322
+ },
323
+ required: ["name"],
324
+ type: "object"
325
+ }
326
+ }
327
+ },
328
+ paths: {
329
+ "/": {
330
+ post: {
331
+ operationId: "myCtrlTest",
332
+ parameters: [],
333
+ requestBody: {
334
+ description: "Description",
335
+ content: {
336
+ "application/json": {
337
+ schema: {$ref: "#/components/schemas/TestModel"}
338
+ }
339
+ },
340
+ required: true
341
+ },
342
+ responses: {
343
+ "200": {
344
+ description: "Success"
345
+ }
346
+ },
347
+ tags: ["MyCtrl"]
348
+ }
349
+ }
350
+ },
351
+ tags: [
352
+ {
353
+ name: "MyCtrl"
354
+ }
355
+ ]
356
+ });
357
+
358
+ const param = JsonParameterStore.get(MyCtrl, "test", 0);
359
+ const schema = getJsonSchema(param);
360
+
361
+ expect(schema).toEqual({
362
+ properties: {
363
+ name: {
364
+ minLength: 1,
365
+ type: "string"
366
+ }
367
+ },
368
+ required: ["name"],
369
+ type: "object"
370
+ });
371
+ });
61
372
  });
62
373
 
63
374
  describe("RawBodyParams()", () => {