[英]Reference field from within the same schema
我有一個架構
const mySchema = new Schema({
limit: Number,
currentlyAdded: Number
});
我希望currentlyAdded
最多為limit
。
max
似乎不接受函數,所以我不能做function() { return this.limit }
有沒有辦法以某種方式從currentlyAdded
引用limit
字段,所以我可以做類似的事情
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
max: %reference_to_limit%
}
});
您可以使用validate
屬性通過調用函數來驗證限制條件,
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
validate: [checkLimit, "Exceeded the limit!"]
}
});
// CHECK VALIDATION
function checkLimit(currentlyAdded) {
return currentlyAdded <= this.limit;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.