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 🙏

© 2026 – Pkg Stats / Ryan Hefner

honey-validate

v1.0.2

Published

## 特性

Readme

Honey-Validate 校验库

特性

  • 兼容 AMDCMD 规范
  • 支持单个数据和数据批量验证
  • 扩展性好 支持自定义验证(函数/正则)

使用说明

获取内置规则

import { rules } from "HoneyValidate";

为字段名增加中文名称

直接在规则中写上"@+名称"即可 举个栗子,不加@

{'name':'require!必须写$0'}
// 必须写name

加上之后

{'name':'@姓名|require!必须写$0'}
// 必须写姓名

自定义消息

在规则名称后面加上“!”则为该条规则的自定义消息
$0为字段名称
$1..$n依次为后面的参数
$n直接显示为参数(逗号隔开)
:1表示该参数为一个字段
例如'confirm': '$0与:1不一致'其中:1将替换为字段名而不是参数

{'name':'@姓名|require!必须写$0'}
// 输出 必须写姓名

开始使用

先引入 <script src='HoneyValidate.js'></script> 然后直接使用

/**
 * 检查数据
 * @param  rule  单或多条规则
 * @param  field 要检查的数据
 * @param  all   是否返回全部错误
 * @return       检查通过返回true 失败返回 错误消息或错误消息集合
 */
HoneyValidate.check(rule, field, all);
/**
 * 添加或修改规则
 * @param  name 规则名称
 * @param  rule 规则内容(函数或正则)
 * @param  msg  消息
 * @return      this
 */
HoneyValidate.rule(name, rule, msg);

使用示例

var rule = {
    name: "@姓名|require|chinese",
    idcard: "@身份证号码|require|idcard",
    type: "@类型|require|in:0,1",
    qq: "@QQ号码|require|number",
    email: "@电子邮箱|require|email",
    password: "@密码|require|len:6,10",
    password2: "@确认密码|confirm:password!确认密码不一致",
    like: "@喜欢的数字|require|in:0,1,2,3,4,5,6",
  },
  data = {
    name: "nullfeng",
    idcard: "50023619951105256a",
    type: 0,
    qq: "",
    email: "nullfeng(at)163.com",
    password: "123123",
    password2: "456456",
    like: "8",
  };
/**
 * 验证全部的错误
 */
var result = HoneyValidate.check(rule, data, true);
console.log(result);
//output:
/*{
	email:"电子邮箱格式不正确"
	idcard:"身份证号码格式不正确"
	like:"喜欢的数字只能取0,1,2,3,4,5,6"
	name:"姓名只能是中文"
	password2:"确认密码不一致"
	qq:"QQ号码不能为空"
}*/
/**
 * 验证单个错误
 */
result = HoneyValidate.check("@昵称|chinese", "nullfeng");
console.log(result);
// output: 昵称只能是中文

增加自定义规则(支持正则和函数)

// 增加一个只能是指定值的规则
HoneyValidate.rule(
  "diy_rule",
  function (v, m) {
    //this.data 获取要验证的数据 如 this.field['username']
    //this.test 执行已有的验证规则 如 this.test.eq('2',2);
    return v == m;
  },
  "$0 只能是$1"
);
// 使用
const test = "23";
const result = HoneyValidate.check(test, "@测试变量|diy_rule:24");
console.log(result);
// 输出:测试变量 只能是24

//增加一个只能是5开头的规则
HoneyValidate.add("diy_rule", /^5\w+/, "$0 只能是5开头哦");
// 使用
const test = "abcd";
const result = HoneyValidate.check(test, "@测试变量|diy_rule");
console.log(result);
// 输出:测试变量 只能是5开头哦

check()返回值

验证通过返回真 true,失败直接返回错误消息

const result = HoneyValidate.check(...);
if(result !== true){
    // 验证不通过
    alert(result);
}