satpam
Satpam is a wrapper for some nodejs validator libraries, I made Satpam
so it's easy to create
custom validator with parameters and custom validation messages.
Quick Usage
var satpam = require('satpam');
var rules = {
name : ['required']
officeEmail : ['email'],
phone : ['required', 'numeric']
};
var input = {
name: 'Sendy',
title: 'Lord',
officeEmail: 'invalid email',
phone: 'hi there123'
};
var result = satpam.validate(rules, input);
if (result.success === true) {
} else {
result.messages.officeEmail.email === 'OfficeEmail must be email';
result.messages.phone.number === 'Phone must be numeric';
result.messageArray[0] = 'OfficeEmail must be email';
result.messageArray[1] = 'Phone must be numeric';
}
Custom rules
validator.addCustomValidation('must-be-ironman', function(val) {
return val === 'ironman';
});
validator.setValidationMessage('must-be-ironman', 'Not ironman D:');
validator.addCustomValidation('range:$1:$2', function(val, ruleObj) {
return val >= ruleObj.params[0] && val <= ruleObj.params[1];
});
validator.setValidationMessage('range:$1:$2', '<%= propertyName %> must between <%= ruleParams[0] %> and <%= ruleParams[1] %>');
TODOs
- Add more basic validation rules
- Validate file types
More examples -> Here