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
//! 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, max_length = 100, label = "display name",
//! messages(required = "Please enter your :attribute."))]
//! 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. Rules check the submitted values before
//! serde defaults fill in missing fields, 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 recognizes `String`, `Option`, `Vec`, [`Sometimes`], and
//! [`Password`](crate::hash::Password) fields by the type name written in the struct. A field that
//! uses a type alias, `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 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. A dotted name reaches into a child object, and a wildcard doesn't expand.
//!
//! In `#[validate(..)]`, name fields by their Rust names. The derive resolves each name to the
//! field's input name, after serde renames, and a name that isn't a field of the struct doesn't
//! compile. [`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`.
//!
//! [`Rules::validate`] has 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`,
/// `HashMap`, `BTreeMap`, `IndexMap`, and type aliases of them) applies that type's rules
/// recursively. `#[laravel::input]` derives `Validate` together with `Deserialize`.
///
/// 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(Validate)]
/// struct Address {
/// #[validate(required)]
/// city: String,
/// }
///
/// #[derive(Validate)]
/// struct Profile {
/// #[validate(required)]
/// address: Option<Address>,
/// previous_addresses: Vec<Address>,
/// }
/// ```
///
/// `messages(..)` may also override the errors that come from decoding a field's type:
///
/// - `integer`, `numeric`, `boolean`, `string`, `character`, and `array`, for a value of the wrong
/// type.
/// - `between`, for an integer outside its type's bounds.
/// - `finite`, for a number that overflows `f32`.
/// - `max_bytes`, for a `Password` over 72 bytes.
/// - `required`, for a missing field that isn't optional.
/// - `selected`, for an unknown enum choice.
/// - `prohibited`, for an unknown field that serde rejects.
/// - `invalid`, for any other decoding failure.
///
/// When a field has both a rule error and a decoding error, the rule error is reported.
///
/// `messages(invalid = ..)` also sets the message of every check that the field's type implies,
/// unless the field overrides that check's own message. Those checks are the type errors above,
/// except `required`, `prohibited`, and `max_bytes`, and the format rules that the derive infers
/// from types such as `Uuid`, `IpAddr`, `Date`, and `Timestamp`. A rule that the field declares,
/// such as `uuid = 7`, keeps its own message.
///
/// 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,
/// }
/// ```
pub use Validate;
pub use ;
pub use ;