next-joi icon indicating copy to clipboard operation
next-joi copied to clipboard

Is Joi.default() supported?

Open enquestor opened this issue 4 years ago • 1 comments

I am trying to add a default value to an optional parameter, but Joi.default() doesn't seem to take effect. My code looks somehow like this:

const params = Joi.object({
  language: Joi.string().default('en-us')
})

export default validate({ body: params }, async (req, res) => {
  console.log(req.body.language) // outputs undefined if not passed
})

I'm guessing either I shouldn't access body with req.body or this is not supported, is it? Thanks in advance.

enquestor avatar Dec 24 '21 11:12 enquestor

Hi, @Allen-Hu!

Currently, any operation that modifies the request's body (or query, headers) is not supported. We use joi to perform validation only. Anyway, I think you raise a valid point here, and I will take a look to try adding support for this anytime soon.

I think this may be somehow related to #9 too.

And yes, you are doing the right thing accessing req.body from your handler :-)

sergioalvz avatar Dec 26 '21 19:12 sergioalvz