I using Node.js API's, I also use Joi package to validate my entries , but I keep getting error ($__ is not allowed) while the form I submit seems to be perfectly valid. I have searched a lot but couldn't find any useful solution.
Joi version is use is 14.3.1, and the node version is 14.3.1
here is the schema :
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) ;
}
The form data:
{
"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"
}
the model :
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
}
}));
Any ideas will be appreciated. Thanks
I suppose item = new Item(formData);
which makes the item a Mongoose object.
If you do this:
Joi.validate(item._doc, schema);
then "$__" is not allowed
error is not observed. This looks a bit hacky, though. If I were you, I'd probably use Mongoose to validate before saving to database and perhaps use Joi just to validate user input.