domainstack 1.1.1

Write validation once, use everywhere: Rust rules auto-generate JSON Schema + OpenAPI + TypeScript/Zod. WASM browser validation. Axum/Actix/Rocket adapters.
Documentation
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# Cross-Field Validation

**Complete guide to validating relationships between fields: date ranges, password confirmation, conditional requirements, and complex business rules.**

## Table of Contents

- [Overview]#overview
- [Derive Macro Approach]#derive-macro-approach
- [Manual Implementation]#manual-implementation
- [Common Patterns]#common-patterns
- [Conditional Cross-Field Validation]#conditional-cross-field-validation
- [Complex Business Rules]#complex-business-rules
- [Error Handling]#error-handling
- [Best Practices]#best-practices

## Overview

Cross-field validation enforces relationships between multiple fields in a struct. Unlike single-field rules (email format, string length), cross-field validation answers questions like:

- Is `check_out` after `check_in`?
- Does `password` match `password_confirmation`?
- Is `discount` only applied when `total > 100`?
- Is `shipping_address` required when `requires_shipping` is true?

## Derive Macro Approach

Use struct-level `#[validate(...)]` attributes for declarative cross-field validation.

### Basic Syntax

```rust
use domainstack::prelude::*;
use chrono::{DateTime, Utc};

#[derive(Validate)]
#[validate(
    check = "self.end_date > self.start_date",
    code = "invalid_date_range",
    message = "End date must be after start date"
)]
struct DateRange {
    start_date: DateTime<Utc>,
    end_date: DateTime<Utc>,
}
```

**Parameters:**

| Parameter | Required | Description |
|-----------|----------|-------------|
| `check` | Yes | Rust expression that evaluates to `bool`. Use `self.` to reference fields. |
| `code` | Yes | Machine-readable error code (used by frontends, logging, i18n) |
| `message` | Yes | Human-readable error message |
| `when` | No | Condition that must be true for validation to run |

### Date Range Validation

The most common cross-field validation pattern:

```rust
use domainstack::prelude::*;
use chrono::{DateTime, Utc, Duration};

#[derive(Validate)]
#[validate(
    check = "self.end_date > self.start_date",
    code = "invalid_date_range",
    message = "End date must be after start date"
)]
struct EventBooking {
    #[validate(future)]
    start_date: DateTime<Utc>,

    #[validate(future)]
    end_date: DateTime<Utc>,

    #[validate(length(min = 1, max = 100))]
    event_name: String,
}

// Usage
let booking = EventBooking {
    start_date: Utc::now() + Duration::days(1),
    end_date: Utc::now() + Duration::days(30),
    event_name: "Conference".to_string(),
};
booking.validate()?;  // [ok] Valid

let invalid = EventBooking {
    start_date: Utc::now() + Duration::days(30),
    end_date: Utc::now() + Duration::days(1),  // Before start!
    event_name: "Conference".to_string(),
};
invalid.validate()?;  // [error] Error: invalid_date_range
```

### Password Confirmation

```rust
#[derive(Validate)]
#[validate(
    check = "self.password == self.password_confirmation",
    code = "password_mismatch",
    message = "Passwords do not match"
)]
struct PasswordChange {
    #[validate(length(min = 8, max = 128))]
    password: String,

    password_confirmation: String,
}

// Usage
let change = PasswordChange {
    password: "SecureP@ss123".to_string(),
    password_confirmation: "SecureP@ss123".to_string(),
};
change.validate()?;  // [ok] Valid

let mismatch = PasswordChange {
    password: "SecureP@ss123".to_string(),
    password_confirmation: "DifferentPass".to_string(),
};
mismatch.validate()?;  // [error] Error: password_mismatch
```

### Multiple Cross-Field Rules

Stack multiple struct-level validations:

```rust
#[derive(Validate)]
#[validate(
    check = "self.end_date > self.start_date",
    code = "invalid_date_range",
    message = "End date must be after start date"
)]
#[validate(
    check = "self.total >= self.minimum_order",
    code = "below_minimum",
    message = "Order total is below minimum"
)]
#[validate(
    check = "self.discount <= self.total * 0.5",
    code = "discount_too_high",
    message = "Discount cannot exceed 50% of total"
)]
struct Order {
    start_date: DateTime<Utc>,
    end_date: DateTime<Utc>,
    total: f64,
    minimum_order: f64,
    discount: f64,
}
```

