rustapi_validate/lib.rs
1//! # RustAPI Validation
2//!
3//! Validation system for RustAPI framework. Provides declarative validation
4//! on structs using the `#[derive(Validate)]` macro.
5//!
6//! ## Example
7//!
8//! ```rust,ignore
9//! use rustapi_validate::prelude::*;
10//! use validator::Validate;
11//!
12//! #[derive(Validate)]
13//! struct CreateUser {
14//! #[validate(email)]
15//! email: String,
16//!
17//! #[validate(length(min = 3, max = 50))]
18//! username: String,
19//!
20//! #[validate(range(min = 18, max = 120))]
21//! age: u8,
22//! }
23//! ```
24//!
25//! ## V2 Validation Engine
26//!
27//! The v2 module provides a custom validation engine with async support:
28//!
29//! ```rust,ignore
30//! use rustapi_validate::v2::prelude::*;
31//!
32//! #[derive(Validate)]
33//! struct CreateUser {
34//! #[validate(email, message = "Invalid email format")]
35//! email: String,
36//!
37//! #[validate(length(min = 3, max = 50))]
38//! username: String,
39//!
40//! #[validate(async_unique(table = "users", column = "email"))]
41//! unique_email: String,
42//! }
43//! ```
44//!
45//! ## Validation Rules
46//!
47//! - `email` - Validates email format
48//! - `length(min = X, max = Y)` - String length validation
49//! - `range(min = X, max = Y)` - Numeric range validation
50//! - `regex = "..."` - Regex pattern validation
51//! - `url` - URL format validation
52//! - `required` - Non-empty string/option validation
53//! - `async_unique(table, column)` - Database uniqueness check
54//! - `async_exists(table, column)` - Database existence check
55//! - `async_api(endpoint)` - External API validation
56//!
57//! ## Error Format
58//!
59//! Validation errors return a 422 Unprocessable Entity with JSON:
60//!
61//! ```json
62//! {
63//! "error": {
64//! "type": "validation_error",
65//! "message": "Validation failed",
66//! "fields": [
67//! {"field": "email", "code": "email", "message": "Invalid email format"},
68//! {"field": "age", "code": "range", "message": "Value must be between 18 and 120"}
69//! ]
70//! }
71//! }
72//! ```
73
74// Load I18n locales
75rust_i18n::i18n!("locales");
76
77pub mod custom;
78mod error;
79
80/// V2 validation engine with async support.
81///
82/// This module provides a custom validation engine that replaces the external
83/// `validator` dependency and adds support for async validation operations.
84pub mod v2;
85
86pub use error::{FieldError, ValidationError};
87pub use v2::Validate;
88
89// Re-export the v2 Validate derive macro
90pub use rustapi_macros::Validate as DeriveValidate;
91
92/// Prelude module for validation
93pub mod prelude {
94 pub use crate::error::{FieldError, ValidationError};
95 pub use crate::v2::Validate;
96
97 // Re-export v2 prelude
98 pub use crate::v2::prelude::*;
99
100 // Re-export derive macro
101 pub use rustapi_macros::Validate as DeriveValidate;
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn validation_error_to_json() {
110 let error = ValidationError::new(vec![
111 FieldError::new("email", "email", "Invalid email format"),
112 FieldError::new("age", "range", "Value must be between 18 and 120"),
113 ]);
114
115 let json = serde_json::to_string_pretty(&error).unwrap();
116 assert!(json.contains("validation_error"));
117 assert!(json.contains("email"));
118 assert!(json.contains("age"));
119 }
120}