proxy-validator
Small package that leverages the power of ES6 Proxy to validate and sanitize objects.
Learn More, read the article:
How I made a validation library using ES6 Proxy
Installation
$ npm i proxy-validator
Usage
The API is fairly simple. Create a validator by providing a validation schema and/or a sanitizing schema.
import ProxyValidator from 'proxy-validator';
const validators = {
name: {
isLength: {
options: {
min: 6
},
errorMessage: 'Minimum length 6 characters.'
},
isUppercase: true
},
mobile: {
isMobilePhone: {
options: 'en-GB',
errorMessage: 'Invalid mobile number.'
},
isAlphanumeric: true
}
};
const sanitizers = {
name: {
trim: true
},
email: {
normalizeEmail: {
options: {
all_lowercase: true
}
}
}
};
const ContactValidator = ProxyValidator(validators, sanitizers);
const contact = ContactValidator();
contact.name = 'Mike';
contact.name = ' MICHAEL';
console.log(contact);
Validators
The validation is based on the amazing lib validator
by chriso. Find the complete list of available validators and sanitizers in here.