How to specify HTTP error 422 instead of 400 in Hapi Joi
Is there a way to get status 422 instead of 400 at https://github.com/hapijs/joi
UPDATE
following @Matt Harrison answer
apply logic globally to 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();
});
+3
source to share
1 answer
Joi itself doesn't know or care about HTTP or status codes. It's just for validating JavaScript values.
But I am assuming you are using Joi with hapi. In this case it is hapi, which gives you 400. You can override this using the property failAction
in the validation config .
You can also use Boom to generate a 422 error with HTTP support.
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) {
...
}
});
+3
source to share