1.2.2 • Published 5 years ago

jvd.js v1.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

jvd.js

js validator, an awesome validator for js

just do it

npm i --save jvd.js
var JVD = require('jvd.js')

// test 
JVD().isString().gt(23).lt(13).test(null)
// test 81 > 10  and 81 < 80
JVD().isNumber().gt(10).lt(80).test(81)
// test 110 >=10  and 110 <= 100
JVD().isNumber().not().lt(10).not().gt(100).test(110)
// test 9 < 10 or 9 > 100
JVD().isNumber().lt(10).or().gt(100).test(9)


// test not empty
JVD().anything().test(null)
// test required
JVD().required().test('')
// test 25 between 24 and 27
JVD().between(24,27).test(25)
//test 25 between 24 and 23
JVD().between(24,23).test(25)
//test json equils
JVD().equils({name : 'LiSA'},(n1,n2)=>{return n1.name == n2.name}).test({'name':'LiSA'})
//test array contains 
JVD().in([1,3,5,7]).test(5)
//test 'hello' equils 'hi'
JVD().is('hello').test('hi')
//test is Array
JVD().isArray().test([])
//test is AsyncFunction
JVD().isAsyncFunction().test(()=>{})
// test is int
JVD().isInteger().test(1.1)
// test is JSON
JVD().isJSON().test({})
// test is regexp
JVD().isRegExp().test(/ab/g)
// test matches
JVD().match(/abc/g).test('helloabcaa')

采用表达式

表达式更简单易写

 //JVD().isString().gt(23).lt(13).run(null)
JVD("?''&&>23&&<13").run(null)
//JVD().isNumber().gt(10).lt(80).run(81)
JVD("?d&>10&<80").run(81)
//JVD().isNumber().not().lt(10).not().gt(100).run(110)
JVD("?d&!<10&!>100").run(110)
//JVD().isNumber().lt(10).or().gt(100).test(9)
JVD("?d&<10|?>100").test(9)

// JVD().anything().test(null)
JVD('!!').test(null)
JVD("!!").test(9)
// JVD().required().test('')
JVD("!!").test('')
// JVD().between(24,27).test(25)
JVD("?(24,27)").test(25)
// JVD().between(24,23).test(25)
JVD("?(24,23)").test(25)
// JVD().equils({name : 'LiSA'},(n1,n2)=>{return n1.name == n2.name}).test({'name':'LiSA'})
// JVD().in([1,3,5,7]).test(5)
JVD("?[1,3,5,7]").test(5)
// JVD().is('hello').test('hi')
JVD("='hello'").test('hello')
// JVD().isArray().test([])
JVD("?[]").test([1,2])
// JVD().isAsyncFunction().test(()=>{})
JVD("?async").test(()=>{})
// JVD().isInteger().test(1.1)
JVD("?int").test(1.1)
// JVD().isJSON().test({})
JVD("?j").test({})
// JVD().isRegExp().test(/ab/g)
JVD("?//").test(/ab/g)
// JVD().match(/abc/g).test('helloabcaa')
JVD("?/abc/").test('helloabcaa')

说明

expressionjvd方法名称notremark
& &&.内置操作符, and
|||or内置操作符
not内置操作符
?''?stringisString!'' !string
?d?numberisNumber!d !number
?intisInteger!int
?function?fn?fun?methodisFunction!function!fn!fun!method
?syncisSyncFunction!sync
?asyncisAsyncFunction!async
?json or ?j or ?{} or ?objectisJSON!json or !j or !{} or !object
?[] or ?arrayisArray![] or !array
?// or ?regexpisRegExp!// or !regexp
!!requiredanything
?/ab/patternmatch!/ab/
?>12>12gtgreaterThan!>12<=12当待验证数据是字符串时,比较字符串长度当验证数据为数组时,比较数组长度
?<12<12ltlitterThan!<12>=12同上
?(1,200.2)betweenrange!(1,200.2)
?1,'a','ccc'oneOfin1,'a','ccc'
?=1?='b'='dd'equilstoBeis!=2
transInfo(属性)字符串表达式转换结果,用于判断字符串是否可以转换为jvd
regadd用于扩展jvd的方法,可用于覆盖现有实现
testrungo执行jvd验证,核心方法
isJVD用于判断是否为JVD,永久返回true
$空实现,根据传值判断是否返回true,不传值返回true

如何扩展

var JVD = require('jvd.js')

var jvd = JVD()
//这里注意 参数data和options是必须的 
jvd.reg('hello', (data,options,yourParam) => {
    console.log(options.hi)
    console.log(yourParam)
    if (data && data =='hello')
        return true
    return false
})

var options = { hi : 'good days'}
jvd.hello('here is your param').test('hello',options)
1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.1

5 years ago

1.1.2

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago