0

Can someone take a look at this code? Apparently they are not equivalant.

This was my original code which did not behave as I want to. Basically, what I want is to validate the variable only when it is defined. Ignore the logs. They were for debugging. This does not work as expected

let { fullName, email, password } = userInformation;

  if(email){
    console.log("EMAIL IS DEFINED");
    if(!validator.isEmail(email)) {
      errors.email = "Please enter a valid email address";
    }
  }

  if(fullName){
    console.log("FULLNAME IS DEFINED");
    if(validator.isEmpty(fullName)){
      errors.fullName = "Fullname is required";
    }
  }

  if(password){
    console.log("PASSWORD IS DEFINED");
    if(validator.isEmpty(password)){
      errors.password = "Password is required";
    }
  }

This works as expected. Why is it so?

let { fullName, email, password } = userInformation;

  if(!email){
    console.log("EMAIL IS DEFINED");
    if(!validator.isEmail(email)) {
      errors.email = "Please enter a valid email address";
    }
  }

  if(!fullName){
    console.log("FULLNAME IS DEFINED");
    if(validator.isEmpty(fullName)){
      errors.fullName = "Fullname is required";
    }
  }

  if(!password){
    console.log("PASSWORD IS DEFINED");
    if(validator.isEmpty(password)){
      errors.password = "Password is required";
    }
  }

Edit: Basically what i want to happen is to run the validations if the variable is DEFINED. What's currently is that its not validating unless I do like the second example

  • What's the problems you're facing with the first approach? – motanelu Apr 19 '17 at 14:17
  • 1
    It sounds like `fullName, email, password` are **undefined**. Which is why the second "approach" works. – evolutionxbox Apr 19 '17 at 14:17
  • you know what exclamation mark is used for right? so if it works with it, maybe you can try to log variables just after userInformation line .. – moped Apr 19 '17 at 14:18
  • Take a look at this: http://stackoverflow.com/questions/4686583/can-someone-explain-this-double-negative-trick – Levarne Sobotker Apr 19 '17 at 14:27
  • @LevarneSobotker is using double negative considered bad practice in javascript? –  Apr 19 '17 at 14:30
  • The reason why i added that comment above was to establish to you that, when your values are undefined, the if statement will evaluate it as undefined. But if you use the ! (logical not), you now convert that undefined value to a boolean, that is why it works. And that is why we always say if (name && name !== undefined) – Levarne Sobotker Apr 21 '17 at 22:49

1 Answers1

3

Your checks don't check for undefined, they check for falsiness. "" is falsy. So are 0, NaN, null, undefined, and of course, false.

Since "" is falsy, this doesn't work if userInformation has a fullName property with the value "":

if(fullName) {
  console.log("FULLNAME IS DEFINED");
  if(validator.isEmpty(fullName)){
    errors.fullName = "Fullname is required";
  }
}

You'll never go into that outer if, so you'll never use your validator to check fullName.

If you want to bypass your validators if the value is undefined, specifically (because the property didn't exist on userInformation, or it existed but had the value undefined), you need to be specific:

if (fullName !== undefined) {
  console.log("FULLNAME IS DEFINED");
  if(validator.isEmpty(fullName)){
    errors.fullName = "Fullname is required";
  }
}

If you want to bypass your validators only if userInformation didn't have the property at all (but keep using the validator if it did have it with the value undefined, then you need to check specifically for that:

if ("fullName" in userInformation) { // or `if (userInformation.hasOwnProperty("fullName")) {` if you only want an own property
  console.log("FULLNAME IS DEFINED");
  if(validator.isEmpty(fullName)){
    errors.fullName = "Fullname is required";
  }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Sorry where does `""` come from here? I thought I was destructuring the variables? oh I misread. This makes sense now. Thanks! –  Apr 19 '17 at 14:27