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

mm_check

v1.5.1

Published

这是超级美眉check参数验证模块,用于验证参数是否正确

Readme

mm_check

这是超级美眉check参数验证模块,用于验证参数是否正确。本模块提供了一个强大的参数验证类,可以对各种类型的参数进行验证,包括字符串、数字、布尔值、数组和对象等。

安装

npm install mm_check

基本使用

const Check = require('mm_check');

// 创建验证实例
const check = new Check({
    name: "username",    // 参数名
    title: "用户名",    // 参数介绍名
    type: "string",     // 参数类型
    string: {
        notEmpty: true,   // 非空验证
        min: 3,           // 最小长度
        max: 20          // 最大长度
    }
});

// 验证参数
const result = check.run("test_user");
$.log.debug(result);  // 如果验证通过返回null,否则返回错误信息

参数配置说明

基础配置

  • name: 参数名称
  • title: 参数介绍名
  • type: 参数类型,支持 string、number、bool、dateTime、object、array
  • splitter: 分隔符号,用于查询时判断多个传参,默认为 "|"
  • default: 默认值

字符串验证配置 (string)

string: {
    notEmpty: false,      // 是否非空
    min: 0,               // 最小长度
    max: 0,               // 最大长度
    range: [],            // 长度范围,例:[6, 20]
    regex: "",           // 正则表达式
    identical: "",       // 验证与某个参数值是否相同
    different: "",       // 验证与某个参数值是否不同
    extension: "",       // 后缀名,多个用"|"分隔
    format: ""           // 格式验证:email、url、date、dateISO、number、digits、phone
}

数值验证配置 (number)

number: {
    min: 0,               // 最小值
    max: 0,               // 最大值
    range: []             // 数值范围,例:[0, 100]
}

数组验证配置 (array)

array: {
    min: 0,               // 最小成员数
    max: 0,               // 最大成员数
    range: [],            // 成员数范围
    type: "",            // 成员类型:string、number、object
    object: []            // 成员为对象时的验证规则
}

使用示例

1. 字符串验证

const userCheck = new Check({
    name: "username",
    title: "用户名",
    type: "string",
    string: {
        notEmpty: true,
        min: 3,
        max: 20,
        regex: "^[a-zA-Z0-9_]+$"  // 只允许字母、数字和下划线
    }
});

$.log.debug(userCheck.run(""));           // 用户名(username) 不能为空
$.log.debug(userCheck.run("ab"));         // 用户名(username) 最少需要输入 3 个字符
$.log.debug(userCheck.run("user@123"));   // 用户名(username) 格式不正确

2. 数值验证

const ageCheck = new Check({
    name: "age",
    title: "年龄",
    type: "number",
    number: {
        min: 0,
        max: 120
    }
});

$.log.debug(ageCheck.run(150));  // 年龄(age) 请输入不大于 120 的数值
$.log.debug(ageCheck.run(-1));   // 年龄(age) 请输入不小于 0 的数值

3. 数组验证

const tagsCheck = new Check({
    name: "tags",
    title: "标签",
    type: "array",
    array: {
        min: 1,
        max: 5,
        type: "string"
    }
});

$.log.debug(tagsCheck.run([]));              // 标签(tags) 数组的成员数不小于 1 个
$.log.debug(tagsCheck.run([1, 2, 3]));       // 数组成员数据类型不正确, 应为string型

4. 对象验证

const userInfoCheck = new Check({
    name: "userInfo",
    title: "用户信息",
    type: "object",
    object: [
        {
            name: "name",
            title: "姓名",
            type: "string",
            string: {
                notEmpty: true
            }
        },
        {
            name: "age",
            title: "年龄",
            type: "number",
            number: {
                min: 0,
                max: 120
            }
        }
    ]
});

$.log.debug(userInfoCheck.run({
    name: "",
    age: 150
}));  // 属性 -> 姓名(name) 不能为空

错误提示配置

可以通过 lang 方法自定义错误提示信息:

check.lang({
    notEmpty: "字段不能为空",
    minLength: "长度不能小于 {0} 个字符",
    maxLength: "长度不能超过 {0} 个字符",
    // ... 其他提示信息
});

API 参考

Check 类

构造函数

  • constructor(param): 创建一个新的验证实例
    • param: 验证配置对象

实例方法

  • run(value, [config]): 验证值是否正确

    • value: 要验证的值
    • config: 可选的验证配置,默认使用构造函数的配置
    • 返回:验证通过返回null,否则返回错误信息
  • lang(obj): 获取或设置错误提示

    • obj: 设置的提示对象,为空则获取当前提示
    • 返回:错误提示集合
  • msg(key, v1, v2): 获取错误提示

    • key: 语言包键
    • v1: 替换的词1
    • v2: 替换的词2
    • 返回:错误提示语句

注意事项

  1. 使用前需要先安装 mm_expand 模块,因为本模块依赖于它。
  2. 验证失败时返回的错误信息会自动包含参数名和标题。
  3. 支持链式验证,可以同时设置多个验证规则。
  4. 对于数组和对象的验证,支持递归验证其成员或属性。

许可证

MIT