Search code examples
validationhapi.jsjoi

How to validate uploaded file size with Joi


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.


Solution

  • 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.