【发布时间】:2015-05-19 19:44:17
【问题描述】:
我有以下猫鼬模式
var Mongoose = require("mongoose");
var Schema = Mongoose.Schema;
var headerSchema = new Schema({
lang : {
type: String,
required: true,
match: [ /^(en|es|pt)$/, "{VALUE} is not a supported language" ],
index: { unique: true }
},
menu : {
type: [{
link: { type: Schema.ObjectId, ref: "Link"},
dropdown: { type: Schema.ObjectId, ref: "Dropdown"},
autopopulate: true
}]
}
});
headerSchema.plugin(require("mongoose-autopopulate"));
module.exports = Mongoose.model("header", headerSchema);
您可能已经猜到了,这描述了网页标题的配置文档。此标头有一个导航(架构中的菜单),其中每个项目可以是下拉列表或链接,但不能同时是两者。有没有办法将自定义验证添加到我的架构中,以仅在设置了其中任何一个时才允许保存文档,但不能同时设置,也不能两者都设置? (想想布尔运算 XOR)
例如,这是一个有效的头文件:
{
lang: "es",
menu: [{
link: {
title: "Contact",
href: "/contact"
}
}, {
dropdown: {
title: "FAQ",
links: [{
title: "What is this?",
href : "/about"
}]
}
}]
}
【问题讨论】: