loki-validator
v1.1.3
Published
A validation package for Express
Downloads
14
Maintainers
Readme
loki-validator
A simple request validator utility for validating request body , query and parameters.
Installation
You can install the package using npm:
npm install loki-validator
Usage
const validator = require('loki-validator');
// body
app.post('/example', (req, res) => {
const validatorObj = new validator(['username', 'email', 'password']);
const isValid = validatorObj.validateRequestBody(req);
if (!isValid) {
return res.status(400).json({ error: 'Invalid request body' });
}
});
// params
app.get('/example/:id/:name', (req, res) => {
const validatorObj = new validator(['id','name']);
const isValid = validatorObj.validateRequestParams(req);
if (!isValid) {
return res.status(400).json({ error: 'Invalid request parameters' });
}
});
// query
app.get('/example/', (req, res) => {
const validatorObj = new validator(['search','limit','page']);
const isValid = validatorObj.validateRequestQuery(req);
if (!isValid) {
return res.status(400).json({ error: 'Invalid Query parameters' });
}
});