- Simple
- Lightweight
- Extensible
validate = require('vchains').validate;
console.log(
validate('4276-8010-XXXX-5090', 'Credit card number is valid.').isCard('It is not credit card number!').msg()
); // Will print "Credit card number is valid."If you need custom validation method, your can extend vchains.
Call vchains.use() with your validation method as parameter. After that you can access you method from any validation object instance.
vchains = require('vchains');
validate = vchains.validate;
vchains.use('globalTest', function(msg){
if(this.value.indexOf(' ') >= 0) return msg || 'Value contains space!';
});
console.log(
validate('Hello, world!').globalTest().msg()
); // Will print "Value contains space!"You can register multiple custom validators at once.
vchains = require('vchains');
validate = vchains.validate;
vchains.use({
'globalTest1': function(msg){
if(this.value.indexOf(' ') >= 0) return msg || 'Value contains space!';
},
'globalTest2': function(msg){
if(this.value.indexOf('#') >= 0) return msg || 'Value contains "#"!';
}
});
console.log(
validate('Hello, world! #1').globalTest1().msg(),
validate('Hello, world! #2').globalTest2().msg()
); // Will print "Value contains space! Value contains "#"!"You can add custom validation method directly from validation chain.
In this case custom method will be accessible only for current validation object.
validate = require('vchains').validate;
console.log(
validate('My string', 'No error :)').use('localTest', function(c, msg){
if(this.value.indexOf(c) >= 0) return msg || 'Error in localTest!';
}).localTest('s').msg()
); // Will print "Error in localTest!"If the second parameter of vchains.use() is 'true' validator will be accessible from any new validation object.
validate = require('vchains').validate;
console.log(
validate('My string', 'No error :)').use('globalTest', function(msg){
if(this.value.indexOf(c) >= 0) return msg || 'Error in localTest!';
}, true /* !!! */).globalTest().msg(),
validate('Other_string', 'This string does not contains spaces.').globalTest().msg()
); // Will print "Error in localTest! This string does not contains spaces."You can pass any number of parameters to your custom validator.
Last parameter must be an error message if validation failed.
validate = require('vchains').validate;
console.log(
validate('Short string', 'No error :)').use('parametersTest', function(min, max, msg){
var l = String(this.value).length;
if(min > l || l > max) return msg || 'Error in parametersTest!';
}).parametersTest(0, 32).msg();
); // Will print "Error in parametersTest!"validate = require('vchains').validate;
validate('123a').onError(function(err){
if(err) console.log('Validation error:', err); else console.log('No error!');
}).isNum('Not numeric string!'); // Will print "Validation error: Not numeric string!"