-
-
Notifications
You must be signed in to change notification settings - Fork 622
fix: correctly run .matches when passing regex object
#1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: correctly run .matches when passing regex object
#1150
Conversation
…t reset the RegExp instance
.matches when passing regex object
src/chain/validators-impl.ts
Outdated
| typeof pattern === 'string' ? pattern : pattern.source, | ||
| modifiers, |
There was a problem hiding this comment.
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/gThere was a problem hiding this comment.
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...
fedeci
left a comment
There was a problem hiding this 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)
Hi Federico, it fixes (apart from the flags matter, that I'm fixing tomorrow) the problem I discovered (yes, I'm KikoCosmetics) in #1127. Let me know what you think...we can also chat in Italian if it helps to better understand each other :) |
|
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 |
|
Hi @fedeci I just added a commit... |
gustavohenke
left a comment
There was a problem hiding this 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
| return this.addStandardValidation.apply(this, [ | ||
| validator.matches, | ||
| ...(typeof pattern === 'string' | ||
| ? [pattern, modifiers] | ||
| : [pattern.source, ...[new Set((modifiers || '') + pattern.flags)].join('')]), | ||
| ]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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! 😉
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thank you!
There was a problem hiding this comment.
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...
|
Has this been closed for any particular reason? I'm sorry for the long delay in re-reviewing this PR! |
|
Hi I needed to delete the repo, I did some cleanup of my github account. |
|
I can create another if you guys want btw |
|
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! |
|
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! |
|
Sadly, no, it won't let me reopen this PR. :( |

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 didn't add any tests, because it works exactly as before, but simply extracts the source when passing the regex.