Hapi plugin to default the request payload to an empty object
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.eslintignore
.eslintrc
.gitignore
.node-version
.travis.yml
CHANGELOG.md
LICENSE
README.md
package.json

README.md

hapi-default-payload npm version Build Status

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

var Hapi = require('hapi');

var server = new Hapi.Server();

server.register([
  require('hapi-default-payload')
], function (err) {
  // Insert your preferred error handling here...
});