feat: @IsStrongPassword()
converter
#102
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is a rough implementation of a converter for
@IsStrongPassword()
. It (ab)uses positive lookahead to represent the options of the constraint in terms of a regular expression.All specified options are enforced using a similar regex pattern that looks like this:
(?=.*(?:[^...]*[...]){n})
This regular expression fragment is a positive lookahead that requires the presence of n occurrences of a certain pattern.
The pattern is defined as:
[^...]*[...]
, where the[^...]
represents any characters except for the characters within the brackets, and[...]
represents the specific characters within the brackets. The * before the first brackets means zero or more occurrences of the first pattern.Therefore, the entire pattern
(?:[^...]*[...])
matches zero or more characters, followed by the specific characters within the brackets.The lookahead
(?=.*(?:[^...]*[...]){n})
requires that this pattern occur n times in the string, without actually consuming any characters in the string. The.*
before the lookahead allows for any characters to occur before and between each occurrence of the pattern.For example, if n=3 and
[...]
is replaced with a-z, the lookahead(?=.*(?:[^a-z]*[a-z]){3})
would require three occurrences of any lowercase character in the input, possibly separated by other characters.This allows us to achieve exactly what we want: to check that certain characters are present at least a certain amount of times, without consuming the input. All of these so-called requirements are then wrapped in a basic expression that matches anything:
^.*$
.I've jumped the gun and decided that it makes sense for symbols to be configurable, as there's no universal definition for that.
Any suggestions that concern readability/maintainability are welcome :^)