validator_rs/
lib.rs

1//! # Validator-rs
2//!
3//! A comprehensive validation library for Rust that provides various validator functions
4//! for common data types and formats.
5//!
6//! ## Usage
7//!
8//! ```rust
9//! use validator_rs::email::is_valid_email;
10//! use validator_rs::url::is_valid_url;
11//!
12//! assert!(is_valid_email("test@example.com"));
13//! assert!(is_valid_url("https://www.example.com"));
14//! ```
15
16// Export all validator modules
17pub mod credit_card;
18pub mod date;
19pub mod email;
20pub mod mobile;
21pub mod numeric;
22pub mod string;
23pub mod url;
24
25// Re-export commonly used validators for convenience
26pub 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
34/// Common result type used across validators
35pub type ValidationResult = Result<(), ValidationError>;
36
37/// Error type for validation failures
38#[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}