The package is adding custom matchers to the zxcvbn-ts package. The matchers enforce specific character requirements in passwords and provide feedback and scoring.
Please note! this package works only with zxcvbn-ts v.4 and above.
npm install zxcvbn-custom-matchers
import { ZxcvbnFactory } from '@zxcvbn-ts/core';
import {
numberMatcher,
specialMatcher,
lowercaseMatcher,
uppercaseMatcher,
minLengthMatcher,
maxLengthMatcher,
MatchersTranslations,
MatcherNames
} from 'zxcvbn-custom-matchers';
import { merge } from 'lodash';
// Add the custom matchers and their translations
const options = {
translations: merge({}, zxcvbnEnPackage.translations, MatchersTranslations)
};
const customMatchers = {
[MatcherNames.minLength]: minLengthMatcher(MIN_PASSWORD_LENGTH),
[MatcherNames.maxLength]: maxLengthMatcher(MAX_PASSWORD_LENGTH),
[MatcherNames.special]: specialMatcher,
[MatcherNames.numberRequired]: numberMatcher,
[MatcherNames.lowercase]: lowercaseMatcher,
[MatcherNames.uppercase]: uppercaseMatcher
};
const zxcvbn = new ZxcvbnFactory(options, customMatchers);
// Use zxcvbn as usual
import { zxcvbn } from '@zxcvbn-ts/core';
const result = zxcvbn.check('password123');
console.log(result);
Note: When adding a custom matcher with addMatcher, the first parameter (a string) should be the same as the matcher's pattern.
This project includes several matchers that enforce specific character requirements in passwords. Each matcher checks for a particular type of character and provides feedback and scoring.
- Pattern:
uppercase
- Purpose: Ensures the password contains at least one uppercase letter.
- Feedback: Suggests including at least one uppercase letter if missing.
- Scoring: Returns a score of
1
if missing uppercase letters.
- Pattern:
lowercase
- Purpose: Ensures the password contains at least one lowercase letter.
- Feedback: Suggests including at least one lowercase letter if missing.
- Scoring: Returns a score of
1
if missing lowercase letters.
- Pattern:
number
- Purpose: Ensures the password contains at least one number.
- Feedback: Suggests including at least one number if missing.
- Scoring: Returns a score of
1
if missing numbers.
- Pattern:
special
- Purpose: Ensures the password contains at least one special character (e.g., !, @, #, $, etc.).
- Feedback: Suggests including at least one special character if missing.
- Scoring: Returns a score of
1
if missing special characters.
- Pattern:
minLength
- Purpose: Ensures the password meets the minimum length requirement.
- Feedback: Suggests the password must be at least the specified length if shorter.
- Scoring: Returns a score of
1
if the password is shorter than the specified length.
- Pattern:
maxLength
- Purpose: Ensures the password meets the max length requirement.
- Feedback: Suggests the password must be at least the specified length if longer.
- Scoring: Returns a score of
1
if the password is longer than the specified length.