2.0.5 • Published 2 years ago
@wepost/validator v2.0.5
This is a validation library that examines multiple rules on one or more fields. It is also possible to use the "and","or" operators.
Installation and Usage
npm install @wepost/validator --save
Create an instance of the class and add one or more fields to it.
const emokValidator = new EmokValidator();
emokValidator.addField({
fieldName: 'firstName',
type: 'string',
value: "Mokhtar",
op:"and" // One rule is enough or all must be TRUE.
validators: [
// We can define a sync or async custom function
{
validator:(v)=>{
return v.length >5 ? true : false;
},
err: 'custom error'
},
// We can define an asynchronous custom function
{
validator:async(v)=>{
const name = await getDbName(v);
return v===name?true : false;
},
err: 'custom error'
},
// We can use the functions available in the Validatorjs or Validatejs or PersianTools library.
{
validator: 'isAlpha',
options: ['fa-IR', { ignore: "-_، آ.," }],
err: err: 'custom error for isAlpha '
}
],
typeErr: 'custom error for type error'
});
emokValidator.addField({
fieldName: 'age',
type: 'number',
value: 35,
typeErr: 'age must be number type'
})
const results = await emokValidator.run();