bunk-manager-mern
bunk-manager-mern copied to clipboard
Add validator to every required field in the form like Email, username, password etc using express-validator
Write validator function as middleware. It should be before the actual controller function in every routes that take input from client side.
For example -
Email - should be email with proper formate Username - should be unique and at-least 6 character long Password - should be 8 character long which include at-least 1 special character, 1 capital letter Semester - should not be greater than 8
and so on.
Have any doubt feel free to ask her. Good Luck
@Mustafiz04 i would like to work on this issue.
Yes you can @Muditxofficial I am assigning to you.
@Mustafiz04 What should i do about error messages should i just console log it. Also, i have to use express-validator right just to confirm as i need to install it .
@Muditxofficial return the message in similar way we return the data that can be used by frontend to display that message. Yes, use express-validator.
https://express-validator.github.io/docs/check-api.html for reference
don't forget to use it as middleware.
@Muditxofficial How you work going on ??
@Mustafiz04 I have started doing login but on which route should i check it.
http://localhost:3000/api/auth/login does not work
Sorry for late. Wait I am explaining you.
Validator file
const { check, validationResult } = require('express-validator');
exports.validateSignupRequest = [
check('name').notEmpty().withMessage("First name is required"),
check('email').isEmail().withMessage("Email is required"),
check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
];
exports.isRequestValidated = (req, res, next) => {
const errors = validationResult(req);
if( errors.array().length > 0 ){
return res.status(400).json({
errors : errors.array()[0]
})
}
next();
}
in route file
const { validateSignupRequest, isRequestValidated } = require('./../../middleware/validator/auth');
router.post('/signup', validateSignupRequest, isRequestValidated, signup );
Apply to all route with take input from frontend.
@Mustafiz04 Is there a way to check if my implementation is right using postman ? i found two different url localhost:3000/user/signup and localhost:3000/api/auth/new/signup
yes you can check every API on postman.
localhost:3000/api/auth/new/signup
this is correct
I give only example how to write validator and apply that to route as middleware.
Follow that ways, create and apply middleware to every route.
When will you complete this task because it take lots of time.
Validator
const { check, validationResult } = require('express-validator');
exports.validateSignupRequest = [
check('name').notEmpty().withMessage("First name is required"),
check('email').isEmail().withMessage("Email is required"),
check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
];
exports.validateLogin = [
check('email').isEmail().withMessage("Email is required"),
check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
]
exports.isRequestValidated = (req, res, next) => {
const errors = validationResult(req);
if( errors.array().length > 0 ){
console.log(errors.array()[0])
return res.status(400).json({
errors : errors.array()[0]
})
}
next();
}
Routes
const { validateSignupRequest, isRequestValidated, validateLogin } = require("../middlewares/validators")
router.post('/api/auth/login',isRequestValidated,validateLogin,loginUser);
router.post('/api/auth/new/signup',validateSignupRequest,isRequestValidated,createUser);
Is it ok or should i add something?
correct but isRequestValidated
always after validator function like validateLogin