fix(matches): prevent regex state from breaking following validations by fedeci · Pull Request #1975 · validatorjs/validator.js

Conversation

fedeci

pattern = new RegExp(pattern, modifiers);
}
return pattern.test(str);
return !!str.match(pattern);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.test keeps the state when used along with a regex that has the global or the sticky flag. We can prevent that behaviour getting stateless validation using .match

/cc @tux-tn @ezkemboi

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using Array.isArray(str.match(pattern)) or str.match(pattern) !== null since the possible return values of .matches are Array or null?
The result is obviously the same, but semantically seems more appropriate to me :)
And maybe they'll be more inclined to accept the PR :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly find that excessively verbose and probably won't be a blocker for the pr. Let's see what the maintainers think.

@codecov

tux-tn

@tux-tn tux-tn left a comment

Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fedeci

We have published an hot fix in express-validator so we are fine to have this merged later too!

@tux-tn

Good for you, really sorry for the delay @fedeci !

profnandaa

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fedeci fedeci deleted the fix/regex-state branch

June 30, 2022 09:01

@tonysamperi

@profnandaa

This was referenced

Jun 3, 2024

This was referenced

Jun 14, 2024

This was referenced

Jun 20, 2024

This was referenced

Jul 10, 2024

This was referenced

Jul 19, 2024

This was referenced

Aug 30, 2024

Labels