Skip to content

combine multiple string validations within a common context

License

Notifications You must be signed in to change notification settings

FlorianEdelmaier/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

combine-validators

Travis Codecov npm downloads MIT License

A Validator gives validator.js string validators a common context and allows composition.

const Validator = require('combine-validators');
const validations = Validator.validations;

const v = new Validator();
v.check(validations.isMandatory, validations.isEmail)(req.body, 'email', 'Email not valid');
v.check(validations.isOptional, validations.isIn('a', 'b', 'c'))(req.body, 'type', 'Type not valid');
v.check(validations.isMandatory, validations.isMongoId)(req.body, '_id', 'Id not valid');
if(v.hasErrors()) console.log('Validation rejected');