AssertionError [ERR_ASSERTION]:你必须将 Joi 作为参数传递

AssertionError [ERR_ASSERTION]: you must pass Joi as an argument

我如何解决这个 Joi 问题(使用 express,joi@17.4.2,joi-objectid@2.0.0,,猫鼬)我正在尝试 link 两个猫鼬模式使用 ref as

{
...
   enroledcourses: [{
        type: Schema.Types.ObjectId,
        ref: "courses"
    }]
}

joi 验证

...    
enroledcourses: Joi.array().items(Joi.ObjectId()).required()

课程合集不错 我已将其导入 index.js,如下所示

const Joi = require("joi")
Joi.objectId = require("joi-objectid")(Joi);

但我最终得到了这个错误 enter image description here

AssertionError [ERR_ASSERTION]: you must pass Joi as an argument at joiObjectId (D:\Programing projects21\ikodeafrika\ikodeafrikabackend\node_modules\joi-objectid\index.js:6:3) at Object. (D:\Programing projects21\ikodeafrika\ikodeafrikabackend\index.js:2:39)

index.js:2:39 是上面的 joi.objectId 导入加上当我将鼠标悬停在 joi.objectId 导入语句

上时我得到了这个

Property 'objectId' may not exist on type 'Root'. Did you mean 'object'?ts(2568) index.d.ts(1998, 9): 'object' is declared here. any

问题是因为 joi-objectid@2.0.0joi@17.4.2 不兼容。

修复:
joi-objectid 升级到 v4.0.2

关于 Typescript 错误(属性 'objectId' 可能不存在于类型 'Root' 上),您需要扩充 Joi 的类型以添加​​ objectId 属性。这可以通过创建声明文件来完成:

// src/types/joi.d.ts
import { Schema } from "joi";

declare module "joi" {
  interface Root {
    objectId(): Schema;
  }
}