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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! # Scrutiny
//!
//! A powerful, Laravel-inspired validation library for Rust. Uses derive macros and
//! field-level attributes to declaratively validate structs — no runtime string parsing.
//!
//! ## Correct by default
//!
//! Format rules delegate to dedicated, standards-compliant crates rather than
//! hand-rolled regexes:
//!
//! | Rule | Standard | Crate | Feature |
//! |------|----------|-------|---------|
//! | `email` | RFC 5321 | `email_address` | `email` |
//! | `url` | WHATWG URL | `url` | `url-parse` |
//! | `uuid` | RFC 4122 | `uuid` | `uuid-parse` |
//! | `ulid` | ULID spec | `ulid` | `ulid-parse` |
//! | `date` / `datetime` | ISO 8601 | `chrono` | `chrono` |
//! | `timezone` | IANA tz database | `chrono-tz` | `timezone` |
//! | `ip` / `ipv4` / `ipv6` | RFC 791 / 2460 | `std::net` | — |
//!
//! All enabled by default. Disable default features for a minimal build and
//! opt in to what you need.
//!
//! ## Quick Start
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! struct CreateUser {
//! #[validate(required, email, bail)]
//! email: Option<String>,
//!
//! #[validate(required, min = 2, max = 255)]
//! name: Option<String>,
//!
//! #[validate(required, min = 8, confirmed)]
//! password: Option<String>,
//!
//! password_confirmation: Option<String>,
//!
//! #[validate(nullable, url)]
//! website: Option<String>,
//! }
//!
//! let user = CreateUser {
//! email: Some("test@example.com".into()),
//! name: Some("Jane".into()),
//! password: Some("secret123".into()),
//! password_confirmation: Some("secret123".into()),
//! website: None,
//! };
//! assert!(user.validate().is_ok());
//! ```
//!
//! ## Custom Error Messages
//!
//! Every rule has a sensible default message. Override per-rule with `message`:
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! #[validate(attributes(name = "full name"))]
//! struct Profile {
//! #[validate(required(message = "We need your name!"), min = 2)]
//! name: Option<String>,
//!
//! #[validate(required, email(message = "That doesn't look right."))]
//! email: Option<String>,
//! }
//!
//! let p = Profile { name: None, email: Some("bad".into()) };
//! let err = p.validate().unwrap_err();
//! assert_eq!(err.messages()["name"][0], "We need your name!");
//! assert_eq!(err.messages()["email"][0], "That doesn't look right.");
//! ```
//!
//! ## Nested & Vec Validation
//!
//! Use `nested` to recursively validate nested structs and Vec elements.
//! Errors use dot-notation paths: `address.city`, `members.0.email`.
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! struct Address {
//! #[validate(required)]
//! city: Option<String>,
//! }
//!
//! #[derive(Validate)]
//! struct Order {
//! #[validate(nested)]
//! address: Address,
//! }
//!
//! let order = Order { address: Address { city: None } };
//! let err = order.validate().unwrap_err();
//! assert!(err.messages().contains_key("address.city"));
//! ```
//!
//! ## Tuple Structs
//!
//! Newtypes get validation for free — encode your invariants in the type system:
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! struct Email(#[validate(email)] String);
//!
//! #[derive(Validate)]
//! struct Score(#[validate(min = 0, max = 100)] i32);
//!
//! assert!(Email("user@example.com".into()).validate().is_ok());
//! assert!(Score(101).validate().is_err());
//! ```
//!
//! ## Enums
//!
//! Validate fields per variant. Unit variants always pass.
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! enum ContactMethod {
//! Email {
//! #[validate(required, email)]
//! address: Option<String>,
//! },
//! Phone {
//! #[validate(required, min = 5)]
//! number: Option<String>,
//! },
//! None,
//! }
//!
//! let c = ContactMethod::Email { address: Some("bad".into()) };
//! assert!(c.validate().is_err());
//!
//! let c = ContactMethod::None;
//! assert!(c.validate().is_ok());
//! ```
//!
//! ## Restricting Enum Variants
//!
//! Use `in_list`/`not_in` with [strum](https://crates.io/crates/strum)'s `AsRefStr`
//! to restrict which variants are accepted:
//!
//! ```rust,ignore
//! #[derive(Deserialize, strum::AsRefStr)]
//! enum UserStatus { Active, Inactive, Banned }
//!
//! #[derive(Validate, Deserialize)]
//! struct CreateUser {
//! #[validate(in_list("Active", "Inactive"))]
//! status: UserStatus,
//! }
//! ```
//!
//! Works on any type implementing `AsRef<str>`.
//!
//! ## Conditional Validation
//!
//! ```rust
//! use scrutiny::Validate;
//! use scrutiny::traits::Validate as _;
//!
//! #[derive(Validate)]
//! struct Registration {
//! #[validate(required, in_list("user", "admin"))]
//! role: Option<String>,
//!
//! #[validate(required_if(field = "role", value = "admin"))]
//! admin_code: Option<String>,
//! }
//!
//! // admin_code only required when role is "admin"
//! let reg = Registration { role: Some("user".into()), admin_code: None };
//! assert!(reg.validate().is_ok());
//! ```
//!
//! ## Available Rules
//!
//! ### Presence & Meta
//! `required`, `filled`, `nullable`, `sometimes`, `bail`, `prohibited`,
//! `prohibited_if`, `prohibited_unless`
//!
//! ### Type & Format
//! `string`, `integer`, `numeric`, `boolean`, `email`, `url`, `uuid`, `ulid`,
//! `ip`, `ipv4`, `ipv6`, `mac_address`, `json`, `ascii`, `hex_color`, `timezone`
//!
//! ### String
//! `alpha`, `alpha_num`, `alpha_dash`, `uppercase`, `lowercase`,
//! `starts_with`, `ends_with`, `doesnt_start_with`, `doesnt_end_with`,
//! `contains`, `doesnt_contain`, `regex`, `not_regex`
//!
//! ### Size, Length & Range
//! `min`, `max`, `between`, `size` — **type-aware**: compares numeric values
//! for number fields, string length for `String`, and item count for `Vec`.
//!
//! `digits`, `digits_between`, `decimal`, `multiple_of`
//!
//! ### Comparison
//! `same`, `different`, `confirmed`, `gt`, `gte`, `lt`, `lte`,
//! `in_list`, `not_in`, `in_array`, `distinct`
//!
//! ### Conditional
//! `required_if`, `required_unless`, `required_with`, `required_without`,
//! `required_with_all`, `required_without_all`, `accepted`, `accepted_if`,
//! `declined`, `declined_if`
//!
//! ### Date (ISO 8601 strict)
//! `date`, `datetime`, `date_equals`, `before`, `after`,
//! `before_or_equal`, `after_or_equal`
//!
//! ### Structural
//! `nested` (alias: `dive`), `custom`
//!
//! ## Typed Fields & Deserialization Errors
//!
//! Use actual types like `uuid::Uuid` or `chrono::NaiveDate` instead of validating
//! strings manually. Deserialization errors become field-level validation errors
//! automatically.
//!
//! **Axum users** — `Valid<T>` handles this out of the box. Just use typed fields.
//!
//! **Everyone else** — use [`deserialize::from_json`] to get unified errors:
//!
//! ```rust,ignore
//! use scrutiny::deserialize::from_json;
//!
//! // id: uuid::Uuid — if "not-a-uuid" is sent, you get:
//! // {"id": ["invalid type: expected UUID"]}
//! match from_json::<CreateUser>(body_bytes) {
//! Ok(user) => { /* deserialized AND validated */ }
//! Err(errors) => { /* same ValidationErrors type for both */ }
//! }
//! ```
//!
//! ## Error Serialization
//!
//! With the `serde` feature (default), `ValidationErrors` serializes to:
//!
//! ```json
//! {
//! "email": ["The email field is required."],
//! "name": ["The name field must be at least 2 characters."]
//! }
//! ```
pub use Validate;