am-i-right v1.0.5
Am I Right
"Am I Right" is a Object Schema Validator plug and use package which is helpful for mantaining single source of validation logic for front-end.
Managing validation schema and custom error messages is straight forward using this plug-in rather maintaing seperate validation logics in each and every component.
As the name of the package tells you, that you just need to ask whether you are doing is right or wrong, package will validate and let you know what's wrong!!!!!
Installation
Use the package manager npm to install Am I Right.
npm install am-i-right
Usage
import {validator} from 'am-i-right';
Need to feed two things for the plug-in which is necessary for performing validation.
- Validation schema
- Object data need to get validated against the validation schema
- Validation Schema should be defined like below, where we can give our custom error messages for the fields.
let validationSchema = {
id: {
minLength: 3,
maxLength: 20,
required: true,
onlyNumbers: false,
onlyAlpha: true,
errorMessages: {
errorMinLength: "Min length should be 3",
errorMaxLength: "Max length should be 20",
errorRequired: "Please fill this field",
errorOnlyNumbers: "ID should be only numbers",
errorOnlyAlpha: "ID should contain only alphabets"
}
}
};
As we observed in the above schema, using this plug-in we can validate against minLength, maxLength, required, onlyAlpha, onlyNumber
where the properties we define in schema is case sensitive.
We can define the respective error messages for each category in an errorMessages
object, and we need to make sure that we are using the same properties as in schema.
- Object data : The data object you need to validate against the validator.
let data = { id: "abcd" };
And finally you can call our validator function which will do the magic for you.
validator(validationSchema, data)