Skip to content

Commit

Permalink
Update validation.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
PancernyJez authored Jul 2, 2024
1 parent 8786eea commit d6b696b
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
#include "validation.hpp"
// TODO: Put implementations here
std::string getErrorMessage(ErrorCode error) {
switch (error) {
default:
return "";

case ErrorCode::Ok:
return "Ok";

case ErrorCode::PasswordNeedsAtLeastNineCharacters:
return "PasswordNeedsAtLeastNineCharacters";

case ErrorCode::PasswordNeedsAtLeastOneNumber:
return "PasswordNeedsAtLeastOneNumber";

case ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter:
return "PasswordNeedsAtLeastOneSpecialCharacter";

case ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter:
return "PasswordNeedsAtLeastOneUppercaseLetter";

case ErrorCode::PasswordsDoNotMatch:
return "PasswordsDoNotMatch";
}
}

bool doPasswordsMatch(const std::string &password1, const std::string &password2) {
if (password1 == password2) {
return true;
} else {
return false;
}
}

ErrorCode checkPasswordRules(const std::string &password) {
// first i will try to check if theres at least 9 characters
if (password.length() < 9) {
return ErrorCode::PasswordNeedsAtLeastNineCharacters;
}
// if predycate if there is none of the numbers rerturn errorcode
else if (std::none_of(password.begin(), password.end(), [](char c) { return std::isdigit(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneNumber;
} else if (std::none_of(password.begin(), password.end(), [](char c) { return std::isupper(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter;
} else if (std::none_of(password.begin(), password.end(), [](char c) { return std::ispunct(c); })) {
return ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
} else
return ErrorCode::Ok;
}

ErrorCode checkPassword(const std::string &password1, const std::string &password2) {
if (doPasswordsMatch(password1, password2))
return checkPasswordRules(password1);
else
return ErrorCode::PasswordsDoNotMatch;
}

0 comments on commit d6b696b

Please sign in to comment.