arb / celebrate

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

Celebrate V13 not able to validate params data which is in array. #207

Closed airrakeshkumarsharma closed 3 years ago

airrakeshkumarsharma commented 3 years ago

My joi schema looks like this:


Joi.object().keys({
  // Filters
  filters: Joi.array()
    .items({
      key: Joi.string().required(),
      type: Joi.string(),
      value: [Joi.string().trim(), Joi.array(), Joi.boolean()],
      upperValue: [Joi.number(), Joi.string().isoDate().allow(null)],
      lowerValue: [Joi.number(), Joi.string().isoDate().allow(null)],
      operation: Joi.string()
    })
    .default([]),

  // Sort
  sort: Joi.array()
    .items({
      key: Joi.string().required(),
      order: Joi.number().required()
    })
    .default([]),

  // Pagination
  perPage: Joi.number().min(1).default(CONSTANTS.PAGINATION.PER_PAGE),
  pageNumber: Joi.number().min(1).default(1)
});

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

?filters=[{"key":"userType","value":"STUDENT"}, {"type":"hash","key":"email","value":"abhishek.k.vghgghj@gmail.com"}]

The issue I am having with celebrate is:

When I go to version 10 then celebrate is working fine and it is able to validate the params but when I just shift to v13 then it is not able to validate

arb commented 3 years ago

What else changes? Are you using the bundled version of Joi that celebrate exports or are you using one from package.json?

What do you mean "not able to validate"? In what way?

Finally, you'll get better and faster responses if your issues are formatted correctly.

airrakeshkumarsharma commented 3 years ago

Yes, I am using the bundled version which comes with celebrate. And thanks for your comments.

arb commented 3 years ago

Are you still having a problem or can this issue be closed?

Marsup commented 3 years ago

This is very likely due to your joi upgrade, it was described as a breaking change in joi@16, see https://github.com/sideway/joi/issues/2037 section Array and object string coercion. I'm not sure how celebrate handles joi extensions.

arb commented 3 years ago

Have you tried setting convert: true in the joi options? Also, you still haven't explained precisely HOW it is not able to validate. What error are you getting? What data are you passing? What does req.query look like before you try to validate it. I can't really help because your responses do not include enough information.

airrakeshkumarsharma commented 3 years ago

Yes, I set the { convert: true } but this also does not work.

Here is my input filters=[{"key":"userType","value":"STUDENT"}, {"type":"hash","key":"email","value":"abhishek.k.v83408@gmail.com"}]

Output error ` details: Map(1) { 'query' => [Error [ValidationError]: "filters" must be an array] { _original: [Object], details: [Array] } },

} `

arb commented 3 years ago

Log out req.query in some middleware before calling celebrate, let's see how the query string is actually being parsed.

Marsup commented 3 years ago

I already explained the problem, you just have to find a way to apply the solution with celebrate. Looks like there's a test that demonstrates how to extend, it's just not documented.