#Swagger UI in NestJS displays form data property as string instead of array

2 messages · Page 1 of 1 (latest)

keen coral
#

I'm working on a NestJS application where I use Swagger UI for API documentation. I have a DTO with a property called prices, which is supposed to be an array of objects. However, Swagger UI is sending it as a string instead.

Here is my Zod price schema

    prices: z.array(
      z.object({ currencyId: z.coerce.number(), price: z.coerce.number() }),
    ),

it is showing in the swagger as you can see in the img but when sending the request it is received as a string like this

{
  prices: '[{\r\n  "currencyId": 0,\r\n  "price": 0\r\n}]'
}

and when adding one more item to the 'array' it shows up like this when logging it

  prices: '{\r\n' +
    '  "currencyId": 0,\r\n' +
    '  "price": 0\r\n' +
    '},{\r\n' +
    '  "currencyId": 0,\r\n' +
    '  "price": 0\r\n' +
    '}',

The thing is It's not as easy as just parse it because i am using Zod which throws an error because it expected an array in the schema definition and i am sending a string.!
What is the proper solution to this

#

Swagger UI in NestJS displays form data property as string instead of array