validatorexample
v1.0.0
Published
Custom Validator
Downloads
1
Readme
Project Title
Custom Validator
GETTING STARTED
Installation
npm install validator
Adding to the nodejs module
let validator = require('validator');
###EXAMPLE
Validating Email
validationMethod.Email({ key: "sEmail", value: req.body.sEmail, msg: "Invalid Email" });
Validating Email
validationMethod.NotEmpty({ key: "sEmail", value: body.sEmail, msg: "Email is Required" });
Validating Email
validationMethod.UserName({ key: "sDisplayName", value: body.sDisplayName, msg: "Invalid DisplayName" });
Validating Email
validationMethod.NotEmpty({ key: "sDisplayName", value: body.sDisplayName, msg: "sDisplayName is Required" });
Validating Email
validationMethod.MinMaxChar({ key: "sDisplayName", value: body.sDisplayName, min: 3, max: 6, msg: "sDisplayName is out of range" });
Validating Email
validationMethod.MinMaxNumber({ key: "number", value: body.number, min: 5, max: 10, msg: "sDisplayName is out of range" });
Get All Errors
let VErros = validationMethod.printAllErrors();
if (VErros != {}) return res.json(VErros);
else res.json("Process ahead");
OPTIONS
Options are passed to the function in an object format
- [key] - key of the body parameter to be validate
- [value]- value of the body parameter to be validate
- [min] - Minimum length or value passed to match the condition
- [max] - Maximum length or value passed to match the condition
- [msg] - Message that should be returned id not validated
CODE EXAMPLE
app.post('/validate',(req,res,next) => {
validationMethod.Email({ key: "sEmail", value: req.body.sEmail, msg: "Invalid Email" });
validationMethod.NotEmpty({ key: "sEmail", value: req.body.sEmail, msg: "Email is Required" });
validationMethod.UserName({ key: "sDisplayName", value: req.body.sDisplayName, msg: "Invalid DisplayName" });
validationMethod.NotEmpty({ key: "sDisplayName", value: req.body.sDisplayName, msg: "sDisplayName is Required" });
validationMethod.MinMaxChar({ key: "sFirstName", value: req.body.sFirstName, min: 3, max: 6, msg: "sFirstName is out of range" });
validationMethod.MinMaxNumber({ key: "nAge", value: req.body.nAge, min: 18, max: 28, msg: "nAge is not validated" });
let VErros = validationMethod.printAllErrors();
if (VErros != {}) return res.json(VErros);
return next();
},(req,res) => {
res.json("validated data");
})
Authors
- Shiavm Mishra - Full Stack Developer
License
This project is licensed under the ISC License