7

After upgrading Joi to the latest version @hapi/Joi(17.1.1) my server is not staring I am getting below error on startup. Seems there were some breaking changes in recent versions. Not able to get any clue yet, any help is appreciated.

Error: Schema can only contain plain objects (name)
    at new module.exports (/Users/xyz/project/projectxyz/node_modules/@hapi/hoek/lib/error.js:23:19)
    at module.exports (/Users/xyz/project/projectxyz/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:88:5)
    at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:17:26)
    at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/base.js:631:24)
    at /Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/types/keys.js:255:92
    at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/common.js:173:16)
    at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/types/keys.js:255:32)
    at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:90:25)
    at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:17:26)
    at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:117:24)
    at Object.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/index.js:123:24)
    at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/validation.js:49:22)
    at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/route.js:197:43)
    at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/route.js:122:14)
    at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/server.js:498:23)
    at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/server.js:491:22)
    at /Users/xyz/project/projectxyz/src/app.js:73:14
    at Array.forEach (<anonymous>)
    at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
  path: 'name'
} Server Init error Error: Schema can only contain plain objects (name)
    at new module.exports (/Users/xyz/project/projectxyz/node_modules/@hapi/hoek/lib/error.js:23:19)
    at module.exports (/Users/xyz/project/projectxyz/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:88:5)
    at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:17:26)
    at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/base.js:631:24)
    at /Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/types/keys.js:255:92
    at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/common.js:173:16)
    at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/types/keys.js:255:32)
    at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:90:25)
    at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:17:26)
    at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/compile.js:117:24)
    at Object.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/joi/lib/index.js:123:24)
    at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/validation.js:49:22)
    at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/route.js:197:43)
    at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/route.js:122:14)
    at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/server.js:498:23)
    at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/@hapi/hapi/lib/server.js:491:22)
    at /Users/xyz/project/projectxyz/src/app.js:73:14
    at Array.forEach (<anonymous>)
    at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
  path: 'name'
}
amyst
  • 616
  • 1
  • 10
  • 22

4 Answers4

3

To anyone who finds themselves in this dark place again, for me it was an issue when extending a Joi schema using the spread operator:

joi.object().keys({
   ...otherSchema,
   position: joi.number()
})

This was causing the "Schema can only contain plain objects" error when I upgraded to Joi 17.5.0. Using the keys() syntax got around it:

otherSchema.keys({
   position: joi.number()
})
Rory Donohue
  • 418
  • 2
  • 9
1

Thanks all for your comments. I was able to resolve it after going through Joi's release notes.

The problem was due to the mix of Joi versions. In my codebase older version of Joi was used which was causing this. Below Github issue helped me realizing the issue.

https://github.com/hapijs/joi/issues/1913

amyst
  • 616
  • 1
  • 10
  • 22
0

i think u should do some thing like this to handle this error:

    var ObjectJoi = Joi.object({
    //some item that you want to add

})

var mySchema = new Schema(Joigoose.convert(ObjectJoi))
const myModel = mongoose.model('myModel', mySchema);
0

It's hard to tell without seeing the code.
But I faced the same issue when using Joi.extend().

const extendedType = Joi.extend(Joi => {
  return {
    type: 'myType', // <- PAY ATTENTION
    base: Joi.array(),
    messages: {
      'stringArray:base': '...'
    },
    validate(value, helpers) {
      ...
    },
    coerce(value, helpers) {
      ...
    }
  }
});

The problem was that I used extendedType like below by mistake:

Joi.object({
  someKey: extendedType,
  ...
});

instead of:

Joi.object({
  someKey: extendedType.myType(),
  ...
});
Amirhosein Al
  • 470
  • 6
  • 18