Skip to content

How to define schema when request is form data? #785

Answered by sinclairzx81
dev-uros asked this question in Q&A
Discussion options

You must be logged in to vote

@dev-uros Hi,

I'm not familiar with Fastify multipart form data package, but taking a look at the documentation, you should be able to do.

https://github.com/fastify/fastify-multipart?tab=readme-ov-file#json-schema-body-validation

fastify.register(require('@fastify/multipart'), { attachFieldsToBody: 'keyValues' })

fastify.post('/upload/files', {
  schema: {
    consumes: ['multipart/form-data'],
    body: {
      type: 'object',
      required: ['myFile'],
      properties: {
        // file that gets decoded to string
        myFile: {
          type: 'object',
        },
        hello: {
          type: 'string',
          enum: ['world']
        }
      }
    }
  }
}, function (req, r…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dev-uros
Comment options

Answer selected by dev-uros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants