veza/veza-common/src/utils/validation.rs
senke 670282989b chore(refactor/sumi-migration): commit pending changes — tests, stream server, dist_verification
- apps/web: test updates (Vitest/setup), playbackAnalyticsService, TrackGrid, serviceErrorHandler
- veza-common: logging, metrics, traits, validation, random
- veza-stream-server: audio pipeline, codecs, cache, monitoring, routes
- apps/web/dist_verification: refresh build assets (content-hashed filenames)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-13 19:39:18 +01:00

60 lines
1.7 KiB
Rust

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<bool> {
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<Vec<String>> {
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()
}