express-request-validator-middlewares
v1.0.0
Published
An express middleware for validating request data using joi
Downloads
10
Maintainers
Readme
express-request-validator-middlewares
An express middleware for validating request data using joi
Installation
npm install express-request-validator-middlewares
yarn add express-request-validator-middlewares
Queck start
const express = require('express');
const Joi = require('joi');
const reqValidator = require('express-request-validator-middlewares');
const app = express () ;
const port = process.env.PORT || 4000 ;
// VLIDATE REQUEST HEADERS DATA
app.get(
'/validate-headers',
reqValidator.headers(
Joi.object().required().min(1).keys({
token : Joi.string().min(24).max(24).required()
})
) ,
( req , res , next) => res.send ( 'Headers verifyed')
)
// VLIDATE REQUEST PARAMS DATA
app.get(
'/validate-params',
reqValidator.params(
Joi.object().required().min(1).keys({
id : Joi.string().min(24).max(24).required()
})
) ,
( req , res , next) => res.send ( 'Headers verifyed')
)
// VLIDATE REQUEST QUERY DATA
app.get(
'/validate-params',
reqValidator.query(
Joi.object().required().min(1).keys({
serach : Joi.string().min(24).max(24).required()
})
) ,
( req , res , next) => res.send ( 'Headers verifyed')
)
// VLIDATE REQUEST BODY DATA
app.get(
'/validate-params',
reqValidator.body(
Joi.object().required().min(1).keys({
name : Joi.string().min(24).max(24).required(),
email : Joi.string().email().required(),
})
) ,
( req , res , next) => res.send ( 'Headers verifyed')
)
app.listen(port , () => console.log(`Lisning at port :- ${port}`));
Request error response
When validation failed user gets this response.
{
"statusCode": 400,
"ErrorMessage": "Bad request",
"ErrorType": "Request params validation failed",
"message": "id is required"
}
Change request response
You can change statusCode or ErrorMessage or add more keys in response
reqValidator.config({
statusCode:401 ,
ErrorMessage:"Validation failed",
ApiDoc:"check this API docs"
})