@ozylog/validator
v0.3.1
Published
Validator and sanitizer
Downloads
5
Readme
@ozylog/validator
Validator and sanitizer
This validator is inspired by express-validator and using validator functions to validate.
Installation
npm install @ozylog/validator --save
Usage Examples
validator
Please check validator page to see list all available functions + isRequired()
import {validator} from '@ozylog/validator';
validator.isEmpty(''); // returns true
validator.isRequired('') // returns false
addValidator
import {addValidator} from '@ozylog/validator';
addValidator({
isEmailAvailable: async () => true
});
sanitizer
import {sanitizer} from '@ozylog/validator'
sanitizer.trim(' test '); // returns 'test'
Rules
import {Rules, Rule} from '@ozylog/validator';
const name = '';
const nameRule = new Rule('name', name);
nameRule.isRequired().setMessage('Name is required');
nameRule.isLength({min: 8}).setMessage('Min name length is 8');
const email = '[email protected]';
const emailRule = new Rule('email', email);
emailRule.isEmail().setMesssage('Invalid email');
const rules = new Rules(nameRule, emailRule);
rules.validate(); // returns {name: 'Name is required'}
rules.validate({checkAll: true}); // returns {name: ['Name is required', 'Min name length is 8']}
import {addValidator, Rules} from '@ozylog/validator';
addValidator({
isEmailAvailable: async () => false
});
const email = '[email protected]';
const rules = new Rules();
rules.addRule('email', email).isEmailAvailable().setMessage('Email is not available');
await rules.validatePromise(); // returns {email: 'Email is not available'}
await rules.validatePromise({checkAll: true}); // returns {email: ['Email is not available']}
import {Rules, Rule} from '@ozylog/validator';
const email = '[email protected]';
const rules = new Rules();
const emailRule = new Rule('email', email).isRequired().isEmail();
rules.addRules(emailRule);
const errors = rules.validate(); // returns null
License
MIT