I followed in the steps of a fullstack course. This problem is related to the express-validator dependency. I configured and pasted the code that was passed to me. And in the postman when sending in the sigup url, the following error message appeared:
{
"error": "Email must contain @"
}
But that wasn’t supposed to happen, because I had inserted the @ in the email
{
"name": "dave",
"email": "dave@gmail.com",
"password": "rrrrrr9"
}
Here are the information and codes for my application:
Express-validator version: ^ 5.3.1
app.js file:
const expressValidator = require ('express-validator')
app.use (expressValidator ())
validator/index.js file:
exports.userSignupValidator = (req, res, next) => {
req.check ('name', 'Name is required'). notEmpty ()
req.check ('name', 'Email must be between 3 to 32 characters')
.matches (/.+\@.+\..+/)
.withMessage ('Email must contain @')
.isLength ({
min: 4,
max: 32
})
req.check ('password', 'Password is required'). notEmpty ()
req.check ('password')
.isLength ({min: 6})
.withMessage ('Password must contain at least 6 characters')
.matches (/ \ d /)
.withMessage ("Password must contain a number")
const errors = req.validationErrors ()
if (errors) {
const firstError = errors.map (error => error.msg) [0]
return res.status (400) .json ({error: firstError})
}
next ()
}
routes/user.js file:
const express = require('express')
const router = express.Router()
const {userSignupValidator} = require('../validator')
router.post("/signup", userSignupValidator, signup)
module.exports = router
How can I solve it?
Try using this :-
[
check("name", "Name is required").not().isEmpty(),
check("email", "Please enter valid email").isEmail(),
check(
"password",
"Please enter valid password with 6 or more characters"
).isLength({ min: 6 }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}