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
268
269
270
271
272
273
274
//! Validation for the input that requests submit.
//!
//! Describe a request's input as a struct with rules on its fields, derive [`Validate`] for it,
//! and take [`Valid<T>`] in a handler. The handler runs only when the input passes. When it
//! fails, a JSON request gets a `422` response, and a browser form gets a redirect back with the
//! errors and old input.
//!
//! [`Rule`] lists the built-in rules, [`ValidationRule`] lets you write your own, and [`Rules`]
//! builds rules at runtime. The
//! [validation guide](https://github.com/laravel/rust-framework/blob/0.x/docs/validation.md)
//! walks through each step.
//!
//! ```no_run
//! use laravel::validation::{Valid, Validate};
//! use serde::Deserialize;
//!
//! #[derive(Deserialize, Validate)]
//! #[validate(anyone)]
//! struct NewUser {
//! #[validate(
//! required.message("Please enter your :attribute."),
//! max_length(100),
//! label = "display name",
//! )]
//! name: String,
//! #[validate(required, email)]
//! email: String,
//! #[validate(url("https"))]
//! website: Option<String>,
//! #[validate(between(0.0, 5.0))]
//! rating: Option<f64>,
//! }
//!
//! async fn store(Valid(user): Valid<NewUser>) -> String {
//! format!("Hello, {}", user.name)
//! }
//! ```
//!
//! # Input
//!
//! [`Valid`] reads the query string and the request body, after input middleware such as
//! `TrimStrings` and `ConvertEmptyStringsToNull` have run. Input decodes into your struct first,
//! and rules then validate the decoded values. Presence rules such as `required` and `accepted`
//! still check whether a field was submitted, so `accepted` on a `#[serde(default)]` checkbox
//! fails when the box is unchecked. The errors list decoding errors and rule errors together, so a
//! bad type in one field doesn't hide a rule error in another. Validation stops once 1,000 fields
//! have failed, so the errors for a very long list of bad items name only the first items.
//!
//! The derive supports named structs and serde's `rename`, `rename_all`, `alias`, `default`, and
//! `skip` attributes. It applies child rules through nested structs, wrappers, lists, sets, maps,
//! tuples, and type aliases. A field that uses `flatten` or `deserialize_with` needs a manual
//! [`Validate`] implementation. Field names can't contain dots, because a dot means nesting in a
//! rule path.
//!
//! Serde buffers the input for `untagged` enums and `flatten` fields, and the buffering may convert
//! numbers through floating point before a [`Decimal`](crate::numeric::Decimal) field sees them.
//! Send decimals on those paths as quoted strings.
//!
//! # Field transforms
//!
//! Mark string fields with `#[transform(lowercase)]` or `#[transform(uppercase)]` to normalize
//! casing when request input decodes into a struct, before validation rules and handlers run.
//! Transforms apply to `String`, `Newtype` wrappers around `String`, and `Option`, `Sometimes`,
//! `Box`, `Vec`, set, and map fields that hold strings, across [`Valid`], [`ValidRequest`],
//! [`Request::validate`](crate::http::Request::validate),
//! [`Request::parse`](crate::http::Request::parse),
//! [`Request::json`](crate::http::Request::json), and
//! [`Request::query`](crate::http::Request::query). The request's stored input is left unchanged,
//! so flashed old input preserves what the client submitted.
//!
//! ```no_run
//! use laravel::prelude::*;
//!
//! #[laravel::input]
//! #[validate(anyone)]
//! struct RegisterUser {
//! #[transform(lowercase)]
//! #[validate(required, email)]
//! email: String,
//! #[transform(uppercase)]
//! #[validate(required, min_length(2), max_length(2))]
//! country: String,
//! }
//! ```
//!
//! Non-string fields fail to compile:
//!
//! ```compile_fail
//! use laravel::prelude::*;
//!
//! #[laravel::input]
//! #[validate(anyone)]
//! struct BadTransform {
//! #[transform(lowercase)]
//! age: u32,
//! }
//! ```
//!
//! # Field references
//!
//! Rules that name another field, such as `required_if`, `gte`, and `after`, resolve the name
//! relative to the object that declares the rule. In a list of objects, the name stays within the
//! same entry.
//!
//! In `#[validate(..)]`, reference fields by their Rust names without quotes, such as
//! `required_if(shipping.country, "US")` or `after(starts_at)`. Every segment is checked at
//! compile time and resolves to the field's input name after serde renames. [`Rules`] that you
//! build by hand name fields by their input names.
//!
//! `required_if` does nothing when the other field is missing. The other value conditions treat a
//! missing field as null. Numeric and date comparisons fail when the other field is missing, null,
//! or malformed.
//!
//! # Old input
//!
//! When a failed validation or [`invalid!`] redirects back, or when a handler calls
//! [`Redirect::with_input`](crate::http::Redirect::with_input) or
//! [`View::with_input`](crate::view::View::with_input), the old input leaves out these values:
//!
//! - The top-level `password`, `password_confirmation`, and `current_password` fields.
//! - Uploaded files.
//! - Every `Password` field of the validated type, at any depth, and its `_confirmation` field.
//!
//! # Rules that read the request
//!
//! These rules read the request, such as its route parameters or its logged-in user:
//!
//! - `unique` with `ignore = user` or `ignore = route::<Team>("team")`.
//! - `unique` and `exists` with a `filter` that reads `route::<Project>("project")`.
//! - `current_password`.
//!
//! [`Validate::validate`] and [`Rules::validate`] have no request, so these rules fail there with a
//! `500`. `ignore = user` and `current_password` read the session guard's user. On a route that has
//! neither a session nor a request guard, they fail with a `500` whose message names the fix.
pub use invalid;
pub use *;
pub use ;
/// Derives [`Validate`] from `#[validate(..)]` attributes on a named struct's fields.
///
/// A `String` field accepts only strings, and an `Option` field accepts null. The derive reads
/// serde's field names and aliases, and a field attribute may name a custom rule expression. A
/// field whose type implements `Validate` (including nested structs, `Option`, `Box`, `Vec`,
/// `HashSet`, `BTreeSet`, `IndexSet`, `HashMap`, `BTreeMap`, `IndexMap`, and type aliases of them)
/// applies that type's rules recursively. `#[laravel::input]` derives `Validate` together with
/// `Deserialize`.
///
/// Every field's type must be one that validation can read: text, numbers, `bool`, `Decimal`, the
/// `jiff` date and time types, `Uuid`, `Ulid`, IP addresses, `Password`, `UploadedFile`,
/// `serde_json::Value`, a type that derives `Validate`, `Enum`, `Newtype`, or `db::Json`,
/// or one of the containers above holding them. Any other field type, such as a tuple, fails to
/// compile with an error that names the derive to add.
///
/// These attributes go on the struct itself:
///
/// - `#[validate(anyone)]`, `#[validate(authenticated)]`, and `#[validate(authorize(..))]`
/// implement [`ValidatesRequest`].
/// - `#[validate(form = "login")]` sets [`Validate::form`].
///
/// ```
/// use laravel::validation::Validate;
///
/// #[derive(serde::Deserialize, Validate)]
/// struct Address {
/// #[validate(required)]
/// city: String,
/// }
///
/// #[derive(serde::Deserialize, Validate)]
/// struct Profile {
/// #[validate(required)]
/// address: Option<Address>,
/// previous_addresses: Vec<Address>,
/// }
/// ```
///
/// Chain `.message("...")` onto any rule to replace its failure message, set `label = "..."` on a
/// field to change its `:attribute` name, and set `invalid = "..."` to replace the message for a
/// value that doesn't fit the field's type (such as text for a `u32`, a malformed `Uuid`, `IpAddr`,
/// `Date`, or `Timestamp`, or a `Password` over 72 bytes). Rules declared on the field, such as
/// `required` or `uuid(7)`, keep their own messages. For lists and maps, chain `.label("...")` and
/// `.invalid("...")` onto `each(...)`.
///
/// When a field fails decoding, implicit rules (such as `required`) and format rules (such as
/// `uuid`, `ip`, `date`, or `file`) supply field-specific messages while value and custom rules
/// skip the failed field.
///
/// A bare `#[validate]` requires the field's type to implement `Validate`, such as a field whose
/// type is a generic type parameter:
///
/// ```compile_fail
/// #[derive(laravel::validation::Validate)]
/// struct Profile {
/// #[validate]
/// address: Address,
/// }
/// struct Address {
/// city: String,
/// }
/// ```
///
/// Nested field references check every segment at compile time, so a misspelled nested field fails
/// to compile:
///
/// ```compile_fail
/// #[derive(laravel::validation::Validate)]
/// struct Address {
/// country: String,
/// }
///
/// #[derive(laravel::validation::Validate)]
/// struct StoreOrder {
/// shipping: Address,
/// #[validate(required_if(shipping.contry, "US"))]
/// phone: Option<String>,
/// }
/// ```
///
/// Stepping into a field whose type doesn't derive `Validate` also fails to compile:
///
/// ```compile_fail
/// #[derive(laravel::validation::Validate)]
/// struct StoreOrder {
/// country: String,
/// #[validate(required_if(country.code, "US"))]
/// phone: Option<String>,
/// }
/// ```
///
/// Generic fields automatically require `Validate`, so a wrapper can't silently skip a nested
/// type's rules:
///
/// ```compile_fail
/// use laravel::validation::Validate;
///
/// #[derive(Validate)]
/// struct Wrapper<T> {
/// inner: T,
/// }
///
/// let _ = Wrapper::<String>::declared_rules();
/// ```
pub use Validate;
pub use ;
pub use ;