1pub mod credit_card;
18pub mod date;
19pub mod email;
20pub mod mobile;
21pub mod numeric;
22pub mod string;
23pub mod url;
24
25pub use credit_card::is_valid_credit_card;
27pub use date::is_valid_date;
28pub use email::is_valid_email;
29pub use mobile::is_valid_phone;
30pub use numeric::{is_in_range, is_negative, is_positive};
31pub use string::{is_alpha, is_alphanumeric, is_numeric};
32pub use url::is_valid_url;
33
34pub type ValidationResult = Result<(), ValidationError>;
36
37#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct ValidationError {
40 pub field: String,
41 pub message: String,
42}
43
44impl ValidationError {
45 pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
46 Self {
47 field: field.into(),
48 message: message.into(),
49 }
50 }
51}
52
53impl std::fmt::Display for ValidationError {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 write!(f, "Validation error for '{}': {}", self.field, self.message)
56 }
57}
58
59impl std::error::Error for ValidationError {}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_validation_error() {
67 let error = ValidationError::new("email", "Invalid email format");
68 assert_eq!(error.field, "email");
69 assert_eq!(error.message, "Invalid email format");
70 }
71}