我使用Node.js API,也使用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);
这使该项目成为猫鼬对象。如果您这样做:
Joi.validate(item._doc, schema);
则"$__" is not allowed
未观察到错误。不过,这看起来有点骇人听闻。如果我是您,那么在保存到数据库之前,我可能会使用Mongoose进行验证,或者可能仅使用Joi来验证用户输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句