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 currency;
19pub mod date;
20pub mod email;
21pub mod mobile;
22pub mod numeric;
23pub mod string;
24pub mod url;
25
26// Re-export commonly used validators for convenience
27pub 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
36/// Common result type used across validators
37pub type ValidationResult = Result<(), ValidationError>;
38
39/// Error type for validation failures
40#[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}