1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Core traits for the validation system.
//!
//! - [`Validate`] is the main trait, generated by `#[derive(Validate)]`.
//! - [`FieldAccess`] enables cross-field rules like `same`, `required_if`, `gt`, etc.
//! - [`Rule`] lets you create reusable custom validation rules.
use crateValidationErrors;
use crateFieldValue;
/// The main validation trait. Derived via `#[derive(Validate)]`.
///
/// Call `.validate()` on any struct that derives `Validate` to run all rules.
/// Returns `Ok(())` if valid, or `Err(ValidationErrors)` with all failures.
///
/// # Example
///
/// ```rust
/// use scrutiny::Validate;
/// use scrutiny::traits::Validate as _;
///
/// #[derive(Validate)]
/// struct Login {
/// #[validate(required, email)]
/// email: Option<String>,
/// #[validate(required, min = 8)]
/// password: Option<String>,
/// }
///
/// let login = Login {
/// email: Some("user@example.com".into()),
/// password: Some("hunter42!".into()),
/// };
/// assert!(login.validate().is_ok());
/// ```
/// Allows rules to look up sibling field values by name at runtime.
///
/// Automatically generated by the derive macro for each struct. This trait
/// bridges Rust's static type system with cross-field rules that need to
/// compare arbitrary fields by name (like `required_if`, `same`, `gt`, etc.)
/// A custom validation rule. Implement this for reusable validation logic.
///
/// # Example
///
/// ```rust
/// use scrutiny::traits::{Rule, FieldAccess};
///
/// struct NoSpam;
///
/// impl Rule<Option<String>> for NoSpam {
/// fn validate(&self, field: &str, value: &Option<String>, _fields: &dyn FieldAccess) -> Result<(), String> {
/// if let Some(v) = value {
/// if v.contains("buy now") {
/// return Err(format!("The {} field looks like spam.", field));
/// }
/// }
/// Ok(())
/// }
/// }
/// ```
/// Function signature for custom validators used with `#[validate(custom = my_fn)]`.
///
/// The function receives a reference to the field value and the struct (as `FieldAccess`
/// for cross-field access). Return `Ok(())` if valid, or `Err(message)`.
pub type CustomValidator<T> = fn ;