validator-it
v2.1.0
Published
Just validate it, based on [validator](https://github.com/chriso/validator.js).
Downloads
19
Readme
validator-it
Just validate it, based on validator.
Install
npm i validator-it
Examples
var validatorIt = require('validator-it');
var validatePosts = validatorIt({
author: {
_id: validatorIt.isMongoId(),
name: function checkName(name) {
if (['nswbmw', 'jack'].indexOf(name) === -1) {
throw new Error('You are not admin.');
}
},
email: validatorIt.isEmail()
},
content: validatorIt.isByteLength(10)
});
validatePosts({
author: {
_id: '5643263df301e3550988b3c8',
name: 'nswbmw',
email: '[email protected]'
},
content: 'abcdefghijk'
});
// return true
validatePosts({
author: {
_id: 'abc',
name: 'nswbmw',
email: '[email protected]'
}
});
// return false
validatePosts({
author: {
_id: 'abc',
name: 'nswbmw',
email: '[email protected]'
}
}, true);
// throw
// [author._id: abc] ✖ isMongoId
validatePosts({
author: {
_id: '5643263df301e3550988b3c8',
name: 'guest',
email: '[email protected]'
}
});
// throw
// You are not admin.
validatorIt.isEmail()('[email protected]') // { key: 'isEmail', value: true }
validatorIt.contains('foo')('[email protected]') // { key: 'contains(foo)', value: true }
validatorIt.validator.isEmail('[email protected]') // true
validatorIt.validator.contains('[email protected]', 'foo') // true
More examples see test.
License
MIT