Javascript Mongoose扩展默认验证

Javascript Mongoose扩展默认验证,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,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

我想在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'));
});
我如何才能做到这一点,这可能吗?

查看图书馆。它集成了节点验证程序库,以便在mongoose模式中使用,方法与您描述的非常类似

具体而言,lenminmax方法应提供所需的逻辑

尝试:


我有同样的功能要求。不知道为什么mongoose不为字符串类型提供最小/最大值。您可以扩展mongoose的字符串模式类型(我刚刚从数字模式类型复制了min/max函数,并将其调整为字符串,这对我的项目来说很好)。确保在创建架构/模型之前调用修补程序:

var mongoose = require('mongoose');
var SchemaString = mongoose.SchemaTypes.String;

SchemaString.prototype.min = function (value) {
  if (this.minValidator) {
    this.validators = this.validators.filter(function(v){
      return v[1] != 'min';
    });
  }
  if (value != null) {
    this.validators.push([this.minValidator = function(v) {
      if ('undefined' !== typeof v)
        return v.length >= value;
    }, 'min']);
  }
  return this;
};

SchemaString.prototype.max = function (value) {
  if (this.maxValidator) {
    this.validators = this.validators.filter(function(v){
      return v[1] != 'max';
    });
  }
  if (value != null) {
    this.validators.push([this.maxValidator = function(v) {
      if ('undefined' !== typeof v)
        return v.length <= value;
    }, 'max']);
  }
  return this;
};
var mongoose=require('mongoose');
var schematstring=mongoose.SchemaTypes.String;
schematring.prototype.min=函数(值){
if(此.minValidator){
this.validators=this.validators.filter(函数(v){
返回v[1]!='min';
});
}
if(值!=null){
this.validators.push([this.minValidator=function(v){
如果('undefined'!==typeof v)
返回v.length>=值;
},‘min’);
}
归还这个;
};
schematring.prototype.max=函数(值){
if(此.maxValidator){
this.validators=this.validators.filter(函数(v){
返回v[1]!='max';
});
}
if(值!=null){
this.validators.push([this.maxValidator=function(v){
如果('undefined'!==typeof v)

返回v.lengthmaxlength和minlength现在已经存在

    var mongoose = require('mongoose');
    var blogSchema = new mongoose.Schema({
        title: {
             type: String,
             required: true,
             minLength: 8,
             maxLength: 32
        }
    });

最小值和最大值已更改

var breakfastSchema = new Schema({
  eggs: {
    type: Number,
    min: [6, 'Too few eggs'],
    max: 12
  },
  bacon: {
    type: Number,
    required: [true, 'Why no bacon?']
  },
  drink: {
    type: String,
    enum: ['Coffee', 'Tea'],
    required: function() {
      return this.bacon > 3;
    }
  }
});

    var mongoose = require('mongoose');
    var blogSchema = new mongoose.Schema({
        title: {
             type: String,
             required: true,
             minLength: 8,
             maxLength: 32
        }
    });
var breakfastSchema = new Schema({
  eggs: {
    type: Number,
    min: [6, 'Too few eggs'],
    max: 12
  },
  bacon: {
    type: Number,
    required: [true, 'Why no bacon?']
  },
  drink: {
    type: String,
    enum: ['Coffee', 'Tea'],
    required: function() {
      return this.bacon > 3;
    }
  }
});