Home > Software engineering >  Email validation is not working in node js
Email validation is not working in node js

Time:12-04

I have written code to perform email validation and password validation, but it is not returning any error if condition is not satisfied, didn't get any idea what is wrong with my code.

code in validation.js file

const { check } = require('express-validator');
 
exports.signupValidation = [
    check('name', 'Name is requied').not().isEmpty(),
    check('email', 'Please include a valid email').isEmail().normalizeEmail({ gmail_remove_dots: true }),
    check('password', 'Password must be 6 or more characters').isLength({ min: 6 })
]
 

Code in router.js

const express = require('express');
const router = express.Router();
const db  = require('./dbConnection');
const { signupValidation, loginValidation } = require('./validation');
const { validationResult } = require('express-validator');

router.post('/register', signupValidation, (req, res, next) => {
// already return in validation.js file to throw error, what is the exact way to return error message
});

CodePudding user response:

router.post('/register', signupValidation, (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
    return res.status(422).json({
        status: "failure",
        error: {
            message: "Invalid data to proceed",
            type: "ValidationException",
            code: 422,
            error_data: errors.array(),
        },
    });
}
next();

});

  • Related