Skip to content

Conversation

@tonysamperi
Copy link
Contributor

@tonysamperi tonysamperi commented May 23, 2022

Forcing pattern to be string, to cope with lib validator which doesn't reset the RegExp instance
Fixes #1127

Description

Fixes matches validation failing one other time, passing regex value with g flag.

  • I have added tests for what I changed.

I didn't add any tests, because it works exactly as before, but simply extracts the source when passing the regex.

  • This pull request is ready to merge.

@fedeci fedeci changed the title Fix #1127 fix: correctly run .matches when passing regex object May 23, 2022
Comment on lines 338 to 339
typeof pattern === 'string' ? pattern : pattern.source,
modifiers,
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this counter-intuitive?
I mean this means that a user specifying a regex object with flags need to also set the optional modifiers separately
e.g.

  .matches(/foo/g) // => becomes /foo/
  .matches(/foo/g, 'g') // => is correctly /foo/g

Copy link
Contributor Author

@tonysamperi tonysamperi May 23, 2022

Choose a reason for hiding this comment

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

Hi, yes, you're right! I forgot about the flags...I'll fix it tomorrow...

Copy link
Member

@fedeci fedeci left a comment

Choose a reason for hiding this comment

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

Hey @tonysamperi thanks for the PR, I am unsure how this fixes the linked issue, validator.js correctly handles passed in regex objects (ref)

@coveralls
Copy link

coveralls commented May 23, 2022

Coverage Status

Coverage remained the same at 100.0% when pulling c803450 on tonysamperi:bugfix/regex-instance into 82e4d84 on express-validator:master.

@tonysamperi
Copy link
Contributor Author

tonysamperi commented May 23, 2022

Hey @tonysamperi thanks for the PR, I am unsure how this fixes the linked issue, validator.js correctly handles passed in regex objects (ref)

Hi Federico, it fixes (apart from the flags matter, that I'm fixing tomorrow) the problem I discovered (yes, I'm KikoCosmetics) in #1127.
Basically when you pass a RegExp instance to express-validator, its instance gets preserved.
I noticed that in lib "validator", if pattern is string, a new RegExp gets instantiated, but if it's not (which is the case), it gets used as it is. Which means that after the first match, the lastIndex on the instance won't be 0, causing the second test to fail (one test passes, one fails...and so on...).
The idea of my fix was passing always a string, so that the test is done with a fresh RegExp instance every time.
This for sure is how express-validator should be intended to work.
I don't know if the behaviour of lib "validator" should be considered faulty.

Let me know what you think...we can also chat in Italian if it helps to better understand each other :)

@fedeci
Copy link
Member

fedeci commented May 24, 2022

Don't worry I got that later, I totally missed that explanation in the issue! I've also already updated the validatorjs issue to ask them to start using .match. Thanks for the fix!

@tonysamperi
Copy link
Contributor Author

tonysamperi commented May 24, 2022

Hi @fedeci I just added a commit...
I had to do something to merge regex flags with additional modifiers...
That's the cleanest way I could think of...
How does it look?
Just had to do some other fixing after running the standard-validation.spec...now it runs fine!

image

Copy link
Member

@gustavohenke gustavohenke left a comment

Choose a reason for hiding this comment

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

Looks pretty solid and flexible to me 🙂 happy if @fedeci is

Comment on lines 336 to 341
return this.addStandardValidation.apply(this, [
validator.matches,
...(typeof pattern === 'string'
? [pattern, modifiers]
: [pattern.source, ...[new Set((modifiers || '') + pattern.flags)].join('')]),
]);
Copy link
Member

Choose a reason for hiding this comment

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

Just a super nitpick, can we use a ternary expression or an if statement, instead of the apply? That's definitely correct but makes the code harder to read for new developers.

Copy link
Member

Choose a reason for hiding this comment

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

And can you add a couple tests to check that the flags added after the regex are effectively passed to the addStandatdValidation function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem for the tests,
But removing the apply would mean duplicate entirely the call to addStandardValidation...
I'd say that new developers shouldn't be supposed to touch such a piece of code!
I was reluctant myself despite several years of experience! 😅
Also remember that if they fix validator library we can revert this change! 😉

Copy link
Member

Choose a reason for hiding this comment

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

That's a nice opinion and I'm fine to keep it as is, I'll open a PR and push to get that merged in asap!

Copy link
Member

Choose a reason for hiding this comment

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

And can you add a couple tests to check that the flags added after the regex are effectively passed to the addStandatdValidation function?

An even better testing way would be to reproduce the same regex in the issue, calling the method twice and asserting that the validation is passed both times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I'll add the tests tomorrow!
Thanks!!

Copy link
Member

Choose a reason for hiding this comment

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

Great! Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good thing you asked for the tests...The spread was misplaced...

@tonysamperi tonysamperi closed this Jun 7, 2022
@tonysamperi tonysamperi deleted the bugfix/regex-instance branch June 7, 2022 19:15
@fedeci
Copy link
Member

fedeci commented Jun 8, 2022

Has this been closed for any particular reason? I'm sorry for the long delay in re-reviewing this PR!

@tonysamperi
Copy link
Contributor Author

Hi I needed to delete the repo, I did some cleanup of my github account.
At this point maybe they'll fix on validator.js side and we won't need this!

@tonysamperi
Copy link
Contributor Author

I can create another if you guys want btw

@fedeci
Copy link
Member

fedeci commented Jun 8, 2022

I see it really though they will fix soon, they are likely slower then us because they tend to wait to merge all PRs at once. If you prefer I can create this PR from your commit again tomorrow (you will get the commit credits so it should be fine). Let me know!

@tonysamperi
Copy link
Contributor Author

Don't worry I'll do some magic and maybe it will let me reopen this (I don't know if I can trick github by using the same branch name 😂)

Bye!

@tonysamperi
Copy link
Contributor Author

Sadly, no, it won't let me reopen this PR. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.matches failing when passing regex instead of pattern

4 participants