batch-validator
Create challenges and validate values against them
#####features
- register challenges with regexp's or strict matching values
- supports custom error handler for detailed error message and related data
- can validate one or multiple values at once
- very type safe
###usage
npm install --save batch-validator
###example, can run
var Validator= require( 'batch-validator' );
var validator= new Validator( console.log );
validator.add( 'number': /^[0-9]+$/ );
validator.add({
hex : /^[0-9A-Fa-f]+$/
,name : /^[A-Za-z]{1,7}$/
});
validator.add( 'my-number', 11 );
var validated= validator.validate( 'number', 33 );
console.log( validated );
validated= validator.validate( 'my-number', '11' );
console.log( validated );
validated= validator.validate([
{ number : 33 }
,{ number : 42 }
,{ name : 'hey!' }
,{ hex : 0 }
], true );
console.log( validated );
###usage details
All examples use the context created below.
var
Validator = require( 'batch-validator' )
,validator = new Validator( console.log )
;
#####setErrorHandler
<this> setErrorHandler( <function> callback )
The callback set with setErrorHandler will be called on any error that occurs.
validator.setErrorHandler( (err, obj) => {
console.log( 'error:', err );
console.log( obj );
});
#####hasKey
<boolean>hasKey( <string> key )
Returns true if the key is added to the context object, or false if not.
#####add
<boolean> add( <object>/<string> obj, <regexp> regexp )
Adds a single validation object or multiple at once
validator.add({ number: /^[0-9]+$/ });
validator.add({
number : /^[0-9]+$/
,hex : /^[0-9A-Fa-f]+$/
,name : /^[A-Za-z]{1,7}$/
});
var account= new Validator;
account.add( 'john25', '?kIWs-45.oQsN[/' );
console.log( account.validate('john25', '?kIWs-45.oQsN[/') );
validator.add( 'my-number', 11 );
console.log( validator.validate('my-number', '11') );
#####validate
<boolean>/<null> validate( <array>/<string> validations, <string>/<number> value )
Validate one or multiple values at once. If validations is a collection of validations the second argument can be set to true to continue validating al remaining validations after one or more validations didn't pass.
validate returns true or false for regexp pass or fail, but will return null if a type error occurs or a key has not been registered with add.
var validated= validator.validate( 'number', 0 );
var validated= validator.validate([
{ number : 33 }
,{ number : 42 }
,{ name : 'hey!' }
,{ hex : 0 }
]);
#####validations
<object> validations
Context property holding all validation key's
###change log
#####0.0.1
###license
MIT