Search code examples
node.jshapi.jsjoi

ValidationError: "value" must be an object - ResponseValidation phase


Using hapi server, I am returning from handler like this:

async function handler (req, h) {
  ...
  h.unstate(key, cookieOptions);
  const resp = h.redirect(redirectUri);
  return resp;
}

... but getting back HTTP 500 error. I found the failure in the stack trace:

ValidationError: "value" must be an object
    at Object.exports.process (~/project/node_modules/hapi/node_modules/joi/lib/errors.js:201:19)
    at internals.Object._validateWithOptions (~/project/node_modules/hapi/node_modules/joi/lib/types/any/index.js:751:31)
    at module.exports.internals.Any.root.validate (~/project/node_modules/hapi/node_modules/joi/lib/index.js:146:23)
    at exports.response (~/project/node_modules/hapi/lib/validation.js:173:31)
    at Request._postCycle (~/project/node_modules/hapi/lib/request.js:356:68)
    at Request._reply (~/project/node_modules/hapi/lib/request.js:335:20)
    at Request._execute (~/project/node_modules/hapi/lib/request.js:171:14)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

...which seem to come from Response validation lifecycle step in between onPostHandler and onPreResponse.

What could be the issue?


Solution

  • The failure is coming from uninitialized response on redirect, creating the empty response first should fixed it:

    async function handler (req, h) {
      ...
      const resp = h.response().redirect(redirectUri);
      return resp;
    }