在验证一个完全有效的对象时,我不断收到 ($__ is not allowed) 错误

I keep getting ($__ is not allowed) error while validating a perfectly valid object

我使用 Node.js API's,我也使用 Joi 包来验证我的条目,但我不断收到错误消息($__ 是不允许的),而我提交的表格似乎是完全有效。 我搜索了很多但找不到任何有用的解决方案。

Joi使用的版本是14.3.1, 节点版本为 14.3.1

这是架构:

const schema = Joi.object({
    name: Joi.string().min(5).max(50).required(),
    price : Joi.object().required().allow(),
    description : Joi.string().min(15).max(400).required().allow(),
    isFavorate : Joi.boolean().required().allow(),
    place: Joi.object().required().allow(),
    tags : Joi.array().required().allow(),
    isDeliveryAvailable : Joi.boolean().required().allow(),
    rating: Joi.number().min(0).required().allow(),
    images : Joi.required().allow(),
    secondHand : Joi.required().allow()
})
    return Joi.validate(item , schema) ;
}

表单数据:

{
    "name" : "an image",
    "price" : {
        "IQD" : 1000,
        "USD" : 10
    },
    "place" : {
        "name" : "fata rap"
    },
    "description" : "some item safas dfsa d",
    "isFavorate" : true,
    "tags": ["some tag 1", "tag 2"],
    "isDeliveryAvailable": true,
    "rating" : 4,
    "comments":["comment 1" , "comment 2"],
    "variants": [
        {"name": "item one variant one", "price":{
            "IQD":1000,
            "USD": 1}
        },
            {"name": "item one variant tow", "price":{
            "IQD":2000,
            "USD": 2}
        },
            {"name": "item one variant three", "price":{
            "IQD":1000,
            "USD": 1}
        }
        ],
    "category" : "5d479c60e35db51e3084c007",
    "secondHand" : "true"


}

模特:

const Item = mongoose.model('Items',new  mongoose.Schema({
    name: {
        type : String,
        required: true,
        minlength : 5,
        maxlength : 50
    },

    price : {
        type: Object,
        IQD: {type : Number, required : true, min : 0},
        USD:  {type : Number, required : true, min : 0 }
    },

    description : {
        type : String,
        minlength : 15,
        maxlength : 400,
        required : true
    },

    isFavorate: {
        type: Boolean,
        default: false,
        required : true
    },

    place : {
      name : {
        type : Object,
        required : true,
      }
    },

    tags : {
        type : [String],
        required : true,
    },

    isDeliveryAvailable : {
        type : Boolean,
        default: true,
        required: true
    },

    rating: {
        type: Number,
        min : 0,
        max : 10,
        required : true
    },

    comments : {
        type : [String],
    },

    images: {
        type: [Buffer],
        requied : true
    },

    imageURLArray : [String],

    variants: [Object],

    category: {
        type : mongoose.Schema.Types.ObjectId,
        ref: 'Category'
    },

    subcategory: {
        type : mongoose.Schema.Types.ObjectId,
        ref: 'Subcategory'
    },

    secondHand : {
        type : Boolean,
        required : true,
        default : false
    }

}));

任何想法将不胜感激。谢谢

我想 item = new Item(formData); 使该项目成为 Mongoose 对象。 如果你这样做:

Joi.validate(item._doc, schema);

然后 "$__" is not allowed 没有观察到错误。不过,这看起来有点老套。如果我是你,我可能会在保存到数据库之前使用 Mongoose 进行验证,也许只使用 Joi 来验证用户输入。

一旦您通过 mongoose 验证了架构,就无需通过 Joi 进行验证。如果您删除 Joi 验证,那么错误将消失。