How to validate uploaded file size with Joi

Reza Owliaei picture Reza Owliaei · Jun 7, 2016 · Viewed 8.6k times · Source

I'm using Joi to validate requests. I'm wondering how should I validate uploaded file size with Joi.

I'm sending many files as a stream.

Answer

Jitendra picture Jitendra · Jun 27, 2018

To limits the size of incoming payloads to the specified byte count you can use as follow:

route.options.payload.maxBytes

for example:

      payload: {
          maxBytes: 20715200,
          output: 'stream',
          parse: true,
          allow: 'multipart/form-data'
      },
      validate: {
          payload: {
                bannerImage: Joi.any()
                .meta({swaggerType: 'file'})
                .optional()
                .allow('')
                .description('image file'),
                   },
          failAction: UniversalFunctions.failActionFunction
      },
  • Default value will be 1048576 (1MB).
  • Allowing very large payloads may cause the server to run out of memory.