相关疑难解决方法(0)

Mongoose扩展默认验证

我想在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)

我怎么能这样做,这甚至可能吗?

javascript mongoose mongodb node.js

2
推荐指数
2
解决办法
3792
查看次数

标签 统计

javascript ×1

mongodb ×1

mongoose ×1

node.js ×1