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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::error::{ValidationError, ValidationErrors};
use crate::traits::Validator;
use std::sync::Arc;
type SchemaRule<T> = Arc<dyn Fn(&T, &mut ValidationErrors) + Send + Sync>;
/// Strongly-typed schema validator for arbitrary structs without macros.
#[derive(Clone)]
pub struct Schema<T> {
rules: Vec<SchemaRule<T>>,
}
impl<T> Default for Schema<T> {
fn default() -> Self {
Self { rules: Vec::new() }
}
}
impl<T> Schema<T> {
/// Creates a new schema validator for type `T`.
pub fn new() -> Self {
Self::default()
}
/// Adds a validation rule bound to a specific field selector on `T`.
///
/// # Example
/// ```rust
/// use catfive::{schema, string};
///
/// struct Account {
/// username: String,
/// }
///
/// let account_schema = schema::<Account>()
/// .field("username", |a| &a.username, string().min(3).block(";@#$&"));
/// ```
pub fn field<F, V, Val>(mut self, name: impl Into<String>, extractor: F, validator: Val) -> Self
where
F: Fn(&T) -> &V + Send + Sync + 'static,
V: ?Sized + 'static,
Val: Validator<V> + Send + Sync + 'static,
{
let field_name = name.into();
self.rules.push(Arc::new(move |obj, errors| {
let val = extractor(obj);
let field_errors = validator.validate_all(val);
errors.add_many(&field_name, field_errors);
}));
self
}
/// Adds a validation rule bound to an optional field (`Option<V>`).
///
/// If the extractor returns `None`, validation succeeds.
/// If it returns `Some(&v)`, the validator is executed.
///
/// # Example
/// ```rust
/// use catfive::{schema, string};
///
/// struct Profile {
/// bio: Option<String>,
/// }
///
/// let profile_schema = schema::<Profile>()
/// .optional_field("bio", |p| p.bio.as_deref(), string().max(160));
/// ```
pub fn optional_field<F, V, Val>(
mut self,
name: impl Into<String>,
extractor: F,
validator: Val,
) -> Self
where
F: Fn(&T) -> Option<&V> + Send + Sync + 'static,
V: ?Sized + 'static,
Val: Validator<V> + Send + Sync + 'static,
{
let field_name = name.into();
self.rules.push(Arc::new(move |obj, errors| {
if let Some(val) = extractor(obj) {
let field_errors = validator.validate_all(val);
errors.add_many(&field_name, field_errors);
}
}));
self
}
/// Adds a struct-level cross-field validation rule (refinement).
///
/// # Example
/// ```rust
/// use catfive::{schema, string, ValidationError};
///
/// struct Register {
/// password: String,
/// confirm: String,
/// }
///
/// let reg_schema = schema::<Register>()
/// .field("password", |r| &r.password, string().min(8))
/// .refine("confirm", |r| {
/// if r.password == r.confirm {
/// Ok(())
/// } else {
/// Err(ValidationError::new("mismatch", "Passwords do not match"))
/// }
/// });
/// ```
pub fn refine<F>(mut self, field: impl Into<String>, check: F) -> Self
where
F: Fn(&T) -> Result<(), ValidationError> + Send + Sync + 'static,
{
let field_name = field.into();
self.rules.push(Arc::new(move |obj, errors| {
if let Err(err) = check(obj) {
errors.add(&field_name, err);
}
}));
self
}
/// Composes a nested schema, prefixing all child errors with `"{prefix}."`.
///
/// # Example
/// ```rust
/// use catfive::{schema, string};
///
/// struct Address {
/// street: String,
/// }
///
/// struct User {
/// name: String,
/// address: Address,
/// }
///
/// let address_schema = schema::<Address>()
/// .field("street", |a| &a.street, string().min(5));
///
/// let user_schema = schema::<User>()
/// .field("name", |u| &u.name, string().min(2))
/// .nested("address", |u| &u.address, address_schema);
/// ```
pub fn nested<F, SubT>(
mut self,
prefix: impl Into<String>,
extractor: F,
sub_schema: Schema<SubT>,
) -> Self
where
F: Fn(&T) -> &SubT + Send + Sync + 'static,
SubT: 'static,
{
let prefix_str = prefix.into();
let prefix_with_dot = if prefix_str.ends_with('.') {
prefix_str
} else {
format!("{}.", prefix_str)
};
self.rules.push(Arc::new(move |obj, errors| {
let sub_obj = extractor(obj);
if let Err(mut sub_errors) = sub_schema.validate(sub_obj) {
sub_errors.prefix_fields(&prefix_with_dot);
errors.merge(sub_errors);
}
}));
self
}
/// Validates an instance of `T`, accumulating all errors into `ValidationErrors`.
pub fn validate(&self, target: &T) -> Result<(), ValidationErrors> {
let mut errors = ValidationErrors::new();
for rule in &self.rules {
rule(target, &mut errors);
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl<T: Send + Sync + 'static> Validator<T> for Schema<T> {
fn validate_all(&self, value: &T) -> Vec<ValidationError> {
let mut validation_errors = ValidationErrors::new();
for rule in &self.rules {
rule(value, &mut validation_errors);
}
validation_errors
.into_inner()
.into_iter()
.flat_map(|(field, errs)| {
errs.into_iter().map(move |err| {
ValidationError::custom_code(
err.code().to_string(),
format!("{}: {}", field, err.message()),
)
})
})
.collect()
}
}