class-validator
class-validator copied to clipboard
fix: Unstable Unicode Regex
Description
I'm using NestJS, there I have validations which are done through this library.
I have decorated a class property with @Matches
with a regex which has ug
flags.
The problem is that this validator is returning true
and false
in order, repeatedly, and not stable.
Producing true
and false
(per following request) for the exact same string input.
To show you the exact place that I have caught, I'll share a picture to you so you can directly jump to the line there and test by yourselves.
The Regex that is used:
/^\p{Alpha}{2,}[\p{Alpha}\s]{1,57}$/ug
The image
The link if this is not visible to you.
The same image file is also attached here.
class InputDto {
@IsString()
@IsOptional()
@Length(3, 60)
@Matches(/^\p{Alpha}{2,}[\p{Alpha}\s]{1,57}$/ug, {
message: 'The input is not valid'
})
readonly fullName?: string
}
Expected behavior
Returning true
for all execution/validation.
Actual behavior
Returning true
and false
and true
and so on for each validation.
Could you try to set up a minimal reproduction of this behaviour in runkit or something similar?
Could you try to set up a minimal reproduction of this behaviour in runkit or something similar?
Here it is @braaar, please check this out and play with it as you wish.
Damn, this is really something. I'm not very knowledgeable about regex, so I don't have the faintest idea why this is happening.
~~I tried to put your regex in Regex101 and it says it's an invalid expression. What's up with that?~~
Nevermind, I'm dumb.
Any news about this @braaar ?
This is an upstream issue in validator.js, it seems
I don't see how we would fix this in this project. We are just calling an upstream validation function.
This should be fixed in validator.js. See validator.js#1768.
For now you can implement your own custom regex validator that uses the fix described in the comments to that issue.
@braaar don't forget to remove the needs triage label when we discovered what is the root cause of the report.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.