**Execution order:**
1. All field-level validations run first
2. All struct-level `#[validate(check = "...")]` rules run after
3. All violations accumulate (fail-slow)

## Manual Implementation

For complex business logic that doesn't fit in a single expression, implement `Validate` manually.

### When to Use Manual Implementation

- Complex calculations (e.g., duration checks)
- Multiple related checks with shared logic
- Dynamic error messages based on calculated values
- Early returns for performance
- Conditional validation based on complex state

### Basic Manual Cross-Field Validation

```rust
use domainstack::prelude::*;
use chrono::{DateTime, Utc, Duration};

pub struct EventBooking {
    pub start_date: DateTime<Utc>,
    pub end_date: DateTime<Utc>,
    pub event_name: String,
}

impl Validate for EventBooking {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // Field-level validation
        if let Err(e) = validate("event_name", self.event_name.as_str(),
                                 &rules::length(1, 100)) {
            err.extend(e);
        }

        // Cross-field: end must be after start
        if self.end_date <= self.start_date {
            err.push(
                "end_date",
                "invalid_date_range",
                "End date must be after start date"
            );
        }

        // Cross-field: minimum duration (1 day)
        let duration = self.end_date.signed_duration_since(self.start_date);
        if duration.num_days() < 1 {
            err.push(
                "end_date",
                "duration_too_short",
                "Event must be at least 1 day long"
            );
        }

        // Cross-field: maximum duration (30 days)
        if duration.num_days() > 30 {
            err.push(
                "end_date",
                "duration_too_long",
                format!("Event cannot exceed 30 days (got {} days)", duration.num_days())
            );
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

### Combining Derive and Manual

Use derive for simple rules, manual for complex ones:

```rust
// Use derive for field-level rules
#[derive(Validate)]
pub struct Order {
    #[validate(positive)]
    pub total: f64,

    #[validate(range(min = 0.0, max = 100.0))]
    pub discount_percent: f64,

    #[validate(min_items = 1)]
    pub items: Vec<OrderItem>,
}

// Override with manual for cross-field
impl Order {
    pub fn validate_business_rules(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // Run derive validation first
        if let Err(e) = self.validate() {
            err.extend(e);
        }

        // Add cross-field business rules
        let discount_amount = self.total * (self.discount_percent / 100.0);
        if discount_amount > 50.0 {
            err.push(
                "discount_percent",
                "discount_exceeds_limit",
                format!("Discount amount ${:.2} exceeds maximum of $50", discount_amount)
            );
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

## Common Patterns

### Date Range with Minimum/Maximum Duration

```rust
#[derive(Validate)]
#[validate(
    check = "self.check_out > self.check_in",
    code = "invalid_date_range",
    message = "Check-out must be after check-in"
)]
struct HotelBooking {
    check_in: NaiveDate,
    check_out: NaiveDate,
}

impl HotelBooking {
    pub fn validate_with_duration_limits(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // Run derive validation
        if let Err(e) = self.validate() {
            err.extend(e);
        }

        // Duration checks
        let nights = (self.check_out - self.check_in).num_days();

        if nights < 1 {
            err.push("check_out", "minimum_stay", "Minimum stay is 1 night");
        }

        if nights > 30 {
            err.push("check_out", "maximum_stay", "Maximum stay is 30 nights");
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

### Numeric Comparison

```rust
#[derive(Validate)]
#[validate(
    check = "self.max_value >= self.min_value",
    code = "invalid_range",
    message = "Maximum must be greater than or equal to minimum"
)]
struct PriceRange {
    #[validate(positive)]
    min_value: f64,

    #[validate(positive)]
    max_value: f64,
}
```

### String Matching

```rust
#[derive(Validate)]
#[validate(
    check = "self.email == self.email_confirmation",
    code = "email_mismatch",
    message = "Email addresses do not match"
)]
struct AccountCreation {
    #[validate(email)]
    email: String,

