validatar
Javascript data validator.
A simple way to validate date and customerize the constraint.
Installation
npm install validatar
Links
npm package
Github page
async-validatar
Usage
Example
const Validatar = require('validatar');
Validatar.register('required', '%{key} is required', (v) => v !== undefined);
Validatar.register('isString', '%{key} is not a string. value:%{value} position:%{position}', (v) => (typeof (v) === 'string'));
const data = {
str1: 'I am a string!',
obj1: {
str2: 456,
},
};
const rule = {
str1: [{ constraint: 'isString', message: 'str1 must be string' }],
obj1: {
str2: ['required', 'isString'],
},
};
const result = Validatar.validate(data, rule);
console.log(result);
Pass parameters to check function
const Validatar = require('validatar');
Validatar.register('isString', '%{key} is not a string. value:%{value} position:%{position}', (v) => (typeof (v) === 'string'));
Validatar.register('checkLength', '%{key} length error.', (v, params) => (v.length >= params[0] && v.length <= params[1]));
const data = {
str1: 'I am a string!',
};
const rule = {
str1: [
'isString', {
constraint: 'checkLength',
params: [5, 12],
},
],
};
const result = Validatar.validate(data, rule);
console.log(result);
Main Function
Validatar.register
Validatar.register('checkLength', '%{key} length error.', (v, params) => (v.length >= params[0] && v.length <= params[1]));
Validatar.validate
const result = Validatar.validate(data, rule);
Another Example
const Validatar = require('validatar');
Validatar.register('required', new Error('Wrong input format'), (v) => v !== undefined);
Validatar.register('isString', '%{key} is not a string. value:%{value}', (v) => (typeof (v) === 'string'));
Validatar.register('isEmail', '%{key} is not email. value:%{value}', (v) => (/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String(v).toLowerCase())));
const correctData = {
str1: 'I am a string!',
obj1: {
email: 'random@email.com',
},
};
const rule = {
str1: [{ constraint: 'required', message: 'str1 not exists' }, { constraint: 'isString', message: new Error('str1 must be string') }],
obj1: {
email: ['required', 'isString', 'isEmail'],
},
};
const correctResult = Validatar.validate(correctData, rule);
console.log(correctResult);
const wrongData = {
str1: 'I am a string!',
};
const wrongResult = Validatar.validate(wrongData, rule);
console.log(wrongResult);