npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-validatefilter

v0.0.18

Published

mongoose 数据验证与过滤

Readme

mongoose 数据验证与过滤

mongoose-validatefilter

相关文章

http://qianduanblog.com/3314.html

安装

npm install mongoose-validatefilter

API及说明

  • 引用:var mongooseValidateFilter = require('mongoose-validatefilter');
  • 实例化validatevar validate = new mongooseValidateFilter.validate();
  • 实例化filtervar filter = new mongooseValidateFilter.filter();
  • validate只有一个公开方法:validate.add(字段, 规则)
  • filter只有一个公开方法:validate.add(字段, 规则)
  • validatefilter都是按顺序来验证和过滤的。
  • mongooseValidateFilter的初始化:mongooseValidateFilter.validateFilter(Schema, validate, filter);

较完整例子

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var mongooseValidateFilter = require('../index.js');
var validate = new mongooseValidateFilter.validate();
var filter = new mongooseValidateFilter.filter();

// =============================== email =================================
validate.add('email', {
    required: true,
    msg: '邮箱不能为空!',
});

validate.add('email', {
    type: 'string',
    msg: '邮箱必须为字符串格式!',
});

validate.add('email', {
    type: 'email',
    msg: '邮箱格式不正确!',
});

validate.add('email', {
    maxLength: 120,
    msg: '邮箱长度不能超过120个字符!',
});

validate.add('email', {
    callback: function(value, respond) {
        value = String(value).trim().toLowerCase();
        // 这里有3个判断值,分别为:
        // __isCreate: 执行 cretaOne 时为真
        // __isUpdate: 执行 updateOne 时为真
        // __isRemove: 执行 removeOne 时为真
        if (this.__isCreate) {
            this.model('User').findOne({
                email: value,
            }, function(e, doc) {
                if (e) return respond(e);
                respond(!doc);
            });
        } else {
            respond(true);
        }
    },
    msg: '邮箱重复!',
});

filter.add('email', 'lowercase');



// =============================== nickname =================================
validate.add('nickname', {
    exist: true,
    type: 'string',
    msg: '昵称必须为字符串格式!',
});

validate.add('nickname', {
    exist: true,
    trim: true,
    minLength: 2,
    msg: '昵称长度至少为2个字符',
});

validate.add('nickname', {
    exist: true,
    trim: true,
    maxLength: 12,
    msg: '昵称长度最多为12个字符',
});



filter.add('nickname', function(value, respond) {
    if (value === undefined) value = '     匿名用户' + Date.now()+'      ';
    respond(value);
});
filter.add('nickname', 'trim');





// 数据模型
var Schema = new mongoose.Schema({
    // 邮箱
    email: {
        type: String,
    },
    // 昵称
    nickname: {
        type: String,
    },
});



mongooseValidateFilter.validateFilter(Schema, validate, filter);

var Model = mongoose.model('User', Schema);



// ==========================================================
// ========================= test ===========================
// ==========================================================

// createOne
var createData = {
    email: Date.now() + '@ABC.COM',
};
Model.createOne(createData, function(e, doc) {
    if (e) {
        console.log('错误:');
        console.log(e.message);
    } else {
        console.log('正确。');
        console.log(doc);
    }
});





// updateOne
var updateData = {
    email: '[email protected]',
    nickname: '312312',
};
Model.updateOne({
    _id: '535135df03f2fecc223c2f91',
}, updateData, function(e, doc) {
    if (e) {
        console.log('错误:');
        console.log(e.message);
    } else {
        console.log('正确。');
        console.log(doc);
    }
});


// removeOne
Model.removeOne({
    _id: '535135df03f2fecc223c2f91',
}, function(e, removeLine) {
    if (e) {
        console.log('错误:');
        console.log(e.message);
    } else {
        console.log('正确。');
        console.log('删除了' + removeLine + '个文档');
    }
});