0

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
});
1
  • "but it is not returning any error if condition is not satisfied" — Which condition? For what input? If it isn't returning an error, what is it doing? Commented Dec 2, 2021 at 15:01

1 Answer 1

1
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();

});

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't get why we need to call againvalidationResult(req) already we are checking validation in validation.js
Can u please explain little bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.