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