【问题标题】:Error after upgrading Joi to latest version - Schema can only contain plain objects (name)将 Joi 升级到最新版本后出错 - 架构只能包含普通对象(名称)
【发布时间】:2020-07-09 14:48:06
【问题描述】:

将 Joi 升级到最新版本 @hapi/Joi(17.1.1) 后,我的服务器没有启动,我在启动时遇到以下错误。最近的版本似乎有一些重大变化。还没有得到任何线索,感谢任何帮助。

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'
}

【问题讨论】:

  • 我可以看看你的架构代码和模型导出吗?

标签: node.js joi hapi


【解决方案1】:

对于再次发现自己处于这个黑暗地方的任何人,对我来说,使用扩展运算符扩展 Joi 模式是一个问题:

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

当我升级到 Joi 17.5.0 时,这导致了“架构只能包含普通对象”错误。使用 keys() 语法绕过它:

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

【讨论】:

    【解决方案2】:

    感谢大家的 cmets。在浏览了 Joi 的发行说明后,我能够解决它。

    问题是由于 Joi 版本的混合造成的。在我的代码库中使用了旧版本的 Joi,这导致了这种情况。下面的 Github issue 帮助我意识到了这个问题。

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

    【讨论】:

      【解决方案3】:

      不看代码很难判断。
      但是我在使用Joi.extend() 时遇到了同样的问题。

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

      问题是我错误地使用了extendedType,如下所示:

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

      代替:

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

      【讨论】:

        【解决方案4】:

        我认为你应该做这样的事情来处理这个错误:

            var ObjectJoi = Joi.object({
            //some item that you want to add
        
        })
        
        var mySchema = new Schema(Joigoose.convert(ObjectJoi))
        const myModel = mongoose.model('myModel', mySchema);
        

        【讨论】:

          猜你喜欢
          • 2017-06-21
          • 2021-11-26
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          • 2022-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多