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