hapi-default-payload
v3.0.0
Published
Hapi plugin to default the request payload to an empty object
Downloads
3,561
Maintainers
Keywords
Readme
hapi-default-payload
A tiny plugin to default the request.payload
to an empty object (similar to the pre Hapi 9+ behavior).
This can be useful if you want more descriptive Joi error messages in response to requests that are missing payload data from the request body.
For example, the error message for the schema...
Joi.object.keys({
a: Joi.number().required(),
b: Joi.string().required()
})
can become more detailed with this plugin...
ValidationError: child "a" fails because ["a" is required]. child "b" fails because ["b" is required]
as opposed to the technically accurate but more abrupt and less helpful...
ValidationError: "value" must be an object
Registering the plugin
const hapi = require('@hapi/hapi');
const server = new hapi.Server({});
server.register([
require('hapi-default-payload')
]);