2025-12-13 02:34:34 +00:00
|
|
|
use crate::error::{CommonResult as Result, CommonError as Error};
|
2025-12-03 21:24:14 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
/// Validate email format
|
|
|
|
|
pub fn validate_email(email: &str) -> bool {
|
|
|
|
|
use regex::Regex;
|
2025-12-03 21:24:14 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
let email_regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
|
|
|
|
.expect("Invalid email regex");
|
2025-12-03 21:24:14 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
email_regex.is_match(email)
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
/// Validate username format
|
|
|
|
|
pub fn validate_username(username: &str) -> Result<bool> {
|
|
|
|
|
if username.len() < 3 || username.len() > 30 {
|
|
|
|
|
return Ok(false);
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
use regex::Regex;
|
|
|
|
|
let username_regex = Regex::new(r"^[a-zA-Z0-9_-]+$")
|
|
|
|
|
.expect("Invalid username regex");
|
2025-12-03 21:24:14 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
Ok(username_regex.is_match(username))
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
/// Validate password strength
|
|
|
|
|
pub fn validate_password_strength(password: &str) -> Result<Vec<String>> {
|
|
|
|
|
let mut errors = Vec::new();
|
2025-12-03 21:24:14 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
if password.len() < 8 {
|
|
|
|
|
errors.push("Password must be at least 8 characters long".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
if password.len() > 128 {
|
|
|
|
|
errors.push("Password must be less than 128 characters".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
if !password.chars().any(|c| c.is_uppercase()) {
|
|
|
|
|
errors.push("Password must contain at least one uppercase letter".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
if !password.chars().any(|c| c.is_lowercase()) {
|
|
|
|
|
errors.push("Password must contain at least one lowercase letter".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
2025-12-13 02:34:34 +00:00
|
|
|
|
|
|
|
|
if !password.chars().any(|c| c.is_numeric()) {
|
|
|
|
|
errors.push("Password must contain at least one digit".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
2025-12-13 02:34:34 +00:00
|
|
|
|
|
|
|
|
if !password.chars().any(|c| c.is_ascii_punctuation()) {
|
|
|
|
|
errors.push("Password must contain at least one special character".to_string());
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
2025-12-13 02:34:34 +00:00
|
|
|
|
|
|
|
|
Ok(errors)
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
/// Validate UUID format
|
|
|
|
|
pub fn is_valid_uuid(uuid_str: &str) -> bool {
|
|
|
|
|
uuid::Uuid::parse_str(uuid_str).is_ok()
|
2025-12-03 21:24:14 +00:00
|
|
|
}
|