[Fixed] UnhandledPromiseRejection: Error: Invalid message options with Joi


I am getting Invalid error messages. I have tried many things but nothing is working. The Joi docs state that Joi.validate is deprecated and schema.validate should be used but that is not working for me either.Postman just hangs until I have to cancel the request manually. Below is the code when making a post request to create a user:

const { registervalidation } = require('../validation');

router.post('/register', async (req, res) => {

 const validation = registervalidation(req.body);
 if(error) return res.status(400).send(error.details);
const users = new User ({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password 

    const usersave = await users.save();

The joi schema looks like these..

const Joi = require('joi');

const registervalidation = data =>{

const schema = Joi.object({
    name:     Joi.string()
                 .error(new Error('I am a custom error and I know it!')),

   email:     Joi.string()
                 .email({minDomainSegments: 2, tlds: { allow: ['com', 'net'] } })
                 .error(new Error('I am a custom error and I know it!')),

   password:  Joi.string()
                 .error(new Error('I am a custom error and I know it!'))
return schema.validate(data, schema);   


It throws error..

(node:17752) UnhandledPromiseRejectionWarning: Error: Invalid message options
at new module.exports (D:\Projects\web\jwt\node_modules\@hapi\hoek\lib\error.js:23:19)
at module.exports (D:\Projects\web\jwt\node_modules\@hapi\hoek\lib\assert.js:20:11)
at Object.exports.compile (D:\Projects\web\jwt\node_modules\joi\lib\messages.js:30:5)
at Object.exports.preferences (D:\Projects\web\jwt\node_modules\joi\lib\common.js:165:36)
at Object.exports.entry (D:\Projects\web\jwt\node_modules\joi\lib\validator.js:24:27)
at internals.Base.validate (D:\Projects\web\jwt\node_modules\joi\lib\base.js:548:26)
at registervalidation (D:\Projects\web\jwt\validation.js:23:19)
at D:\Projects\web\jwt\routes\auth.js:9:25
at Layer.handle [as handle_request] (D:\Projects\web\jwt\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Projects\web\jwt\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Projects\web\jwt\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Projects\web\jwt\node_modules\express\lib\router\layer.js:95:5)
at D:\Projects\web\jwt\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:335:12)
at next (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\Projects\web\jwt\node_modules\express\lib\router\index.js:174:3)
 (node:17752) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
 This error originated either by throwing inside of an async function without 
 a catch block, or by rejecting lock, or by rejecting a promise which was not 
handled with .catch(). To terminate the node process on unhandled promise 
rejection, use the CLI flag `--unhandled-r 
 https://nodejs.org/apejections=strict` (see 
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection 
id: 1)
(node:17752) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
 deprecated. In the future, promise rejections that are not handled will 
 the Node.js process with a
 Node.js process with a non-zero exit code.


You have several errors in your code:

— You are not validating your schema correctly:

schema.validate(data, schema);

it should be:


check out .validate documentation for more information

— This it not how you should catch the validation error:

const validation = registervalidation(req.body);
if(error) return res.status(400).send(error.details);

error doesn’t even exist, it should be

if(validation.error) {

— Your try should be placed at the top of your function:

router.post('/register', async (req, res) => {
  try {
  } catch (err) {


— And, I am not sure if you are exporting your registervalidation correctly, but it should be:

module.exports = { registervalidation }

Leave a Reply

(*) Required, Your email will not be published