Describe the bug
I'm trying to use the body function to validate some form inputs that my express app receives, but it seems that the isEmpty validator is not working as expected. When I provide a value to firstName (a non-empty string) in the example below, the validator actually throws the error as if the string was empty.
const { validationResult } = require("express-validator");
class HomeController {
constructor() {}
create(req, res) {
return res.render("home", {
pageTitle: "Home Page",
errors: [],
lastInput: " ",
});
}
store(req, res) {
const { firstName, email } = req.body;
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors.array());
return res.status(422).render("home", {
pageTitle: "Home Page",
errors: errors.array(),
lastInput: { firstName: firstName, email: email },
});
}
}
}
module.exports = new HomeController();
const express = require("express");
const router = express.Router();
const compression = require("compression");
const HomeController = require("../controllers/HomeController");
const validateMiddleware = require("../routes/middlewares/validate");
const { body, check } = require("express-validator");
const handler = require("express-async-handler");
const app = express();
app.use(compression());
router.get("/status/health", handler(HomeController.checkHealthStatus));
router.get("/", handler(HomeController.create));
router.post(
"/form",
[
check("firstName")
.isEmpty()
.withMessage("Please provide a name."),
check("email")
.isEmail()
.withMessage("Please provide a valid email.")
.custom((value, { req }) => {
if (value === "teste@teste.com") {
throw new Error("This email address is forbidden.");
}
return true;
}),
],
handler(HomeController.store)
);
module.exports = router;
For the above example using validate I get no response from express, the page just keeps loading forever until the request times out.
If any further code is required to make this clearer to understand, please let me know. Thanks.
Additional context
Validator.js version: 6.5.0
Node.js version: v10.20.1
OS platform: macOS 10.15.4 (Catalina)
Describe the bug
I'm trying to use the
bodyfunction to validate some form inputs that my expressappreceives, but it seems that theisEmptyvalidator is not working as expected. When I provide a value tofirstName(a non-empty string) in the example below, the validator actually throws the error as if the string was empty.For the above example using
validateI get no response from express, the page just keeps loading forever until the request times out.If any further code is required to make this clearer to understand, please let me know. Thanks.
Additional context
Validator.js version:
6.5.0Node.js version:
v10.20.1OS platform:
macOS 10.15.4 (Catalina)