Skip to content

bolin-L/ruleV

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

getValueStepIn(attr, obj)

递进获取对象属性

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

单个值校验

checkAll(source, ruleConfig, immediately, errReturnWay)array

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

is(value, rule)boolean

正则校验值

isRequired(value)boolean

校验值是否为空

isFilled(value)boolean

校验值是在trim后否存在

isChinese(value)boolean

检查值是否全为中文

isEnglish(value)boolean

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

isName(value)boolean

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

isLength(value, rule)boolean

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

calDateAndSex(length, id)boolea

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

strictValidateIdCard(idCard)boolean

严格校验身份证

isIdCard(value)boolean

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

isQQ(value)boolean

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

isMobilePhone(value)boolean

检查值是否符合手机号

isBasePassword(value)boolean

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

isSafePassword(value)boolean

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

isEmail(value)boolean

检查值是否是邮箱

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

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

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

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 }

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

Releases

No releases published

Packages

No packages published