问题描述
我最近开始学习快递,并在烹饪博客上工作。我正在使用mongodb和mongoose。 我的食谱模式是这样的-
const RecipeSchema = new Schema({
title: { type: String,required: true,maxlength: 50 },chef: { type: Schema.Types.ObjectId,ref: "User",required: true },posted_date: { type: Date,updated_date: { type: Date },body: { type: String,minlength: 10 },heading: { type: String,comments: [{ type: CommentSchema,default: [] }],likes: [{ type: Schema.Types.ObjectId,labels: [{ type: Schema.Types.ObjectId,ref: "Label",images: [{ type: String,default: null }],});
我已经使用用于创建配方(POST请求)端点的express-validator编写了配方验证-
body("title")
.trim()
.isLength({ min: 2,max: 50 })
.withMessage(
"Title should be min 2 characters and max 50 characters long"
),body("chef")
.trim()
.custom((value) => {
return User.findById(value)
.then((user) => {
if (!user) {
return Promise.reject("Only registered users can upload recipes");
}
})
.catch((err) => {
throw new Error("Id is not proper");
});
}),body("body")
.trim()
.isLength({ min: 10 })
.withMessage("Recipe should be atleast 10 characters long"),body("heading").trim().optional({ checkFalsy: true }),body("images")
.isArray()
.custom((array) => {
if (!array.every(checkValidURL)) {
throw new Error("Image url is not valid");
}
return true;
}),body("images.*").trim(),body("labels.*","Invalid Label").trim().isLength({ min: 1 }).escape(),
是否可以将这些验证重用于更新配方端点(PUT请求)?如果我们可以重用,那么如果更新请求仅包含要更改的配方属性(例如,“-{“ title”:“ new_title”}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)