Password constructor
Password(
- String? str
Returns a valid Password object.
Throws ValueException:
- RequiredValueException if
str
is null or empty. - TooShortValueException if
str
has too few characters. - TooLongValueException if
str
has too many characters. - InvalidValueException if
str
is invalid.
Implementation
factory Password(String? str) {
if (str == null || str.isEmpty) {
throw const RequiredValueException();
} else if (str.length < minChar) {
throw TooShortValueException(str);
} else if (str.length > maxChar) {
throw TooLongValueException(str);
} else if (Password.mustContainLowerChar && !hasLowerChar(str)) {
throw InvalidValueException(
str,
code: 'must-contain-lower',
message: 'Password must contain a lower case letter.',
);
} else if (Password.mustContainUpperChar && !hasUpperChar(str)) {
throw InvalidValueException(
str,
code: 'must-contain-upper',
message: 'Password must contain an upper case letter.',
);
} else if (Password.mustContainNumeric && !hasNumeric(str)) {
throw InvalidValueException(
str,
code: 'must-contain-number',
message: 'Password must contain a number.',
);
} else if (Password.mustContainSpecialChar && !hasSpecialChar(str)) {
throw InvalidValueException(
str,
code: 'must-have-special',
message: 'Password must contain a special character.',
);
}
return Password._(str);
}