validate method
Validates the given input string as a password.
Implementation
Either<PasswordError, String> validate(String input) {
if (input.isEmpty) {
return const Left(PasswordEmpty());
}
if (input.length < config.minLength) {
return Left(
PasswordTooShort(
currentLength: input.length,
minLength: config.minLength,
),
);
}
if (input.length > config.maxLength) {
return Left(
PasswordTooLong(
currentLength: input.length,
maxLength: config.maxLength,
),
);
}
if (!_isAsciiOnly(input)) {
return const Left(PasswordNonAscii());
}
final lowered = input.toLowerCase();
if (_hasCommonPasswordPrefix(lowered)) {
return const Left(PasswordTooCommon());
}
if (v.PasswordValidator.hasOnlyRepeatedCharacters(input)) {
return const Left(PasswordTooWeak());
}
final maxAllowedRun = (input.length * config.sequentialRunFactor)
.floor()
.clamp(3, 7);
if (v.PasswordValidator.hasSequentialRun(lowered, minRun: maxAllowedRun)) {
return const Left(PasswordTooPredictable());
}
final score = _classScore(input);
if (score < config.minComplexityScore) {
return Left(
PasswordComplexityRequired(
currentScore: score,
requiredScore: config.minComplexityScore,
),
);
}
return Right(input);
}