    email_confirmation: String,
}
```

### Collection Size Dependencies

```rust
#[derive(Validate)]
#[validate(
    check = "self.rooms.len() <= self.max_rooms",
    code = "too_many_rooms",
    message = "Number of rooms exceeds limit"
)]
struct Reservation {
    #[validate(each(nested))]
    rooms: Vec<Room>,

    #[validate(range(min = 1, max = 10))]
    max_rooms: usize,
}
```

## Conditional Cross-Field Validation

Use the `when` parameter to conditionally run cross-field validation.

### Basic Conditional Validation

```rust
#[derive(Validate)]
#[validate(
    check = "self.total >= self.minimum_order",
    code = "below_minimum",
    message = "Order total is below minimum",
    when = "self.requires_minimum"
)]
struct FlexibleOrder {
    total: f64,
    minimum_order: f64,
    requires_minimum: bool,
}

// Usage
let order = FlexibleOrder {
    total: 50.0,
    minimum_order: 100.0,
    requires_minimum: false,  // Condition is false, validation skipped
};
order.validate()?;  // [ok] Valid - condition not met

let required = FlexibleOrder {
    total: 50.0,
    minimum_order: 100.0,
    requires_minimum: true,  // Condition is true, validation runs
};
required.validate()?;  // [error] Error: below_minimum
```

### Multiple Conditional Rules

```rust
#[derive(Validate)]
#[validate(
    check = "self.shipping_address.is_some()",
    code = "shipping_required",
    message = "Shipping address is required for physical products",
    when = "self.is_physical_product"
)]
#[validate(
    check = "self.download_url.is_some()",
    code = "download_required",
    message = "Download URL is required for digital products",
    when = "!self.is_physical_product"
)]
struct ProductOrder {
    is_physical_product: bool,
    shipping_address: Option<Address>,
    download_url: Option<String>,
}
```

### Enum-Based Conditions

```rust
#[derive(Validate)]
#[validate(
    check = "self.tracking_number.is_some()",
    code = "tracking_required",
    message = "Tracking number required for shipped orders",
    when = "matches!(self.status, OrderStatus::Shipped | OrderStatus::Delivered)"
)]
struct TrackedOrder {
    status: OrderStatus,
    tracking_number: Option<String>,
}

enum OrderStatus {
    Pending,
    Processing,
    Shipped,
    Delivered,
}
```

## Complex Business Rules

Some validation requires calculations or multi-step logic beyond what the derive macro supports.

### Discount Validation with Calculations

```rust
impl Validate for DiscountedOrder {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // Calculate effective discount
        let discount_amount = self.total * (self.discount_percent / 100.0);
        let final_total = self.total - discount_amount;

        // Rule 1: Discount cannot exceed 50% of total
        if self.discount_percent > 50.0 {
            err.push(
                "discount_percent",
                "discount_too_high",
                "Discount cannot exceed 50%"
            );
        }

        // Rule 2: Final total must be at least $10
        if final_total < 10.0 {
            err.push(
                "discount_percent",
                "final_total_too_low",
                format!("Final total ${:.2} is below minimum of $10", final_total)
            );
        }

