promise-validate
v0.0.1
Published
Promise based validation solution.
Downloads
2
Readme
promise-validate
Promise based validation solution.
Install
npm install promise-validate
Example
var V = require('promise-validate');
var Q = require('Q');
// custom async validator
function myValidate(msg) {
return function(val) {
var dfd = Q.defer();
setTimeout(function() {
if (Math.random() > 0.5) {
dfd.resolve(val);
} else {
dfd.reject(new Error(msg));
}
}, 1000);
return dfd.promise;
};
}
// Pipe validators to a new validator.
validateEmail = V.pipe(
V.isEmail('Please input a valid Email'),
V.isLength('6-20 characters', 6, 20),
myValidate('Oops')
);
validateEmail('[email protected]').then(V.log, V.log); // [Error: Oops] or [email protected]
validateEmail('foobar').then(V.log, V.log); // [Error: Please input a valid Email]
// Direct use of validator
V.isIP('Please input an IP address')('127.0.0.1').then(V.log, V.log);
Validators
promise-validate wraps validators from validator.js.
For example, validator.isLength(str, min [, max])
=> V.isLength(msg, min [, max])(str)
.