首页 > 解决方案 > 在验证完全有效的对象时,我不断收到 ($__ is not allowed) 错误

问题描述

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

Joi版本使用的是14.3.1,node版本是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
    }

}));

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

标签: node.jsexpresserror-handlingjoi

解决方案


一旦您通过 mongoose 验证模式,他们就无需通过 Joi 验证。如果您删除 Joi 验证,则错误将消失。


推荐阅读