zoukankan      html  css  js  c++  java
  • MongoDB学习day09--Mongoose数据校验

    一、Mongoose检验参数

    required : 表示这个数据必须传入max: 用于 Number 类型数据, 最大值


    min: 用于 Number 类型数据, 最小值


    enum:枚举类型, 要求数据必须满足枚举值 enum: ['0', '1', '2']


    match:增加的数据必须符合 match(正则) 的规则


    maxlength: 最大值


    minlength
    : 最小值

    var UserSchema = new mongoose.Schema({
      name:{
        type:String,
        required: true,
      },age: {
        type: Number,
        // 是否必须的校验器
        required: true,
        // 数字类型的最大值校验器
        max: 120,
        // 数字类型的最小值校验器
        min: 0
      },
      status: {
        type: String,
        // 设置字符串的可选值
        enum: ['0', '1', '2']
      },
      phone:{
        type:Number,
        match: /^d{11}$/
      },
      desc: {
        type: String,
        maxlength:20,
        minlength:10
      }
    });

    二、Mongoose自定义检验器

    validate函数

    var UserSchema = new mongoose.Schema({
      name:{
        type:String,
        required: true,
      },
      age: {
        type: Number,
        // 是否必须的校验器
        required: true,
        // 数字类型的最大值校验器max: 120,
        // 数字类型的最小值校验器
        min: 0
      },
      status: {
        type: String,
        // 设置字符串的可选值
        enum: ['0', '1', '2']
      },
      phone:{
        type:Number,
        match: /^d{11}$/
      },
      desc: {
        type: String,
        // 自定义的验证器, 如果通过验证返回 true, 没有通过则返回 false
        validate: function(desc) {
          return desc.length >= 10;
        }
      }
    });

     

  • 相关阅读:
    length()
    matlab mod()&rem()
    tf调试函数
    64位win7+PCL1.6.0+VS2010,64位win10+PCL1.6.0+VS2010
    pcl 1.8 + VS 2010 在win7 x64下的配置
    Qt在vs2010下的配置
    VS2010 win7 QT4.8.0,实现VS2010编译调试Qt程序,QtCreator静态发布程序
    [POI2012]ROZ-Fibonacci Representation (贪心)
    CF 666C & 牛客 36D
    数位dp练习
  • 原文地址:https://www.cnblogs.com/xc-chejj/p/10933461.html
Copyright © 2011-2022 走看看