class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: Unstable Unicode Regex

Open incetarik opened this issue 2 years ago • 4 comments

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 Example 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.

Example

incetarik avatar Jul 31 '22 20:07 incetarik

Could you try to set up a minimal reproduction of this behaviour in runkit or something similar?

braaar avatar Aug 16 '22 06:08 braaar

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.

incetarik avatar Aug 19 '22 22:08 incetarik

Damn, this is really something. I'm not very knowledgeable about regex, so I don't have the faintest idea why this is happening.

braaar avatar Aug 23 '22 10:08 braaar

~~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.

braaar avatar Aug 23 '22 10:08 braaar

Any news about this @braaar ?

incetarik avatar Sep 23 '22 10:09 incetarik

This is an upstream issue in validator.js, it seems

braaar avatar Sep 23 '22 11:09 braaar

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 avatar Nov 15 '22 10:11 braaar

@braaar don't forget to remove the needs triage label when we discovered what is the root cause of the report.

NoNameProvided avatar Nov 15 '22 11:11 NoNameProvided

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.

github-actions[bot] avatar Dec 16 '22 00:12 github-actions[bot]