Introduction
Validator Framework, write once and use anywhere.
Install
$ npm install vfw
Demo
var vfw = require('vfw')
var ruleSet = vfw.parse({
a: 'String:required',
b: 'Money'
})
ruleSet.check({a: 'as', b: 99})
ruleSet.check({a: 12, b: 'x.x'})
ruleSet = vfw.parse(['String', 'Moeny'])
ruleSet.check(['as',99])
ruleSet.check([123, 'x.x'])
ruleSet = vfw.parse({
$array: {
$array: 'String'
}
})
ruleSet.check([['as'], ['ds']])
ruleSet = vfw.parse({
a: {
b: 'String'
c: ['Method', 'Url']
}
})
ruleSet.check({a: {b: 'as', c: ['get', 'http://github.com']}})
ruleSet = vfw.parse({
$or: {
a: 'String',
b: 'Moeny'
}
})
ruleSet.check({a: 'as'})
ruleSet.check({a: 23, b: 12.2})
ruleSet = vfw.parse({
a: {
$eq: 1
}
})
ruleSet.check({a: 1})
vfw.type('String', 'as')
var a = 1
vfw.expression('eq', a, 1)
vfw.$('eq', a, 1)
vfw.extend('type', {
Word: function (str) {
return /^\w+$/.test(str)
}
})
vfw.type('Word', 'azAZ_09')
ruleSet = vfw.parse({a: 'Word'})
ruleSet.check({a: 'azAZ_09'})
vfw.extend('expression', {
$gt: function (str, len) {
return str && str.length > len
}
})
var b = [1, 2]
vfw.$('gt', b, 1)
ruleSet = vfw.parse({a: {$gt: 2}})
ruleSet.check({a: 'aas'})
vfw.extend('struct', 'User', {
name: 'Word:required',
password: {
$type: 'Word',
$lte: 6
}
})
vfw.struct('User', {name: 'asd_123', password: 'asd_as'})
vfw.struct('User', {name: 'asd_123', password: 'asd_as123'})
vfw.struct('User', {password: 'ass123'})
CLASS
RuleType
- type, expression, struct are all intanceof RuleType, and you can extend your own RuleType by extendRule api.
-
attributes
- _extended:
Object
store extended functions - _includes:
Array
store included libs
-
methods
- canHandle(rule)
required
check if the rule can be handled by this ruleType
- check(arr, rule)
- you can rewrite this function and check in aother way
- the first arguments is Array
- extend(obj)
- include(lib)
- define how to include a lib
- get(key)
- has(key)
Rule
-
attributes
- _name:
String
name of rule type - _rule: rule object. for {a: 'String'}, _rule is 'String'
- _path: the path of the object. for {a: 'String'}, _path is 'a'
- _hdl: handler of this rule
-
methods
- check(target)
- clone()
- addPath(path, depth)
- add a path to this rule.depth is default 0.
- setHandler(handler, rule)
- finish()
- before add to struct, you should finish this rule first
Struct
-
attributes
- uid
String
uniq id of this struct - _rules
Array
array of rules - _ruleMap
Object
map of rules
-
methods
- add(rule) add a rule to this struct
- check(obj, opts) if opts.withErrors is true, check function will return [res, errs]
API
vfw.type(Type, target)
vfw.type('String', 'asd')
vfw.type('Number', 'asd')
vfw.expression or vfw.$
vfw.$('in', [1, 2], 1)
vfw.struct(StructName, target)
vfw.struct('User', {user: 'lee', password: '123456'})
vfw.extendRule(opts)
- extend your own RuleType. type, expression, struct are all instanceof RuleType.
vfw.extendRule({
canHandle: function (obj) {
return /^@/.test(obj)
},
name: 'custom'
})
vfw.custom('@as', 12)
vfw.parse({
a: '@string'
})
vfw.extendParser(Function)
vfw.extendParser(function (rule, struct, ruleIns) {
if (!(typeof rule === 'string' && rule.charAt(0) === '#')) return false
rule = rule.slice(1)
rule = 'is' + rule.charAt(0).toUpperCase() + rule.slice(1)
var handler = vfw.get(rule)
if (!handler) return false
ruleIns.setHandler(handler, rule)
struct.add(ruleIns)
return true
})
var _ = require('lodash')
vfw.include('type', _)
var struct = vfw.parse({a: '#PlainObject'})
struct.check({a: {a: 1}})
vfw.extend(type, extends)
- type
String
name of RuleType, like 'type', 'expression' - extends
Object
the functions you want to extend
vfw.extend('type', {
Money: function(){},
Url: function(){}
})
vfw.extend('expression', {
$lt: function(target, len){},
$startWith: function(target, char){}
})
vfw.include(type, obj)
- type
String
name of RuleType, like 'type', 'expression' - obj
Object
- you can include a lib like lodash, validator or others and use the functions they supported
var _ = require('lodash')
vfw.include('type', _)
var struct = vfw.parse({a: '#PlainObject'})
struct.check({a: {a: 1}})
vfw.parse(obj)
- parse a rule object and return an instance of Struct
var rule = {
a: 'String:required',
b: 'Money'
}
var struct = vfw.parse(rule)
struct.check({a: 'as', b: 1.2})