Hapi 17+ & Joi Verbose Error Messages

Hapi 17+ & Joi Verbose Error Messages

With Hapi Version 17 they disabled sending verbose Joi messages back to the client. They said in certain cases with Joi returning the user input, it can potentially create XSS attacks. So they disabled it by default. But there is an easy way to re-enable the functionality for testing purposes.

When you create the hapi server you can change how the failAction is handled for validation.

const server = Hapi.server({
  port: 3000,
  host: '0.0.0.0',
  routes: {
    validate: {
      failAction(request, h, err){
        if(!['production', 'staging'].includes(process.env.NODE_ENV)){
          throw err;
        }
        
        logger.error('Validation Error', err);
        throw boom.badRequest(`Invalid request payload input`);
      }
    }
  }
})

Fairly simple.