validation-builder-js
A Javascript Universal Module to build reusable validation patterns.
This module is fully functional but it is more like a proof of concept due to the lack of validators.
However, you can implement your own validators or plug validators from other libraries in as described later.
This module depends on underscorejs.
Installation
You may want to install validation-builder-js
using npm
or bower
.
npm install validation-builder-js --save
bower install validation-builder-js --save
Basic usage
This module is suitable for use in Node.js and browser environments.
It has been tested for use in Node.js.
It has not been tested on any browser yet.
Node
var ValidationBuilder = ; var validation = ValidationBuilder // creates a new instance of ValidationBuilder // creates a new instance of Validation result = validation; // argument to run() is the subject of validation console; // falseconsole; // trueconsole; // true
Browser
Built-in validators
containsAll
var subjectArray = 'a' 'b' 'c' compareArray = 'a' validation = ValidationBuilder result = validation; // does subjectArray contain all elements in compareArray? console; // true
containsAny
var subjectArray = 'a' 'b' 'c' compareArray = 'a' validation = ValidationBuilder result = validation; // does subjectArray contain any of the elements in compareArray? console; // true
Proxied validators
ValidationBuilder proxies to Underscore.js to provide 16 validators.
- isArguments
- isArray
- isBoolean
- isDate
- isElement
- isEmpty
- isEqual
- isFinite
- isFunction
- isNaN
- isNull
- isNumber
- isObject
- isRegExp
- isString
- isUndefined
Custom validators
You can implement your own validator and register it to ValidationBuilder
as follows:
ValidationBuilder;
Custom validator will be available to any new instance of ValidationBuilder
.
Your validator should meet following requirements
- accept subject of validation as first argument;
- validate any argument;
- only return a boolean value;
- not to throw any error.
Plug validators from other libraries in
ValidationBuilder; var v = ValidationBuilder ; console; // true