GithubHelp home page GithubHelp logo

Add validator to every required field in the form like Email, username, password etc using express-validator about bunk-manager-mern HOT 13 OPEN

adre9 avatar adre9 commented on August 22, 2024
Add validator to every required field in the form like Email, username, password etc using express-validator

from bunk-manager-mern.

Comments (13)

Muditxofficial avatar Muditxofficial commented on August 22, 2024

@Mustafiz04 i would like to work on this issue.

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

Yes you can @Muditxofficial
I am assigning to you.

from bunk-manager-mern.

Muditxofficial avatar Muditxofficial commented on August 22, 2024

@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 .

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

@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.

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

@Muditxofficial How you work going on ??

from bunk-manager-mern.

Muditxofficial avatar Muditxofficial commented on August 22, 2024

@Mustafiz04 I have started doing login but on which route should i check it.
github2
http://localhost:3000/api/auth/login does not work

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

Sorry for late.
Wait I am explaining you.

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

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.

from bunk-manager-mern.

Muditxofficial avatar Muditxofficial commented on August 22, 2024

@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

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

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.

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

When will you complete this task because it take lots of time.

from bunk-manager-mern.

Muditxofficial avatar Muditxofficial commented on August 22, 2024

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?

from bunk-manager-mern.

Mustafiz04 avatar Mustafiz04 commented on August 22, 2024

correct but isRequestValidated always after validator function like validateLogin

from bunk-manager-mern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.