2

Reviewing the class-validator ValidatorConstraint implementation and it has a section that looks like this:

        name = target.name;
        if (!name) // generate name if it was not given
            name = name.replace(/\.?([A-Z]+)/g, (x, y) => "_" + y.toLowerCase()).replace(/^_/, "");

Does the !name part make sense? For example console.log(!"") logs true, so it could be that name is an empty string and console.log(!undefined) is also true.

So my hypothesis is that the code block inside the `if (!name) block will never make sense because there will never be a string to perform replacements on inside the block?

Ole
  • 41,793
  • 59
  • 191
  • 359
  • I am puzzled by your question, do you not understand how falsy values work or what? Here's a post that might help https://stackoverflow.com/questions/21206207/javascript-falsy-values-null-undefined-false-empty-string-or-and-0-a – Coder-Man Jun 17 '18 at 20:48
  • 4
    `!name` makes sense, but using `name.replace` on falsy name doesn't ... seems like a bug – Slai Jun 17 '18 at 20:50
  • !"" would enter the if block. Then we would try to replace parts of the string using the regular expression. With "" there's nothing to replace ... – Ole Jun 17 '18 at 20:50
  • If u explicitly want to check for a `truthy` value, you should consider `!!! ` since, it converts all types to an appropriate boolean type – Abrar Hossain Jun 17 '18 at 21:59
  • null.replace()? undefined.replace()? Sorry... – atmin Jun 17 '18 at 22:00
  • Yeah I think it's a bug ... just wanted to double check here before reporting it...but now its reported https://github.com/typestack/class-validator/issues/221 – Ole Jun 17 '18 at 22:10

0 Answers0