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

validater-max

v1.0.1

Published

验证restful API接口的参数(Verify the restful API interface parameters)

Readme

validator

验证restful API接口的参数(Verify the restful API interface parameters)

file-signature的参考地址:https://github.com/leahciMic/file-signature
今后的更改与 https://github.com/ruanjiayou/header-helper 保持一致

安装方式

npm install https://github.com/ruanjiayou/validator.git --save

使用案例

// 特别注意:methods中不能用箭头函数,this是指向validater实例的,validater有一些基本的内置方法(isInt,isFloat等)
// express项目,路由中验证参数
const validater = require('validater');
const validation = new validater({
  // lang: 'zh-cn', //设置语言
  rules: {
    id: 'required|int',
    time: 'required|date',
    status: 'required|enum:pending,success,fail',
    IDCard: 'required|methods:isIDCard18,other'
  },
  methods: {
    isIDCard18: function(v) {
      return this.isID(v);
    },
    other: function(v) {
      // ... 自定义验证,返回boolean值
    }
  }
});
// 方式一
const input = validation.filter(req.body);
try {
  validation.check(input);
  // ... 业务代码
} catch(err) {
  return next(err);
}
// 方式二
try {
  const input = validation.validate(req.body);
  // ... 业务代码
} catch(err) {
  next(err);
}

模块说明

删除了代码中的逻辑验证.不要瞎jb写 required|nullable,min:abc,require少个d,range:(20,10),methods:fn1,,fn2等等乱七八的东西
required,nullable,empty,nonzero的区别:required,值不能为undefined;nullable,值可以为null,empty,值可以为空字符串;(empty:专门为前端准备,有些人就是要传id=&search=&time=);nonzero,不能是0或'0' '000'
file类型: 可以为文件对象,可以为字符串
一.内置字段类型(小写)
  元类型: boolean/enum/int/float/object/array/string/url/email/date/dateonly/timeonly/file/methods/IDCard/creditCard
  限制类型两大类:required/nullable/empty/nonzero/ignore/default/alias/min/max/length/minlength/maxlength/if
  说明:联合使用要求,min/max和int/float,minlength/maxlength/length和string
  int/float默认{m:10,n:2}
二.内置判断方法
  isUrl()/isDate()/isInt()/isFloat()/isEmail()/isID()/isCredit()/isString()/isChar()/isFile()
三.其他成员函数说明
  1) error() 统一错误处理,抛出异常
  2) filter() 滤除参数中额外的字段
  3) check() 对参数中指定的字段进行验证
  4) validate() 集成了filter()和check()的功能
  5) _str2rule() 将某个字段简约的字符串规则转化为详细的规则对象
  6) parse() 对所有的字段使用_str2rule()
  7) compile() 简单模板替换