Validator.js: Validator isEmpty not working properly

Created on 2 Jun 2020  路  6Comments  路  Source: validatorjs/validator.js

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 === "[email protected]") {
          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)

馃悰 bug 馃攳needs-investigation

Most helpful comment

Hey, sorry for the late response. I actually managed do solve this by using notEmpty, which is not currently listed on validator docs . I managed to find it here. Thanks.

All 6 comments

I think you should file this with the express-validator team.

Are you sure? Because the express-validator documentation clearly refers to this repo, as the isEmpty validator function was built here, and not in theirs.

PS.: Please check the snippets above again as I updated them. Actually, the isEmpty validator is throwing an error when the string is not empty like I mentioned above.

Ok, please give examples directly without express-validator so that it's easy to figure out where the problem is.

@rodmirsantana What is the error isEmpty throws? In this codebase, isEmpty itself only throws an error if the first argument is not a string.

Maybe you could console.log whatever is in the firstName/email fields to be certain that it is an empty string and post that here instead of making a whole new example.
Either way, if it's not a TypeError: Expected string but received (type), then that error is probably not in validator.js.
(Also, if I'm remembering correctly, express might not include a field for firstName in the body object if in that form submission the field was empty? You might want to use exists() instead)

Hey, sorry for the late response. I actually managed do solve this by using notEmpty, which is not currently listed on validator docs . I managed to find it here. Thanks.

Cool, thx

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RPDeshaies picture RPDeshaies  路  4Comments

jaxkodex picture jaxkodex  路  3Comments

philfreo picture philfreo  路  3Comments

AtomicBorg picture AtomicBorg  路  3Comments

rathboma picture rathboma  路  4Comments