        // Rule 3: Premium discounts require minimum order
        if self.is_premium_discount && self.total < 100.0 {
            err.push(
                "discount_percent",
                "premium_minimum_order",
                "Premium discounts require orders of $100 or more"
            );
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

### Multi-Field Inventory Validation

```rust
impl Validate for WarehouseTransfer {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // Can't transfer to same warehouse
        if self.source_warehouse == self.destination_warehouse {
            err.push(
                "destination_warehouse",
                "same_warehouse",
                "Cannot transfer to the same warehouse"
            );
        }

        // Quantity must be positive
        if self.quantity <= 0 {
            err.push("quantity", "invalid_quantity", "Quantity must be positive");
        }

        // Transfer date must be in the future
        if self.scheduled_date <= Utc::now() {
            err.push(
                "scheduled_date",
                "past_date",
                "Transfer must be scheduled for a future date"
            );
        }

        // Express transfers have quantity limits
        if self.is_express && self.quantity > 100 {
            err.push(
                "quantity",
                "express_limit",
                "Express transfers limited to 100 units"
            );
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

## Error Handling

### Error Path Strategy

For cross-field errors, choose the most relevant field for the error path:

```rust
// Date range: attach error to end_date (the field being compared)
err.push("end_date", "invalid_date_range", "End must be after start");

// Password match: attach to confirmation field
err.push("password_confirmation", "mismatch", "Passwords do not match");

// Calculation error: attach to the input field
err.push("discount_percent", "final_total_too_low", "Discount too high");
```

### Dynamic Error Messages

Include calculated values in error messages:

```rust
let duration = (self.end_date - self.start_date).num_days();
err.push(
    "end_date",
    "duration_too_long",
    format!("Duration of {} days exceeds maximum of 30 days", duration)
);
```

### Multiple Related Errors

Group related errors logically:

```rust
// Check minimum/maximum range
if self.min_value > self.max_value {
    // Attach to both fields for clear UI feedback
    err.push("min_value", "range_inverted", "Minimum exceeds maximum");
    err.push("max_value", "range_inverted", "Maximum is below minimum");
}
```

## Best Practices

### 1. Use Derive for Simple Comparisons

```rust
// GOOD: Simple comparison in derive
#[validate(
    check = "self.end > self.start",
    code = "invalid_range",
    message = "End must be after start"
)]

// [x] BAD: Complex logic in derive (hard to read/debug)
#[validate(
    check = "self.items.iter().map(|i| i.price).sum::<f64>() == self.total",
    ...
)]
```

### 2. Use Manual for Calculations

```rust
// GOOD: Complex logic in manual implementation
impl Validate for Order {
    fn validate(&self) -> Result<(), ValidationError> {
        let calculated_total: f64 = self.items.iter().map(|i| i.price).sum();
        if (self.total - calculated_total).abs() > 0.01 {
            // ...
        }
    }
}
```

### 3. Include Context in Error Messages

```rust
// GOOD: Specific, actionable message
err.push(
    "end_date",
    "duration_too_short",
    format!("Stay of {} nights is below minimum of 2 nights", nights)
);

// [x] BAD: Generic message
err.push("end_date", "invalid", "Invalid date");
```

### 4. Choose Meaningful Error Codes

```rust
// GOOD: Specific, machine-readable codes
err.push("password_confirmation", "password_mismatch", ...);
err.push("end_date", "before_start_date", ...);
err.push("discount", "exceeds_maximum_percent", ...);

// [x] BAD: Generic codes
err.push("password_confirmation", "invalid", ...);
err.push("end_date", "error", ...);
```

### 5. Run Field Validation Before Cross-Field

```rust
impl Validate for Order {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        // 1. Field-level validation first
        if let Err(e) = validate("total", &self.total, &rules::positive()) {
            err.extend(e);
        }
        if let Err(e) = validate("discount", &self.discount, &rules::range(0.0, 100.0)) {
            err.extend(e);
        }

        // 2. Cross-field validation after (uses validated fields)
        if self.discount > self.total * 0.5 {
            err.push("discount", "too_high", "Discount exceeds 50% of total");
        }

        if err.is_empty() { Ok(()) } else { Err(err) }
    }
}
```

### 6. Test Edge Cases

```rust
#[test]
fn test_date_range_edge_cases() {
    // Same date (boundary)
    let same = DateRange {
        start: date(2025, 1, 1),
        end: date(2025, 1, 1),
    };
    assert!(same.validate().is_err());

    // One day apart (minimum valid)
    let one_day = DateRange {
        start: date(2025, 1, 1),
        end: date(2025, 1, 2),
    };
    assert!(one_day.validate().is_ok());

    // Reversed (invalid)
    let reversed = DateRange {
        start: date(2025, 1, 2),
        end: date(2025, 1, 1),
    };
    assert!(reversed.validate().is_err());
}
```

## See Also

- [Derive Macro]DERIVE_MACRO.md - Field-level validation with `#[derive(Validate)]`
- [Manual Validation]MANUAL_VALIDATION.md - Implementing `Validate` trait
- [Conditional Validation]CONDITIONAL_VALIDATION.md - Runtime-determined validation
- [Error Handling]ERROR_HANDLING.md - Working with `ValidationError`
- [Rules Reference]RULES.md - Built-in validation rules