【问题标题】:Mongoose expand default validationMongoose 展开默认验证
【发布时间】:2012-12-11 02:15:58
【问题描述】:

我想在猫鼬模式验证规则中构建“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'));
});

但是我认为这应该通过添加自定义架构规则来简化,如下所示:

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

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

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    查看库mongoose-validator。它以与您描述的非常相似的方式集成了节点验证器库,以便在 mongoose 模式中使用。

    具体来说,node-validator lenminma​​x 方法应该提供您需要的逻辑。

    试试:

    var validate = require('mongoose-validator').validate;
    
    var blogSchema = new Schema({
     title: {
        type: String,
        required: true,
        validate: validate('len', 8, 32)
     }
    });
    

    【讨论】:

      【解决方案2】:

      现在存在最大长度和最小长度。您的代码应该如下工作。

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

      【讨论】:

        【解决方案3】:

        我有同样的功能要求。不知道,为什么 mongoose 不为 String 类型提供最小值/最大值。您可以扩展 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;
        };
        

        PS:由于这个补丁使用了一些 mongoose 的内部变量,你应该为你的模型编写单元测试,以便在补丁被破坏时注意到。

        【讨论】:

          【解决方案4】:

          最小值和最大值已更改

          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;
              }
            }
          });
          

          https://mongoosejs.com/docs/validation.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-07-27
            • 2021-03-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-12
            • 1970-01-01
            相关资源
            最近更新 更多