'Cannot create property 'ignore_whitespace' on string ' '

const Validator = require("validator");
const isEmpty = require("../validation/is-empty");

module.exports = function validateRegisterInput(data) {
  let errors = {};

  data.name = !isEmpty(data.name) ? data.name : " ";
  data.email = !isEmpty(data.email) ? data.email : " ";
  data.password = !isEmpty(data.password) ? data.password : " ";
  data.password2 = !isEmpty(data.password2) ? data.password2 : " ";

  if (!Validator.isLength(data.name, { min: 3, max: 30 })) {
    errors.name = "Name must be between 3 and 30 characters";
  }
  if (Validator.isNumeric(data.name)) {
    errors.name = "No numbers please!";
  }
  if (Validator.isEmpty(data.name)) {
    errors.name = "Cannot be empty!";
  }
  if (Validator.isEmpty(data.email)) {
    errors.email = "Cannot be empty!";
  }
  if (Validator.isEmail(data.email)) {
    errors.email = "Email is invalid!";
  }
  if (Validator.isEmpty(data.password)) {
    errors.password = "Cannot be empty!";
  }
  if (Validator.isLength(data.password, { min: 6, max: 30 })) {
    errors.password = "Password must be between 6 and 30 characters!";
  }
  if (Validator.isEmpty(data.password, data.password2)) {
    errors.password2 = "Passwords must match!";
  }
  if (Validator.equals(data.password2)) {
    errors.password2 = "Cannot be empty!";
  }
  return {
    errors,
    isValid: isEmpty(errors),
  };
};

Hi, guys!

I am trying to add validations making sure you can't submit empty strings for name, email and passwords but I get TypeError: Cannot create property 'ignore_whitespace' on string ' '

TypeError: Cannot create property 'ignore_whitespace' on string ' '
    at merge (\node_modules\validator\lib\util\merge.js:14:16)
    at Object.isEmpty (\node_modules\validator\lib\isEmpty.js:20:32)
    at validateRegisterInput (\config\validation\register.js:33:17)
    at router.post (\routes\api\users.js:24:31)
    at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
    at next (\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
    at node_modules\express\lib\router\index.js:281:22
    at Function.process_params (\node_modules\express\lib\router\index.js:335:12)
    at next (\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (\node_modules\express\lib\router\index.js:174:3)
    at router (\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (\node_modules\express\lib\router\index.js:317:13)
    at node_modules\express\lib\router\index.js:284:7

What am I missing here?

I am receiving the error when I do a POST request with Postman.



Solution 1:[1]

I had the same issue and the problem was with the returning error message. I was writing it in the isEmpty function!

Wrong Code

check("gender")
 .not()
 .isEmpty('Gender is a required field.')

Correct Code

check("gender")
 .not()
 .isEmpty()
 .withMessage('Gender is a required field.')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 efi poor