codecoolture / next-joi

Validate NEXT.js API Routes with joi
MIT License
112 stars 5 forks source link

Is Joi.default() supported? #10

Open enquestor opened 2 years ago

enquestor commented 2 years ago

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.

sergioalvz commented 2 years ago

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 :-)