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
275
276
277
278
279
280
281
//! A list of all the validators that only validates the given field without
//! mutating it. The type of the field may still be changed. For example, the
//! [`ip`](crate::validators::validate_ip) validator will change the type
//! of the field to [`IpAddr`](std::net::IpAddr).
//!
//! # Contains
//!
//! The `contains` validator checks if the given value contains the given
//! substring using the [`Contains`](crate::validators::Contains) trait. By
//! default, this trait is implemented for the following types:
//!
//! - [`String`](std::string::String)
//! - [`&str`](str)
//! - [`Cow<'a, str>`](std::borrow::Cow)
//! - [`Vec<T>`](std::vec::Vec) where `T: Display`
//! - [`&[T]`](std::slice) where `T: Display`
//! - [`[T; N]`] where `T: Display` and `N` is any constant
//! - [`HashMap<K, V>`](std::collections::HashMap) where `K: Display`
//! - [`HashSet<T>`](std::collections::HashSet) where `T: Display`
//! - [`BTreeMap<K, V>`](std::collections::BTreeMap) where `K: Display`
//! - [`BTreeSet<T>`](std::collections::BTreeSet) where `T: Display`
//!
//! You can extend this trait to your own types by implementing the trait for
//! your type. For example, if you want to implement the trait for your own
//! struct `MyString`, you can do so by implementing the trait for the type:
//!
//! ```rust
//! use preprocess::validators::Contains;
//!
//! pub struct MyString(String);
//!
//! impl Contains for MyString {
//! fn contains(&self, needle: &str) -> bool {
//! self.0.to_string() == needle
//! }
//! }
//! ```
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(contains = "foo")]
//! pub my_string: String,
//! }
//! ```
//!
//! # Does Not Contain
//!
//! The `does_not_contain` validator checks if the given value does not contain
//! the given substring using the [`Contains`](crate::validators::Contains)
//! trait. This is the opposite of the
//! [`contains`](crate::validators::validate_contains) validator.
//! For all practical purposes, this validator is basically `!contains`.
//!
//! The `lowercase` preprocessor converts all the characters in the given value
//! to lowercase using the [`to_lowercase`](`str::to_lowercase`) method.
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocessor(lowercase)]
//! pub my_string: String,
//! }
//! ```
//!
//! # Domain
//!
//! The `domain` validator checks if the given value is a valid domain name.
//! IP addresses are not allowed. To validate IP addresses, use the
//! [`validate_ip`](crate::validators::validate_ip) validator.
//!
//! Examples of valid domain names:
//!
//! - `google.com`
//! - `wikipedia.org`
//! - `stackoverflow.net`
//! - `mail.google.com`
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(domain)]
//! pub domain: String,
//! }
//! ```
//!
//! # Email
//!
//! The `email` validator checks if the given value is a valid email address.
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(email)]
//! pub email: String,
//! }
//! ```
//!
//! # IP
//!
//! The `ip` validator checks if the given value is a valid IP address. This
//! validator will change the type of the field to [`IpAddr`](std::net::IpAddr)
//! if the validation is successful. Both IPv4 and IPv6 addresses are supported.
//! For specific validation, use the
//! [`validate_ipv4`](crate::validators::validate_ipv4) and
//! [`validate_ipv6`](crate::validators::validate_ipv6) validators (using the
//! `ipv4` and `ipv6` preprocessor respectively).
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(ip)]
//! pub ip: String, // This type will be changed to IpAddr
//! }
//! ```
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocessor(ipv4)]
//! pub ipv4: String, // This type will be changed to Ipv4Addr
//! }
//! ```
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocessor(ipv6)]
//! pub ipv6: String, // This type will be changed to Ipv6Addr
//! }
//! ```
//!
//! # Length
//!
//! The `length` validator checks if the length of the given value is within the
//! given range, or exactly equal to the given value. The length of the value is
//! calculated using the [`HasLen`](crate::validators::HasLen) trait. By
//! default, this trait is implemented for the following types:
//!
//! - [`String`](std::string::String)
//! - [`&str`](str)
//! - [`Cow<'a, str>`](std::borrow::Cow)
//! - [`Vec<T>`](std::vec::Vec) where `T: Display`
//! - [`&[T]`](std::slice) where `T: Display`
//! - [`[T; N]`] where `T: Display` and `N` is any constant
//! - [`HashMap<K, V>`](std::collections::HashMap) where `K: Display`
//! - [`HashSet<T>`](std::collections::HashSet) where `T: Display`
//! - [`BTreeMap<K, V>`](std::collections::BTreeMap) where `K: Display`
//! - [`BTreeSet<T>`](std::collections::BTreeSet) where `T: Display`
//!
//! You can extend this trait to your own types by implementing the trait for
//! your type. For example, if you want to implement the trait for your own
//! struct `MyString`, you can do so by implementing the trait for the type:
//!
//! ```rust
//! use preprocess::validators::HasLen;
//!
//! pub struct MyString(String);
//!
//! impl HasLen for MyString {
//! fn length(&self) -> usize {
//! self.0.to_string().len()
//! }
//! }
//! ```
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(length(min = 5, max = 10))]
//! pub my_string: String,
//! }
//! ```
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(length(equal = 5))]
//! // You can also use #[preprocess(length = 5)] as a shorthand
//! pub my_string: String,
//! }
//! ```
//!
//! __Note:__ At least one of `min`, `max` or `equal` must be specified.
//!
//! # Range
//!
//! The `range` validator checks if the given value is within the given range.
//! The range is exclusive of both the start and end values. The range is
//! checked using the [`PartialOrd`] trait.
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(range(min = 5, max = 10))]
//! pub my_string: String,
//! }
//! ```
//!
//! # Regex
//!
//! The `regex` validator checks if the given value matches the given regular
//! expression. The regular expression is checked using the
//! [`Regex`](::regex::Regex) type. The regex is stored in a global, thread-safe
//! map to avoid recompiling the regex every time. This way, the regex is only
//! compiled once and then reused for every validation.
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(regex = r"^[a-zA-Z0-9]+$")]
//! pub my_string: String,
//! }
//! ```
//!
//! # URL
//!
//! The `url` validator checks if the given value is a valid URL. This validator
//! will change the type of the field to [`Url`](::url::Url) if the validation
//! is successful.
//!
//! ## Usage
//!
//! ```rust
//! #[preprocess::sync]
//! pub struct MyStruct {
//! #[preprocess(url)]
//! pub url: String, // This type will be changed to Url
//! }
//! ```
/// Validate if the given value contains the given substring.
/// Validate if the given value does not contain the given substring.
/// Validate if the given value is a valid domain name.
/// Validate if the given value is a valid email address.
/// Empty validator. This validator will always return `Ok` with the given
/// value. This is used for the `#[preprocess(none)]` attribute.
/// Validate if the given value is a valid IP address.
/// Validate if the given value has the required length.
/// Validate if the given value is within the given range.
/// Validate if the given value matches the given regular expression.
/// Validate if the given value is a valid URL.
pub use ;