How to define an array of arrays of something with @tsed/schema? #2916
-
I basically need something like I tried some workarounds by directly defining the schema via |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Ts.ED provides a Function API to describe json-schema (look https://tsed.dev/docs/model.html#using-functions). Here is a working example: import {Controller} from "@tsed/di";
import {BodyParams} from "@tsed/platform-params";
import {Post, array, Property, Required, Schema, getSpec} from "@tsed/schema";
describe("collection", function() {
describe("collection of collection of model", () => {
it("should generate a collection of collection of model", () => {
class InternalStructure {
@Required()
@Property(String)
public field: string;
}
const ArrayOfArrayOfInternalStructure = array()
.items(array().items(InternalStructure))
.label("ArrayOfArrayOfInternalStructure");
expect(ArrayOfArrayOfInternalStructure.toJSON()).toMatchInlineSnapshot(`
{
"definitions": {
"InternalStructure": {
"properties": {
"field": {
"minLength": 1,
"type": "string",
},
},
"required": [
"field",
],
"type": "object",
},
},
"items": {
"items": {
"$ref": "#/definitions/InternalStructure",
},
"type": "array",
},
"type": "array",
}
`);
@Controller("/hello-world")
class HelloWorldController {
@Post("/")
post(@BodyParams() @Schema(ArrayOfArrayOfInternalStructure) items: InternalStructure[][]): void {
console.log(items);
}
}
expect(getSpec(HelloWorldController)).toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"ArrayOfArrayOfInternalStructure": {
"items": {
"items": {
"$ref": "#/components/schemas/InternalStructure",
},
"type": "array",
},
"type": "array",
},
"InternalStructure": {
"properties": {
"field": {
"minLength": 1,
"type": "string",
},
},
"required": [
"field",
],
"type": "object",
},
},
},
"paths": {
"/hello-world": {
"post": {
"operationId": "helloWorldControllerPost",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ArrayOfArrayOfInternalStructure",
},
},
},
"required": false,
},
"responses": {
"200": {
"description": "Success",
},
},
"tags": [
"HelloWorldController",
],
},
},
},
"tags": [
{
"name": "HelloWorldController",
},
],
}
`);
});
});
}); See you ;) |
Beta Was this translation helpful? Give feedback.
Hi @KBroichhausen
Ts.ED provides a Function API to describe json-schema (look https://tsed.dev/docs/model.html#using-functions).
Here is a working example: