arb / celebrate

A joi validation middleware for Express.
MIT License
1.34k stars 65 forks source link

integrate celebrate with multipart/form-data #204

Closed LucasRobs closed 3 years ago

LucasRobs commented 3 years ago

My joi schema looks like this:

routes.post('/product', celebrate({
    [Segments.HEADERS]: Joi.object({
        authorization: Joi.number().required()
    }).unknown(),
    [Segments.BODY]: Joi.object().keys({
        nameProduct: Joi.string().required(),
        codeProduct: Joi.string().required(),
        priceProduct: Joi.number().required(),
        descriptionProduct: Joi.string().optional(),
        weightProduct: Joi.string().required(),
        fkIdType: Joi.number().required(),
        image: Joi.string().optional(),
    })
}), upload.single('image'), ProductController.create);

Here is an example value that is not working as expected:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "celebrate request validation failed",
  "validation": {
    "body": {
      "source": "body",
      "keys": [
        "codeProduct"
      ],
      "message": "\"codeProduct\" is required"
    }
  }
}

The issue I am having with celebrate is:

Hello, I'm fighting with a great problem, I can`t to integrate celebrate with multipart/form-data, can you help me?

joaovictor3g commented 3 years ago

so much important issue to us!

arb commented 3 years ago

Can I see more of your express server code? Also what module are you trying to use to handle multipart/form-data? Your GitHub issue is pretty bare. There are probably fifteen different modules out there for multipart form data and they all work different 😅

LucasRobs commented 3 years ago

what part of my express server code do you want experimentally?I am using Multer. could you leave a link with one of those solutions that you talked about? thank you for all attention :+1:

arb commented 3 years ago

Try moving the celebrate call after the call to upload.single. My hunch is that multer is parsing the incoming payload for you so if you try to validate it with celebrate before it's been parsed, req.body will likely be empty.

Should look something like:

routes.post('/product', upload.single('image'), celebrate(/*...same as above*/), ProductController.create);
LucasRobs commented 3 years ago

it worked dear friend, thank you very much for your help :construction_worker:

saymondamasio commented 3 years ago

but particularly I would like to validate first before uploading so the images would not be burned to disk for no reason