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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rulev

v1.0.5

Published

validator for javascript, nodejs

Downloads

7

Readme

npm 规则校验工具 ruleV

  • 大小4kb, 其他完整的校验库太大也用不到,有点浪费
  • 可以集成validator,做底层校验
  • 自定义覆盖底层校验方法

安装

npm install rulev --save

使用

单个校验

import ruleV from 'rulev';

rulev.check('123456', { type: 'isMobilePhone', message: '请输入正确的手机号' });
// return { success: false, message: '请输入正确的手机号' }

校验对象

import ruleV from 'rulev';

const source = { a: 1, b: { c: '啦啦啦' } };
const ruleConfig = { 
    a: [{ type: 'isRquired', message: '值不能为空' }],
    'b.c': [
        { type: 'isRquired', message: '请输入字符' },
        { type: 'isEnglish', message: '请输入英文字符' },
    ] 
}

rulev.checkAll(source, ruleConfig);
// return { success: false, message: '请输入英文字符', checkAttr: 'b.c' }

覆盖/增强顶层校验

import ruleV from 'rulev';

ruleV.install({
    isMe(name) { return name === 'me' }
});

ruleV.check('me', { type: 'isMe' }); // true

Functions

install(enhance, override) ⇒ object

增强/覆盖校验对象,与默认校验方法合并

Kind: global function

| Param | Type | Description | | --- | --- | --- | | enhance | object | 校验方法对象 | | override | boolean | 覆盖源校验对象方法,如果有同名方法 |

getValueStepIn(attr, obj) ⇒

递进获取对象属性

Kind: global function
Returns:

| Param | Type | Description | | --- | --- | --- | | attr | string | 对象的属性 | | obj | object | 源对象 |

Example

// return 1
getValueStepIn('a.b', { a: { b: 1 } })

getValueStepIn~attrSteps

兼容 a.b 与 a[b]

Kind: inner constant of getValueStepIn

check(value, rules, [checkAttr], [source], errReturnWay) ⇒ object

单个值校验

Kind: global function
Returns: object - { success: true, message: '', checkAttr, }

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 | | rules | array | 校验规则数组 | | [checkAttr] | string | 需要被校验的属性(可选), type为自定义校验方法时可用 | | [source] | object | 属性的源对象(可选) , type为自定义校验方法时可用 | | errReturnWay | boolean | 校验失败返回方式 message|boolean|错误对象 |

checkAll(source, ruleConfig, immediately, errReturnWay) ⇒ array

根据属性配置进行批量校验

Kind: global function
Returns: array - [ { success: false, message: '请输入正确的手机号码', checkAttr: 'user.mobile', } ]

| Param | Type | Default | Description | | --- | --- | --- | --- | | source | object | | 需要校验的属性的源对象 | | ruleConfig | object | | 需要校验的属性与校验规则数组的配置对象 | | immediately | boolean | true | 校验第一个错误立即停止返回 | | errReturnWay | boolean | | 校验失败返回方式 message|boolean|错误数组 |

Example

checkAll({ user: { mobile: '12345' } }, { 'user.mobile': [ { type: 'isRequired', message: '请输入手机号码' }, { type: 'isMobilePhone', message: '请输入正确的手机号码' } ] })

is(value, rule) ⇒ boolean

正则校验值

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 | | rule | object | 校验规则 reg || { reg: /xxx/ } |

isRequired(value) ⇒ boolean

校验值是否为空

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 |

isFilled(value) ⇒ boolean

校验值是在trim后否存在

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 |

isChinese(value) ⇒ boolean

检查值是否全为中文

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 |

isEnglish(value) ⇒ boolean

检查值是否全为英文字符a-zA-Z

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 |

isName(value) ⇒ boolean

检查值是否全为中英文混合/姓名

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 |

isLength(value, rule) ⇒ boolean

检查值的长度是否在限制的范围

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的值 | | rule | object | 校验的规则 { min: 1, max: 5 } | { options: { min: 1, max: 5 } } |

calDateAndSex(length, id) ⇒ boolea

根据位数计算身份证的合理性

Kind: global function

| Param | Type | Description | | --- | --- | --- | | length | Number | 身份证位数 | | id | String | 身份证号 |

strictValidateIdCard(idCard) ⇒ boolean

严格校验身份证

Kind: global function

| Param | Type | Description | | --- | --- | --- | | idCard | String | 身份证号 |

isIdCard(value) ⇒ boolean

检查值是否符合身份证 15|17X|18位

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的身份证号 |

isQQ(value) ⇒ boolean

检查值是否符合QQ号 4+位数字

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的QQ号 |

isMobilePhone(value) ⇒ boolean

检查值是否符合手机号

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的手机号 |

isBasePassword(value) ⇒ boolean

检查值是否符合简单密码 5-17位的大小写数字

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的密码 |

isSafePassword(value) ⇒ boolean

检查值是否符合复杂密码 6-18位的大小写数字组合,开头必须英文字符

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的密码 |

isEmail(value) ⇒ boolean

检查值是否是邮箱

Kind: global function

| Param | Type | Description | | --- | --- | --- | | value | string | 需要校验的密码 |

CHANGE LOG

2019-10-29 - 1.0.5

【A】- 添加isEmail内置校验规则 【A】- checkAll、check方法支持通过传参数errReturnWay指定校验结果返回格式 boolean | message, 默认返回Array|Object

2019-10-14 - 1.0.3

【A】- 支持身份证通过strict字段配置严格校验

2019-09-04 - 1.0.2

【A】- 支持身份证校验最后以为x