#How to pass object array that can contain file in nestjs documenting with swagger

1 messages · Page 1 of 1 (latest)

late rain
#

I'm working on a NestJS application and I'm facing a challenge with handling an array of objects in a form-data request. Each object in the array represents an image with associated metadata like "id", "image" (which can be a File object or an image URL), "title", and "description".

I need guidance on how to handle this request effectively while allowing for the upload of new image files and updating the existing ones with URLs if they are provided.

[{
"id": "0",
"image": File Object,
"title": "some title",
"description": "some description"
},
{
"id": "1",
"image": File Object",
"title": "some title 1",
"description": "some description 1"
}]

#1025199348096700476

final thunder
#
@ApiBody({ 
    description: 'Image upload metadata',
    type: 'multipart/form-data',
    required: true,
    schema: {
      type: 'object',
      properties: {
        image: {
          type: 'file',
          description: 'Image to upload',
        },
        title: {
           type: 'string',
           description: 'Title of image'
        },
        // ...
      },
    },
  })

Maybe you can try adding the decorator above on top of you route handler. But the part I didnt quite get is how can we have arrays of objects as you have described above in muti/part submission. As far as I know, it's not possible. Multipart form data allows you to specify key value pairs only. For instance you could have a key named files that holds an array of files but having json like array as you described, I dont think it's possible with multi-part form data