arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

Celebrate don't recognize my BODY requisition variables with a HEADER validation #170

Closed KZTN closed 4 years ago

KZTN commented 4 years ago

I'm using Celebrate in my router.js file, every single route with 1 type of requisition is working, but i'm having issues with 2 types of requisitions. In this case bellow, my headers ckeckout is working normally, but Celebrate didn't recognize the second param BODY. Specifically the variables

routes.post(
    '/incidents',
    celebrate({
      [Segments.HEADERS]: Joi.object().keys({
        ong_id: Joi.string().length(8).required()
      }).unknown(),
      [Segments.BODY]: Joi.object({
        title: Joi.string().required(),
        description: Joi.string().required(),
        value: Joi.number().required()
      }),
    }),
    IncidentController.create
  )

Result in Insomnia:

Screenshot from 2020-03-28 11-58-37

fmmeireles commented 4 years ago

HEADERS does not contain "ong_id", but authorization. Remember that the request for headers must be performed within Joi.object() and not within .keys() Try this:

routes.post('/incidents', celebrate({
    [Segments.HEADERS]: Joi.object({
        authorization: Joi.string().required(),
    }).unknown(),
    [Segments.BODY]: Joi.object().keys({
        title: Joi.string().required().max(35),
        description: Joi.string().required().max(350),
        value: Joi.number().required(),
    }),
}), IncidentController.create)
KZTN commented 4 years ago

HEADERS does not contain "ong_id", but authorization. Remember that the request for headers must be performed within Joi.object() and not within .keys() Try this:

routes.post('/incidents', celebrate({
    [Segments.HEADERS]: Joi.object({
        authorization: Joi.string().required(),
    }).unknown(),
    [Segments.BODY]: Joi.object().keys({
        title: Joi.string().required().max(35),
        description: Joi.string().required().max(350),
        value: Joi.number().required(),
    }),
}), IncidentController.create)

HI, thanks in advance. As said, it's ok about my HEADERS param. I'm just stuck with the BODY part. Celebrate just can't recognize my body params in this mode.

arb commented 4 years ago

Are you using body-parser in your Express server? What do you get with this:

routes.post('/incidents', (req, res, next) => console.log(req.body); next())

My guess is req.body is either undefined or a string.

KZTN commented 4 years ago

Are you using body-parser in your Express server? What do you get with this:

routes.post('/incidents', (req, res, next) => console.log(req.body); next())

My guess is req.body is either undefined or a string.

The main reason that express didn't recognized the body parser was the lack of content-type on requisition. This clue gave me a light tho. Thanks for all ❤️