js form validator
import { Validator } from './Validator';
const data = {
name: ``,
age: 23,
};
const fieldChecks = [
{
name: `Name`,
field: `name`
check: `required`
},
{
name: `Age`,
field: `age`,
check: `greatorThanZero`,
},
];
let errors = [];
//use the validator class
(new Validator(data, fieldChecks).validate()).then(() => {
//successfull
//perform your request in here
}).catch(errors => {
errors = errors;
});
- required : Required field.
- confirmed : Field that requires confirmation.
- greatorThanZero : Field should be greator than zero eg: price, age
- ArrayHasItems : Array if the length is greator than zero
- shouldContainAtmost2Letters : custom validator to check if the string has at most 2 letters