validator.js
validator.js copied to clipboard
[imporve] isStrongPassword
In the isStrongPassword code you are checking if each type of the character and it's count and then comparing it with the minimum value but it will much more help if you also share the count value of each character is returned. In that case we can use it and make a much more vast error messages for missing the particular character.
If the above object is also available to use that will much help.
Hi all.
Would the team be keen in adding the following to possibly address this issue. Won't break existing functionalities and could be an additional option for individual scores.
Changes under src/lib/isStrongPassword.js
// new function
function scoreResults(analysis, scoringOptions) {
let points = {
uniqueCharsPoints: analysis.uniqueChars * scoringOptions.pointsPerUnique,
pointsPerUniqueCharRepeat: (analysis.length - analysis.uniqueChars) * scoringOptions.pointsPerRepeat,
};
if (analysis.lowercaseCount > 0) {
points.pointsForContainingLower = scoringOptions.pointsForContainingLower;
}
if (analysis.uppercaseCount > 0) {
points.pointsForContainingUpper = scoringOptions.pointsForContainingUpper;
}
if (analysis.numberCount > 0) {
points.pointsForContainingNumber = scoringOptions.pointsForContainingNumber;
}
if (analysis.symbolCount > 0) {
points.pointsForContainingSymbol = scoringOptions.pointsForContainingSymbol;
}
return points;
}
export default function isStrongPassword(str, options = null) {
assertString(str);
const analysis = analyzePassword(str);
options = merge(options || {}, defaultOptions);
if (options.returnScore) {
return scorePassword(analysis, options);
}
if (options.returnIndividualScores) { // <---------------------------- new if block using new function
return scoreResults(analysis, options);
}
return analysis.length >= options.minLength
&& analysis.lowercaseCount >= options.minLowercase
&& analysis.uppercaseCount >= options.minUppercase
&& analysis.numberCount >= options.minNumbers
&& analysis.symbolCount >= options.minSymbols;
}
Could raise a PR and add tests for it if interested. Thanks.
FYI @chriso @profnandaa @ezkemboi @tux-tn
I have resolved the suggestions and added individualScoreResults function.