Files
Training-Team2-Zenvy-Jan26/Trenser.Zenvy/Trenser.Zenvy/utilities/Validator.cpp
T
joelthomastrenser 133785dd3f refactor: change memory management and fix related code
- Switched shared_ptr to raw pointers
- Added cleanup logic in DataStore
- Fixed Factory object creation
- Updated function signatures to match changes
- Small refactors and formatting fixes
2026-04-17 09:36:34 +05:30

165 lines
3.8 KiB
C++

/*
* File: Validator.cpp
* Description: Validates inputs like phone number, email, password
* Author: Trenser
* Created: 01-Apr-2026
*/
#include <string>
#include <cctype>
#include "Validator.h"
#include "Employee.h"
#include "ApplicationConfig.h"
/*
* Function: isPhoneNumberValid
* Description: Validates whether the given string is a valid phone number.
* Parameters:
* phoneNumber - string containing the phone number to validate
* Returns:
* bool - true if the phone number is valid (10 digits, all numeric), false otherwise
*/
bool util::isPhoneNumberValid(const std::string& phoneNumber) {
if (phoneNumber.size() != 10)
{
return false;
}
return std::all_of(phoneNumber.begin(), phoneNumber.end(),
[](char character)
{
return std::isdigit(character);
}
);
}
/*
* Function: isEmailValid
* Description: Validates whether the given string is a properly formatted email address.
* Parameters:
* email - string containing the email address to validate
* Returns:
* bool - true if the email contains exactly one '@' character and is not at the start or end, false otherwise
*/
bool util::isEmailValid(const std::string& email) {
size_t index = email.find('@');
if (index == std::string::npos)
{
return false;
}
if (email.find('@', index + 1) != std::string::npos)
{
return false;
}
if (index == 0 || index == email.size() - 1)
{
return false;
}
return true;
}
/*
* Function: isPasswordValid
* Description: Validates whether the given string meets password requirements.
* Parameters:
* password - string containing the password to validate
* Returns:
* bool - true if the password is valid, false otherwise
* Notes:
* - Must not equal the default password
* - Must be at least 8 characters long
* - Must contain at least one uppercase letter, one lowercase letter, one digit, and one special character
* - Must not contain whitespace
*/
bool util::isPasswordValid(const std::string& password)
{
if (password == Config::Authentication::DEFAULT_PASSWORD)
{
return false;
}
if (password.length() < 8)
return false;
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool hasSpecial = false;
for (char character : password)
{
if (std::isspace(static_cast<unsigned char>(character)))
{
return false;
}
if (std::isupper(static_cast<unsigned char>(character)))
{
hasUpper = true;
}
else if (std::islower(static_cast<unsigned char>(character)))
{
hasLower = true;
}
else if (std::isdigit(static_cast<unsigned char>(character)))
{
hasDigit = true;
}
else
{
hasSpecial = true;
}
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::map<std::string, Employee*> & employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeType() == employeeType && employee->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
{
return true;
}
}
return false;
}
bool util::isEmailDuplicate(const std::string& email, const std::map<std::string, Employee*>& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeEmail() == email)
{
return true;
}
}
return false;
}
bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string, Employee*>& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}
bool util::isPhoneDuplicate(const std::string& phone, const std::vector<const Employee*>& employees)
{
for (const auto& employee : employees)
{
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}