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= ; // can pass console.log as error handler, or any custom you prefervar validator= consolelog ; // add a single onevalidator; // or add multiple at oncevalidator; // add a strict comparison, will only match 11 with type Numbervalidator; // use a registered key to validate a valuevar validated= validator;console;// true validated= validator;console;// false // validate multiple values of different types at once// passing true as second argument to continue validating all values after// on or more values didn't passvalidated= validator;// value: "hey!" did not pass name! { key: 'name', value: 'hey!', regexp: /^[A-Za-z]{1,7}$/ }console;// false
usage details
All examples use the context created below.
var Validator = // can pass console.log as error handler, or any custom you prefer validator = consolelog ;
setErrorHandler
<this> setErrorHandler( <function> callback )
The callback set with setErrorHandler will be called on any error that occurs.
// let console.log be the error handlervalidator;
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
// add a single onevalidator; // or add multiple at oncevalidator; // non RegExp values will be used for strict value && type comparisonvar account= ;account;console;// true // returns false if type is not equalvalidator;console;// false
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.
// singlevar validated= validator; // multiplevar validated= validator;
validations
<object> validations
Context property holding all validation key's
change log
0.0.1
- first commit
license
MIT