Reputation: 32716
Is there a way to get status 422 instead of 400 in https://github.com/hapijs/joi
after the reply of @Matt Harrison
to apply the logic globally you can in your index.js
server.ext('onPreResponse', function (request, reply) {
var req = request.response;
if (req.isBoom && (req.output.statusCode===400)) {
return reply(Boom.badData(req.output.payload.message));
}
return reply.continue();
});
Upvotes: 4
Views: 1710
Reputation: 13567
Joi itself doesn't know or care about HTTP or status codes. It's just for validating JavaScript values.
But I'm guessing you're using Joi with hapi. In this case it's hapi that's giving you a 400. You can override this by using the failAction
property in the validation config.
You can also use Boom to create an HTTP-friendly 422 error.
var Boom = require('boom');
server.route({
config: {
validate: {
...
failAction: function (request, reply, source, error) {
reply(Boom.badData('Bad data', error.data));
}
}
},
method: 'GET',
path: '/',
handler: function (request, reply) {
...
}
});
Upvotes: 3