lexx-data-validator
v0.3.0
Published
![GitHub package.json version](https://img.shields.io/github/package-json/v/lexxyar/lexx-data-validator) ![GitHub](https://img.shields.io/github/license/lexxyar/lexx-data-validator) ![GitHub all releases](https://img.shields.io/github/downloads/lexxyar/le
Downloads
12
Readme
lexx-data-validator
This is simple data validator
Installation
npm i lexx-data-validator
Usage
import {DataValidator, DataValidatorRuleSchemaMap, ValidationRule} from "lexx-data-validator";
const data = {
name: 'John Doe',
age: 10,
email: 'jdoe@[email protected]',
gender: ''
}
const schema: DataValidatorRuleSchemaMap = {
email: ValidationRule().email(),
gender: ValidationRule().required(),
age: ValidationRule().min(12)
}
const oValidator = new DataValidator()
oValidator.schema(schema).validate(data);
console.log(oValidator.getErrors())
Result is:
[
{key: 'age', message:'10 should be greater than or equal to 12'},
{key: 'email', message:'jdoe@[email protected] is not valid'},
{key: 'gender', message: 'gender is required'}
]
Features
Since v 0.2.0 you can add message for errored values like shown below
const schema: DataValidatorRuleSchemaMap = {
email: ValidationRule().email('E-mail is invalid'),
gender: ValidationRule().required('Gender is required'),
age: ValidationRule().min(12, 'Minimum value is 12')
}
Change log
Version|Changes ---|--- 0.3.0|Reorganized error structure 0.2.0|Added messages for error 0.1.3|If field is required and it not pass with data object - it is an error 0.1.2|string is deprecated. Use alpha method instead 0.1.1|min, max, email, string, number, required