pub struct ValidationError {
pub field: String,
pub message: String,
}Expand description
Error type for validation failures
Fields§
§field: String§message: StringImplementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn new(field: impl Into<String>, message: impl Into<String>) -> Self
pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self
Examples found in repository?
examples/error_handling.rs (line 23)
18 fn validate(&self) -> Result<(), Vec<ValidationError>> {
19 let mut errors = Vec::new();
20
21 // Validate email
22 if !is_valid_email(&self.email) {
23 errors.push(ValidationError::new("email", "Invalid email format"));
24 }
25
26 // Validate phone
27 if !is_valid_phone(&self.phone) {
28 errors.push(ValidationError::new("phone", "Invalid phone number"));
29 }
30
31 // Validate website
32 if !is_valid_https_url(&self.website) {
33 errors.push(ValidationError::new("website", "Must be a valid HTTPS URL"));
34 }
35
36 // Validate credit card
37 if !credit_card::is_valid_credit_card(&self.credit_card) {
38 errors.push(ValidationError::new("credit_card", "Invalid credit card number"));
39 }
40
41 if errors.is_empty() {
42 Ok(())
43 } else {
44 Err(errors)
45 }
46 }
47}
48
49/// Validates a password with multiple rules
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}
83
84/// Validates age with range checking
85fn validate_age(age: i32) -> ValidationResult {
86 if !numeric::is_in_range(age, 18, 120) {
87 return Err(ValidationError::new("age", "Age must be between 18 and 120"));
88 }
89 Ok(())
90}Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§fn clone(&self) -> ValidationError
fn clone(&self) -> ValidationError
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Display for ValidationError
impl Display for ValidationError
Source§impl Error for ValidationError
impl Error for ValidationError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl PartialEq for ValidationError
impl PartialEq for ValidationError
impl Eq for ValidationError
impl StructuralPartialEq for ValidationError
Auto Trait Implementations§
impl Freeze for ValidationError
impl RefUnwindSafe for ValidationError
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnwindSafe for ValidationError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more