pub fn has_max_length(s: &str, max: usize) -> boolExpand description
Validates if a string has a maximum length
Examples found in repository?
examples/error_handling.rs (line 57)
50fn validate_password(password: &str) -> Result<(), Vec<String>> {
51 let mut errors = Vec::new();
52
53 if !string::has_min_length(password, 8) {
54 errors.push("Password must be at least 8 characters long".to_string());
55 }
56
57 if !string::has_max_length(password, 128) {
58 errors.push("Password must not exceed 128 characters".to_string());
59 }
60
61 if !password.chars().any(|c| c.is_uppercase()) {
62 errors.push("Password must contain at least one uppercase letter".to_string());
63 }
64
65 if !password.chars().any(|c| c.is_lowercase()) {
66 errors.push("Password must contain at least one lowercase letter".to_string());
67 }
68
69 if !password.chars().any(|c| c.is_ascii_digit()) {
70 errors.push("Password must contain at least one digit".to_string());
71 }
72
73 if !password.chars().any(|c| !c.is_alphanumeric()) {
74 errors.push("Password must contain at least one special character".to_string());
75 }
76
77 if errors.is_empty() {
78 Ok(())
79 } else {
80 Err(errors)
81 }
82}