Mongoose expand default validation

后端 未结 4 534
天涯浪人
天涯浪人 2021-01-06 17:49

I want to build \"minLength\" and \"maxLength\" in the mongoose schema validation rules, the current solution is:

var blogSchema = new Schema({
  title: { re         


        
4条回答
  •  难免孤独
    2021-01-06 17:59

    Check out the library mongoose-validator. It integrates the node-validator library for use within mongoose schemas in a very similar way to which you have described.

    Specifically, the node-validator len or min and max methods should provide the logic you require.

    Try :

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

提交回复
热议问题