我想在mongoose模式验证规则中构建"minLength"和"maxLength",目前的解决方案是:
var blogSchema = new Schema({
title: { required: true, type: String }
});
blogSchema.path('title').validate(function(value) {
if (value.length < 8 || value.length > 32) return next(new Error('length'));
});
Run Code Online (Sandbox Code Playgroud)
但是我想这应该通过添加自定义架构规则来简化:
var blogSchema = new Schema({
title: {
type: String,
required: true,
minLength: 8,
maxLength: 32
}
});
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,这甚至可能吗?