-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation.rs
More file actions
395 lines (340 loc) · 12 KB
/
Copy pathvalidation.rs
File metadata and controls
395 lines (340 loc) · 12 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Validation Example
//!
//! Demonstrates the Validation type and error accumulation patterns.
//! Shows practical patterns including:
//! - Basic validation (Success/Failure)
//! - Combining multiple validations
//! - Error accumulation with Semigroup
//! - Mapping and transforming validations
//! - Business rule validations
use stillwater::Validation;
// ==================== Basic Validation ====================
/// Example 1: Simple validation
///
/// Demonstrates creating Success and Failure validations.
fn example_basic_validation() {
println!("\n=== Example 1: Basic Validation ===");
// Success case
let success: Validation<i32, String> = Validation::success(42);
println!("Success: {:?}", success);
// Failure case
let failure: Validation<i32, String> = Validation::failure("Something went wrong".to_string());
println!("Failure: {:?}", failure);
// Pattern matching
match success {
Validation::Success(value) => println!("Got value: {}", value),
Validation::Failure(error) => println!("Got error: {}", error),
}
}
// ==================== Simple Validation Functions ====================
/// Example 2: Building validation functions
///
/// Demonstrates creating reusable validation functions.
fn example_validation_functions() {
println!("\n=== Example 2: Validation Functions ===");
// Validation function: check if number is positive
fn validate_positive(n: i32) -> Validation<i32, String> {
if n > 0 {
Validation::success(n)
} else {
Validation::failure(format!("{} is not positive", n))
}
}
// Validation function: check if number is even
fn validate_even(n: i32) -> Validation<i32, String> {
if n % 2 == 0 {
Validation::success(n)
} else {
Validation::failure(format!("{} is not even", n))
}
}
println!("Validate positive:");
println!(" 10: {:?}", validate_positive(10));
println!(" -5: {:?}", validate_positive(-5));
println!("\nValidate even:");
println!(" 10: {:?}", validate_even(10));
println!(" 7: {:?}", validate_even(7));
}
// ==================== Combining Validations ====================
/// Example 3: Combining multiple validations
///
/// Demonstrates using and() to chain validations sequentially.
fn example_combining_validations() {
println!("\n=== Example 3: Combining Validations ===");
fn validate_positive(n: i32) -> Validation<i32, String> {
if n > 0 {
Validation::success(n)
} else {
Validation::failure(format!("{} must be positive", n))
}
}
fn validate_even(n: i32) -> Validation<i32, String> {
if n % 2 == 0 {
Validation::success(n)
} else {
Validation::failure(format!("{} must be even", n))
}
}
fn validate_less_than_100(n: i32) -> Validation<i32, String> {
if n < 100 {
Validation::success(n)
} else {
Validation::failure(format!("{} must be less than 100", n))
}
}
// Chain validations with and()
let result1 = validate_positive(50)
.and(validate_even(50))
.and(validate_less_than_100(50));
println!("Validate 50 (positive, even, <100): {:?}", result1);
// Fails on first check
let result2 = validate_positive(-10)
.and(validate_even(-10))
.and(validate_less_than_100(-10));
println!("Validate -10: {:?}", result2);
// Passes first but fails second
let result3 = validate_positive(7)
.and(validate_even(7))
.and(validate_less_than_100(7));
println!("Validate 7: {:?}", result3);
}
// ==================== Error Accumulation ====================
/// Example 4: Accumulating errors from multiple validations
///
/// Demonstrates using all() to collect ALL errors, not just the first one.
/// This is the key feature that distinguishes Validation from Result.
fn example_error_accumulation() {
println!("\n=== Example 4: Error Accumulation ===");
fn validate_min_length(s: &str, min: usize) -> Validation<(), Vec<String>> {
if s.len() >= min {
Validation::success(())
} else {
Validation::failure(vec![format!(
"Must be at least {} characters (got {})",
min,
s.len()
)])
}
}
fn validate_has_uppercase(s: &str) -> Validation<(), Vec<String>> {
if s.chars().any(|c| c.is_uppercase()) {
Validation::success(())
} else {
Validation::failure(vec![
"Must contain at least one uppercase letter".to_string()
])
}
}
fn validate_has_number(s: &str) -> Validation<(), Vec<String>> {
if s.chars().any(|c| c.is_numeric()) {
Validation::success(())
} else {
Validation::failure(vec!["Must contain at least one number".to_string()])
}
}
fn validate_password(password: &str) -> Validation<String, Vec<String>> {
let v1 = validate_min_length(password, 8);
let v2 = validate_has_uppercase(password);
let v3 = validate_has_number(password);
Validation::<((), (), ()), Vec<String>>::all((v1, v2, v3)).map(|_| password.to_string())
}
// Good password
println!("Password 'Secret123':");
match validate_password("Secret123") {
Validation::Success(_) => println!(" Valid!"),
Validation::Failure(errors) => {
println!(" Invalid:");
for error in errors {
println!(" - {}", error);
}
}
}
// Bad password - accumulates ALL errors
println!("\nPassword 'weak':");
match validate_password("weak") {
Validation::Success(_) => println!(" Valid!"),
Validation::Failure(errors) => {
println!(" Invalid ({} errors):", errors.len());
for error in errors {
println!(" - {}", error);
}
}
}
}
// ==================== Mapping and Transforming ====================
/// Example 5: Using map to transform validated values
///
/// Demonstrates how to transform values inside Validation.
fn example_mapping() {
println!("\n=== Example 5: Mapping and Transforming ===");
fn validate_age(age: i32) -> Validation<i32, String> {
if (0..=150).contains(&age) {
Validation::success(age)
} else {
Validation::failure(format!("Age {} is invalid", age))
}
}
// Transform age to age category
let result = validate_age(25).map(|age| {
if age < 18 {
"Minor"
} else if age < 65 {
"Adult"
} else {
"Senior"
}
});
println!("Age 25 category: {:?}", result);
// Map over failure - doesn't execute
let result2 = validate_age(200).map(|age| format!("Valid age: {}", age));
println!("Age 200: {:?}", result2);
}
// ==================== Business Rules Validation ====================
/// Example 6: Practical business rules validation
///
/// Demonstrates a real-world scenario: validating an order.
fn example_business_rules() {
println!("\n=== Example 6: Business Rules Validation ===");
#[derive(Debug)]
struct Order {
quantity: u32,
unit_price: f64,
discount_percent: f64,
}
impl Order {
fn total(&self) -> f64 {
let subtotal = self.quantity as f64 * self.unit_price;
subtotal * (1.0 - self.discount_percent / 100.0)
}
}
fn validate_quantity(qty: u32) -> Validation<(), Vec<String>> {
if qty > 0 && qty <= 1000 {
Validation::success(())
} else {
Validation::failure(vec![format!(
"Quantity must be between 1 and 1000 (got {})",
qty
)])
}
}
fn validate_price(price: f64) -> Validation<(), Vec<String>> {
if price > 0.0 {
Validation::success(())
} else {
Validation::failure(vec![format!("Price must be positive (got {})", price)])
}
}
fn validate_discount(discount: f64) -> Validation<(), Vec<String>> {
if (0.0..=100.0).contains(&discount) {
Validation::success(())
} else {
Validation::failure(vec![format!(
"Discount must be between 0 and 100 (got {})",
discount
)])
}
}
fn validate_order(order: &Order) -> Validation<Order, Vec<String>> {
let v1 = validate_quantity(order.quantity);
let v2 = validate_price(order.unit_price);
let v3 = validate_discount(order.discount_percent);
Validation::<((), (), ()), Vec<String>>::all((v1, v2, v3)).map(|_| Order {
quantity: order.quantity,
unit_price: order.unit_price,
discount_percent: order.discount_percent,
})
}
// Valid order
let valid_order = Order {
quantity: 10,
unit_price: 99.99,
discount_percent: 10.0,
};
println!("\nValid order:");
match validate_order(&valid_order) {
Validation::Success(order) => {
println!(" Order validated!");
println!(" Total: ${:.2}", order.total());
}
Validation::Failure(errors) => {
for error in errors {
println!(" - {}", error);
}
}
}
// Invalid order - multiple errors
let invalid_order = Order {
quantity: 0,
unit_price: -10.0,
discount_percent: 150.0,
};
println!("\nInvalid order:");
match validate_order(&invalid_order) {
Validation::Success(_) => println!(" Order validated!"),
Validation::Failure(errors) => {
println!(" {} validation errors:", errors.len());
for error in errors {
println!(" - {}", error);
}
}
}
}
// ==================== Collecting Results ====================
/// Example 7: Validating collections with all_vec
///
/// Demonstrates validating a collection of items.
fn example_collection_validation() {
println!("\n=== Example 7: Collection Validation ===");
fn validate_email(email: &str) -> Validation<String, Vec<String>> {
if email.contains('@') && email.len() > 3 {
Validation::success(email.to_string())
} else {
Validation::failure(vec![format!("Invalid email: {}", email)])
}
}
let emails = ["alice@example.com", "bob@test.com", "charlie@mail.org"];
println!("Valid emails:");
let validations: Vec<_> = emails.iter().map(|e| validate_email(e)).collect();
match Validation::all_vec(validations) {
Validation::Success(valid_emails) => {
println!(" All {} emails are valid:", valid_emails.len());
for email in valid_emails {
println!(" - {}", email);
}
}
Validation::Failure(errors) => {
println!(" Validation failed:");
for error in errors {
println!(" - {}", error);
}
}
}
// Mix of valid and invalid
let mixed_emails = ["alice@example.com", "invalid", "bob@test.com", "bad"];
println!("\nMixed emails:");
let validations: Vec<_> = mixed_emails.iter().map(|e| validate_email(e)).collect();
match Validation::all_vec(validations) {
Validation::Success(valid_emails) => {
println!(" All emails are valid: {:?}", valid_emails);
}
Validation::Failure(errors) => {
println!(" {} validation errors:", errors.len());
for error in errors {
println!(" - {}", error);
}
}
}
}
// ==================== Main ====================
fn main() {
println!("Validation Examples");
println!("===================");
example_basic_validation();
example_validation_functions();
example_combining_validations();
example_error_accumulation();
example_mapping();
example_business_rules();
example_collection_validation();
println!("\n=== All examples completed successfully! ===");
}