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

lg.validator

v0.0.5

Published

数据校验器,支持复杂嵌套校验,不限制嵌套层级,支持数组嵌套对象嵌套,内置丰富的校验规则,支持自定义全局规则,规则配置简单而不失扩展性

Downloads

88

Readme

数据校验器,支持复杂嵌套校验,不限制嵌套层级,支持数组嵌套对象嵌套,内置丰富的校验规则,支持自定义全局规则,规则配置简单而不失扩展性

六哥开源前后端脚手架

  • https://gitee.com/lgx1992/lg-soar

六哥npm工具库

校验

import validator from 'lg.validator'

// 要校验的数据
const data = {
    name: '六哥',
    age: 61,
    phone: '1507878797',
    idcard: '110105202302190038',
    list: [
        { kkk: 1, idcard: '11010520230219003x', map: { xxx: '1507878797' } },
        { kkk: 1, idcard: '110105202302190036', map: { xxx: '15078787979' } }
    ],
};

// 校验规则
const rule: { [k: string]: Rule } = {
    name: '名称必填', // 使用字符串配置规则
    age: (v: any) => v < 60 ? '' : '年龄必须小于60', // 使用校验函数配置规则
    phone: { phone: true, msg: '请输入有效的手机号码' }, // 使用对象配置规则
    idcard: [{ idcard: true, msg: '请输入有效的身份证号码' }], // 使用对象数组配置规则
    list: {
        // 数组嵌套校验
        array: {
            kkk: {
                min: 2,
                required: true,
                msg: 'kkk不能小于2',
            },
            idcard: {
                idcard: true,
                msg: '请输入有效的身份证号码',
            },
            map: {
                // 对象嵌套校验
                object: {
                    xxx: { phone: true, msg: '请输入有效的手机号码' },
                },
            },
        },
    },
}

// 校验
validator(data, rule).then(() => {
    console.log('校验成功', data);
}).catch((err) => {
    console.error('校验失败', err);
    document.querySelector<HTMLDivElement>('#app')!.innerHTML = JSON.stringify(err);
    alert(err.errors.join(','))
});

添加自定义校验规则

import { addRule } from 'lg.validator'

// 添加全局校验规则
addRule('myv', (value: any, params: any, { data, rules, fieldRule }) => {
    return value === 'lg-soar';
})

// 使用自定义校验规则
validator({ name: 'lg-soar1' }, { name: { myv: true, msg: '必须输入lg-soar', } });

使用内置规则校验

import { getRules } from 'lg.validator'

const rules = getRules();
console.log('idcard', rules.idcard('11010520230219003x'))
console.log('phone', rules.phone('15078787979'))
console.log('maxLength', rules.maxLength('15078787979', 10))