arb / celebrate

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

Celebrate on error shows an HTML page with only object Object printed on console #120

Closed abhishekp1996 closed 5 years ago

abhishekp1996 commented 5 years ago

My joi schema looks like this:

{email: Joi.string().trim().email()}

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

a

The issue I am having with celebrate is:

The issue is that whenever I hit the API, the validation kicks in and if the validation is failed, a HTML page is returned with nothing but object Object in the body and also in console the same thing is printed.

But when the validation format are correct, everything is working good.

What could be wrong?

arb commented 5 years ago

Are you using errors()? If so, yes, the response when a celebrate errors happens is going to be a JSON object which explains why you're seeing object Object. It is up to the consumer (you in this case) to display an HTML page formatted as you'd like if that's what you want.

If you're not using errors(), then you're likely seeing the default Express error handler and again, it responds with HTML rather than JSON if I recall correctly.

abhishekp1996 commented 5 years ago

@arb I was not using errors() and Express was picking up their default error handler. Actually there was error handler in place, but I was using only 3 parameters for error handler - err, request, response. When I added next the code started working correctly. Thank you! I am celebrating