asteasolutions / zod-to-openapi

A library that generates OpenAPI (Swagger) docs from Zod schemas
MIT License
955 stars 60 forks source link

Missing multipart/form-data option #264

Closed khusenov closed 1 day ago

khusenov commented 3 weeks ago

How do I define a form-data schema to upload files in an API request?

I couldn't find it in the documentation.

AGalabov commented 1 day ago

Hello @khusenov you can take a look at Open API's documentation for it - https://swagger.io/docs/specification/v3_0/describing-request-body/describing-request-body/#form-data.

In terms of an API it should be just providing the media type multipart/form-data. This happens as the key of a request.body. So it would be something like:


registry.registerPath({
  method: 'post',
  path: '/some/path',
  description: 'Multipart',
  request: {
    body: {
      content: {
        'multipart/form-data': { // note this!
          schema: z.any() // You can use any form of validation here
        },
      },
    },
  },
  responses: {
    200: {
      content: {
        'application/json': {
          schema: z.object({
            message: z.string(),
          }),
        },
      },
      description: '',
    },
  },
});
khusenov commented 1 day ago

@AGalabov Thank you for the reply.

I was thinking the same thing, but library provides only application/json.

Maybe this is because zod doesn't support file types

Anyway, I hardcoded without using zod to solve this.