V
V
Vladimir Golub2022-01-19 15:46:50
API
Vladimir Golub, 2022-01-19 15:46:50

How to describe an object in swagger, for which we do not exactly know the names of the properties?

I get an object of the form: [product name]: DTO with its properties

export class ProductsDto {
  [product: string]: ProductDto
}

export class ProductDto {
  @ApiResponseProperty()
  id: number;

  @ApiResponseProperty()
  price: number;
}


In the output I get an empty object, I use NestJS.

How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2022-01-19
@iDmitriyWinX

And what prevents you from writing ProductDTO[]instead of creating a separate DTO? This will also work, or better yet, turn it on in the validation configuration in your application to be sure, so to speak. If validation is not enabled in your application, then I advise you to do it globally in the filetransform: true

Only read if you're new to Nest
Вы же не пишете отдельные ValidationPipe для каждого Create эндпоинта?

app.useGlobalPipes(new ValidationPipe({ transform: true }));

Then we easily pull it out in the controller
@Body() productsDto: ProductDto[]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question