Skip to content
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

feat: @IsStrongPassword() converter #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

invakid404
Copy link

@invakid404 invakid404 commented Feb 9, 2023

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 :^)

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.

1 participant