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
206
207
208
209
//! Post-decode validation constraints for oxicode.
//!
//! This module provides a standalone constraint/validator API for checking
//! already-decoded values. **It is not decode-time middleware**: nothing in
//! this module hooks into [`crate::Decode`] or the [`crate::de::Decoder`]
//! trait, and constructing a [`Validator`] does not change how encoding or
//! decoding behaves. Instead, the typical flow is:
//!
//! 1. Decode a value normally (e.g. with [`crate::decode_from_slice`], or,
//! when the `checksum` feature is enabled, [`crate::checksum::decode_with_checksum`]).
//! 2. Explicitly call [`Validator::validate`] (or one of its variants) on the
//! decoded value, at a point of the caller's choosing.
//!
//! [`Validator::decode_and_validate`] (available with the `checksum` feature)
//! bundles both steps for convenience, but it is still an explicit call the
//! application makes after obtaining the raw bytes — not an automatic hook
//! invoked by the core encode/decode path.
//!
//! ## Features
//!
//! - **Size Limits**: Limit string/collection lengths via [`Constraints::max_len`] /
//! [`Constraints::min_len`]. For `str`/`String`, lengths are counted in
//! **UTF-8 bytes**, not chars — see [`constraints::MaxLength`] for details.
//! - **Range Constraints**: Validate numeric values with [`Constraints::range`],
//! including half-open Rust ranges via [`Range::from_bounds`] (exclusive
//! bounds are preserved faithfully, not silently dropped).
//! - **Non-empty**: Reject empty strings or collections via [`Constraints::non_empty`]
//! - **ASCII enforcement**: Require ASCII-only content with [`Constraints::ascii_only`]
//! - **Custom Validators**: User-defined logic via [`Constraints::custom`]
//! - **Collect or fail-fast**: Control error accumulation through [`ValidationConfig`]
//! - **Depth-limited recursive validation**: Guard hand-rolled recursive
//! validators against excessive nesting with
//! [`Validator::validate_at_depth`] and [`ValidationConfig::max_depth`]
//! - **Default fallbacks**: Recover gracefully with [`Validator::validate_or_default`]
//!
//! ## Examples
//!
//! ### Basic field validation
//!
//! ```rust
//! use oxicode::validation::{Validator, Constraints};
//!
//! // Build a validator for i32 values in [0, 120].
//! let validator: Validator<i32> = Validator::new()
//! .constraint("age", Constraints::range(Some(0i32), Some(120i32)));
//!
//! assert!(validator.validate(&50).is_ok());
//! assert!(validator.validate(&-1).is_err());
//! assert!(validator.validate(&200).is_err());
//! ```
//!
//! ### String constraints
//!
//! ```rust
//! use oxicode::validation::{Validator, Constraints};
//!
//! let mut validator: Validator<String> = Validator::new();
//! validator.add_constraint("username", Constraints::min_len(3));
//! validator.add_constraint("username", Constraints::max_len(32));
//! validator.add_constraint("username", Constraints::ascii_only());
//!
//! assert!(validator.validate(&"alice".to_string()).is_ok());
//! assert!(validator.validate(&"".to_string()).is_err());
//! assert!(validator.validate(&"x".to_string()).is_err());
//! ```
//!
//! ### Returning a default on failure
//!
//! ```rust
//! use oxicode::validation::{Validator, Constraints};
//!
//! let validator: Validator<i32> = Validator::new()
//! .constraint("score", Constraints::range(Some(0i32), Some(100i32)));
//!
//! // Returns the value unchanged when valid.
//! assert_eq!(validator.validate_or_default(75, 0), 75);
//!
//! // Returns the default when validation fails.
//! assert_eq!(validator.validate_or_default(-5, 0), 0);
//!
//! // Lazy default via closure — only evaluated on failure.
//! assert_eq!(validator.validate_or_default_with(&200, || 100), 100);
//! ```
//!
//! ### Collecting all errors (non-fail-fast)
//!
//! ```rust
//! use oxicode::validation::{Validator, Constraints, ValidationConfig};
//!
//! let config = ValidationConfig::new().with_fail_fast(false);
//! let mut validator: Validator<String> = Validator::with_config(config);
//! validator.add_constraint("field", Constraints::min_len(10));
//! validator.add_constraint("field", Constraints::max_len(5));
//!
//! // "ab" is 2 bytes, so it fails the min_len(10) constraint. With
//! // fail-fast disabled the validator reports every failing constraint at
//! // once rather than stopping at the first.
//! let result = validator.validate(&"ab".to_string());
//! assert!(result.is_err());
//! ```
pub use ;
pub use ;
pub use ;
pub use ValidatedDecodeError;
/// Configuration for validation behavior.