[英]Sanitize email using express-validator only if it is an email, otherwise ignore
[英]Problem in email validation when using express-validator
我按照全棧課程的步驟進行操作。 此問題與 express-validator 依賴關系有關。 我配置並粘貼了傳遞給我的代碼。 而在postman中sigup url發送時出現如下錯誤信息:
{
"error": "Email must contain @"
}
但這不應該發生,因為我在 email 中插入了 @
{
"name": "dave",
"email": "dave@gmail.com",
"password": "rrrrrr9"
}
以下是我的應用程序的信息和代碼:
Express-validator 版本:^ 5.3.1
app.js 文件:
const expressValidator = require ('express-validator')
app.use (expressValidator ())
驗證器/index.js 文件:
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 ()
}
路線/user.js 文件:
const express = require('express')
const router = express.Router()
const {userSignupValidator} = require('../validator')
router.post("/signup", userSignupValidator, signup)
module.exports = router
我該如何解決?
嘗試使用這個: -
[
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() });
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.