arb / celebrate

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

Error details are empty in error message #224

Closed TheRealThor closed 3 years ago

TheRealThor commented 3 years ago

I am using this mini express setup


const express = require('express')
const { errors, celebrate, isCelebrateError, Segments, Joi } = require('celebrate');

const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.use('/test', celebrate({
  [Segments.QUERY]: {
    test_parameter: Joi.string().required()
  }
}), (req, res, next) => {

   return res.status(200).send();

});

app.use(errors());

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

the error thrown is when no test_parameter is provided is:

Error: Validation failed
    at /Users/thor/Dev/www/AF/celebr/node_modules/celebrate/lib/celebrate.js:95:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

How can I find out more about why the validation failed (for example because test_parameter is mandatory)? The "details" key in the error object is empty, which I found out when I used an error handler and had those details printed to console.log.

When I downgrade to celebrate version 13.04 , a detailed error message is provided ("message":"\"test_parameter\" is required)

arb commented 3 years ago

With your example, when I go to http://localhost:3000/test here is what I get as a result...

{"statusCode":400,"error":"Bad Request","message":"Validation failed","validation":{"query":{"source":"query","keys":["test_parameter"],"message":"\"test_parameter\" is required"}}}
arghyac35 commented 3 years ago

I am also facing the same problem. This issue started happening after upgrading to v15

arghyac35 commented 3 years ago

I see what went wrong. I was not using the error handler from celebrate. So, the error details were coming to the details Map which was not handled by me. Now, it is fixed.

TheRealThor commented 3 years ago

Could you share a code example please ?

arghyac35 commented 3 years ago

I use a custom error handler in my application, so I handled the celebrate error on it by checking the err object with https://github.com/arb/celebrate#iscelebrateerrorerr and generated a single message from the details Map of the CelebrateError object. Below is an example error handler of the said implementation: `app.use((err, req, res, next) => { if (isCelebrateError(err)) { let message = ''; for (let value of err.details.values()) { message += value.message + '; '; } res.status(400).json({ message }); }

// Other error handler code...

});`

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.