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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# Error Handling

**Working with ValidationError, Violation, and structured error responses.**

## Table of Contents

- [ValidationError Construction]#validationerror-construction
- [Adding Violations]#adding-violations
- [Violation Structure]#violation-structure
- [Error Accumulation]#error-accumulation
- [Extracting Error Information]#extracting-error-information
- [Message Transformation]#message-transformation
- [HTTP Integration]#http-integration
- [Best Practices]#best-practices

## ValidationError Construction

### Creating Empty Errors

```rust
use domainstack::prelude::*;

// Create empty error accumulator
let mut err = ValidationError::new();

// Or use Default
let err = ValidationError::default();

// Check if empty
if err.is_empty() {
    println!("No errors!");
}
```

### Single Error Constructor

For cases where you have exactly one error:

```rust
let err = ValidationError::single(
    "email",
    "invalid_email",
    "Invalid email format"
);

// Equivalent to:
let mut err = ValidationError::new();
err.push("email", "invalid_email", "Invalid email format");
```

### Incremental Construction

Build up errors by validating multiple fields:

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

        // Validate each field
        if let Err(e) = validate("name", &self.name, &rules::min_len(1)) {
            err.extend(e);
        }

        if let Err(e) = validate("email", &self.email, &rules::email()) {
            err.extend(e);
        }

        if let Err(e) = validate("age", &self.age, &rules::range(18, 120)) {
            err.extend(e);
        }

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

## Adding Violations

### Basic push()

Add violations with field path, error code, and message:

```rust
let mut err = ValidationError::new();

// String field path
err.push("email", "invalid_email", "Invalid email format");

// Nested path
err.push("guest.email", "required", "Email is required");

// Array index path
err.push("rooms[0].adults", "out_of_range", "Must be between 1 and 4");
```

### Path Types

The `push()` method accepts anything that implements `Into<Path>`:

```rust
// String slice
err.push("email", "invalid", "Invalid email");

// String
err.push("email".to_string(), "invalid", "Invalid email");

// Path API (type-safe)
let path = Path::root().field("rooms").index(0).field("adults");
err.push(path, "out_of_range", "Must be between 1 and 4");
```

### Error Codes

Error codes are `&'static str` for zero-allocation:

```rust
// Built-in rule codes
err.push("email", "invalid_email", "Invalid email format");
err.push("age", "out_of_range", "Must be between 18 and 120");

// Custom codes
err.push("password", "weak_password", "Password too weak");
err.push("username", "username_taken", "Username already exists");
```

**Common error codes:**
- `required` - Missing required field
- `invalid_email` - Email format invalid
- `invalid_url` - URL format invalid
- `min_length` - String too short
- `max_length` - String too long
- `out_of_range` - Number outside valid range
- `pattern_mismatch` - Regex pattern failed
- `min_items` - Collection too small
- `max_items` - Collection too large

See [RULES.md](RULES.md) for complete list of rule codes.

## Violation Structure

Each violation contains:

```rust
pub struct Violation {
    pub path: Path,           // Field path (e.g., "guest.email")
    pub code: &'static str,   // Machine-readable code
    pub message: String,      // Human-readable message
    pub meta: Meta,           // Additional context
}
```

### Accessing Violations

```rust
let err = ValidationError::single("email", "invalid_email", "Invalid email");

for v in &err.violations {
    println!("Path: {}", v.path);          // "email"
    println!("Code: {}", v.code);          // "invalid_email"
    println!("Message: {}", v.message);    // "Invalid email"
    println!("Meta: {:?}", v.meta);        // Meta { ... }
}
```

### Meta Fields

Violations can include additional context:

```rust
// Rules automatically populate meta fields
let rule = rules::range(18, 120);
// On failure, includes: meta.insert("min", 18); meta.insert("max", 120);

let rule = rules::max_len(255);
// On failure, includes: meta.insert("max", 255);
```

**Accessing meta:**

```rust
for v in &err.violations {
    if let Some(min) = v.meta.get("min") {
        println!("Minimum value: {}", min);
    }

    if let Some(max) = v.meta.get("max") {
        println!("Maximum value: {}", max);
    }
}
```

**Common meta keys:**
- `min`, `max` - Range bounds
- `actual` - Actual value that failed
- `expected` - Expected value
- `pattern` - Regex pattern that failed

## Error Accumulation

### extend() - Same-Level Errors

Use `extend()` when merging errors from the **same level** (no path change):

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

        // Validate name (path: "name")
        if let Err(e) = validate("name", &self.name, &name_rule) {
            err.extend(e);  // Path stays "name"
        }

        // Validate age (path: "age")
        if let Err(e) = validate("age", &self.age, &age_rule) {
            err.extend(e);  // Path stays "age"
        }

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

### merge_prefixed() - Nested Errors

Use `merge_prefixed()` when merging errors from **nested types** (add path prefix):

```rust
// Email validates itself with path "value"
impl Validate for Email {
    fn validate(&self) -> Result<(), ValidationError> {
        validate("value", self.0.as_str(), &rules::email())
        // Error path: "value"
    }
}

// User needs to prefix "email" when merging
impl Validate for User {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        if let Err(e) = self.email.validate() {
            err.merge_prefixed("email", e);
            // "value" becomes "email.value"
        }

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

### prefixed() - Transform All Paths

Transform an entire error by adding a prefix:

```rust
// Validate booking
let booking_err = booking.validate();  // Paths: "check_in", "check_out"

// Add prefix to all paths
let prefixed_err = booking_err.prefixed("booking");
// Paths become: "booking.check_in", "booking.check_out"
```

**When to use:**
- Converting DTO → Domain at HTTP boundary
- Nesting validation in parent structures
- Scoping errors to subsystems

### Collection Validation Pattern

Combine array indices with nested errors:

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

        for (i, member) in self.members.iter().enumerate() {
            if let Err(e) = member.validate() {
                // Build path with index
                let path = Path::root().field("members").index(i);
                err.merge_prefixed(path, e);
                // Produces: "members[0].email", "members[1].name", etc.
            }
        }

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

## Extracting Error Information

### field_violations_map() - Recommended

Get violations grouped by field path (preserves codes and meta):

```rust
let mut err = ValidationError::new();
err.push("email", "invalid_email", "Invalid email format");
err.push("email", "max_length", "Email too long");
err.push("age", "out_of_range", "Age out of range");

let map = err.field_violations_map();
// BTreeMap<String, Vec<&Violation>>

for (field, violations) in map {
    println!("Field: {}", field);
    for v in violations {
        println!("  - [{}] {}", v.code, v.message);
    }
}

// Output:
// Field: age
//   - [out_of_range] Age out of range
// Field: email
//   - [invalid_email] Invalid email format
//   - [max_length] Email too long
```

**Why use this:**
- Preserves error codes (needed for client-side handling)
- Preserves meta fields (needed for context)
- Enables proper error classification
- Supports internationalization

### field_errors_map() - Deprecated

Get only error messages grouped by field (loses codes and meta):

```rust
#[allow(deprecated)]
let map = err.field_errors_map();
// BTreeMap<String, Vec<String>>

for (field, messages) in map {
    println!("{}: {:?}", field, messages);
}

// Output:
// email: ["Invalid email format", "Email too long"]
// age: ["Age out of range"]
```

**⚠️ Warning:** This method only returns messages and **loses error codes and metadata**. Use `field_violations_map()` instead for complete error information.

### Direct Violation Access

Iterate over violations directly:

```rust
for v in &err.violations {
    match v.code {
        "invalid_email" => handle_email_error(v),
        "out_of_range" => handle_range_error(v),
        _ => handle_generic_error(v),
    }
}
```

## Message Transformation

### map_messages() - Internationalization

Transform all violation messages (useful for i18n):

```rust
let mut err = ValidationError::new();
err.push("email", "invalid_email", "Invalid email");
err.push("age", "out_of_range", "Must be 18+");

// Translate to Spanish
let err = err.map_messages(|msg| {
    match msg.as_str() {
        "Invalid email" => "Email inválido".to_string(),
        "Must be 18+" => "Debe tener 18 años o más".to_string(),
        _ => msg,
    }
});

assert_eq!(err.violations[0].message, "Email inválido");
assert_eq!(err.violations[1].message, "Debe tener 18 años o más");
```

### Code-Based Translation

Use error codes for proper i18n:

```rust
fn translate(err: ValidationError, lang: &str) -> ValidationError {
    err.map_messages(|_msg| {
        // Ignore English message, translate from code
        format!("Translated to {}", lang)
    })
}

// Better: Use error codes + meta for contextual translation
fn translate_with_context(v: &Violation, lang: &str) -> String {
    match (v.code, lang) {
        ("out_of_range", "es") => {
            let min = v.meta.get("min").unwrap();
            let max = v.meta.get("max").unwrap();
            format!("Debe estar entre {} y {}", min, max)
        }
        ("invalid_email", "es") => "Email inválido".to_string(),
        _ => v.message.clone(),
    }
}
```

### filter() - Conditional Errors

Remove certain violations based on criteria:

```rust
let mut err = ValidationError::new();
err.push("email", "warning", "Email format questionable");
err.push("age", "invalid", "Age is required");
err.push("name", "warning", "Name seems unusual");

// Remove warnings, keep only errors
let err = err.filter(|v| v.code != "warning");

assert_eq!(err.violations.len(), 1);
assert_eq!(err.violations[0].code, "invalid");
```

**Use cases:**
- Separating warnings from errors
- Filtering by severity level
- Removing certain error types conditionally

## HTTP Integration

### error-envelope Integration

Convert ValidationError to structured HTTP responses:

```rust
use domainstack_envelope::IntoEnvelopeError;
use axum::{Json, http::StatusCode};

async fn create_user(
    Json(dto): Json<UserDto>
) -> Result<Json<User>, ErrorResponse> {
    // Convert DTO → Domain
    let user = User::try_from(dto)
        .map_err(|e| e.into_envelope_error())?;

    // Save user...
    Ok(Json(user))
}
```

### Structured Error Response Format

Validation errors are serialized to structured error responses:

```json
{
  "code": "VALIDATION",
  "status": 400,
  "message": "Validation failed with 2 errors",
  "retryable": false,
  "details": {
    "fields": {
      "guest.email.value": [
        {
          "code": "invalid_email",
          "message": "Invalid email format",
          "meta": {"max": 255}
        }
      ],
      "rooms[1].adults": [
        {
          "code": "out_of_range",
          "message": "Must be between 1 and 4",
          "meta": {"min": 1, "max": 4}
        }
      ]
    }
  }
}
```

### Client-Side Handling

The structured format maps cleanly to frontend forms:

```typescript
// TypeScript example
interface ErrorResponse {
  code: string;
  status: number;
  message: string;
  details: {
    fields: {
      [fieldPath: string]: Array<{
        code: string;
        message: string;
        meta?: Record<string, string>;
      }>;
    };
  };
}

function displayErrors(response: ErrorResponse) {
  const fields = response.details.fields;

  for (const [path, errors] of Object.entries(fields)) {
    // Map directly to form fields
    const inputElement = document.querySelector(`[name="${path}"]`);

    errors.forEach(err => {
      showErrorAtField(inputElement, err.message);

      // Use error codes for custom handling
      if (err.code === "invalid_email") {
        highlightEmailField(inputElement);
      }
    });
  }
}
```

**React Hook Form example:**

```typescript
const { setError } = useFormContext();

// Map validation errors to form fields
for (const [path, errors] of Object.entries(response.details.fields)) {
  setError(path, {
    type: errors[0].code,
    message: errors[0].message,
  });
}
```

### Custom Error Responses

Build custom error responses for your API:

```rust
use serde::Serialize;

#[derive(Serialize)]
struct ApiError {
    success: false,
    errors: BTreeMap<String, Vec<ErrorDetail>>,
}

#[derive(Serialize)]
struct ErrorDetail {
    code: &'static str,
    message: String,
}

fn to_api_error(err: ValidationError) -> ApiError {
    let mut errors = BTreeMap::new();

    for (path, violations) in err.field_violations_map() {
        let details = violations.iter().map(|v| ErrorDetail {
            code: v.code,
            message: v.message.clone(),
        }).collect();

        errors.insert(path, details);
    }

    ApiError { success: false, errors }
}
```

## Best Practices

### 1. Always Use Error Codes

Error codes enable proper client-side handling:

```rust
// GOOD: Include error codes
err.push("email", "invalid_email", "Invalid email format");

// [x] BAD: Generic or missing codes
err.push("email", "error", "Something went wrong");
```

### 2. extend() vs merge_prefixed()

Choose the right method for error merging:

```rust
// Use extend() for same-level validation
if let Err(e) = validate("name", &self.name, &name_rule) {
    err.extend(e);  // Path stays "name"
}

// Use merge_prefixed() for nested types
if let Err(e) = self.email.validate() {
    err.merge_prefixed("email", e);  // Adds "email." prefix
}
```

### 3. Preserve Complete Error Information

Use `field_violations_map()` instead of deprecated `field_errors_map()`:

```rust
// GOOD: Preserves codes and meta
let map = err.field_violations_map();
for (field, violations) in map {
    for v in violations {
        log_error(field, v.code, &v.message, &v.meta);
    }
}

// [x] BAD: Loses codes and meta
#[allow(deprecated)]
let map = err.field_errors_map();  // Only messages!
```

### 4. Use Path API for Complex Paths

Type-safe path building prevents typos:

```rust
// GOOD: Type-safe path building
let path = Path::root()
    .field("booking")
    .field("rooms")
    .index(0)
    .field("guest")
    .field("email");
err.push(path, "invalid_email", "Invalid email");

// [x] BAD: String concatenation (error-prone)
err.push("booking.rooms[0].guest.email", "invalid_email", "Invalid email");
```

### 5. Include Context in Meta Fields

Help clients display better error messages:

```rust
// GOOD: Include useful context
let mut err = ValidationError::new();
let mut violation = Violation {
    path: "age".into(),
    code: "out_of_range",
    message: "Must be between 18 and 120".to_string(),
    meta: Meta::new(),
};
violation.meta.insert("min", "18");
violation.meta.insert("max", "120");
violation.meta.insert("actual", &self.age.to_string());
err.violations.push(violation);

// [x] BAD: No context
err.push("age", "out_of_range", "Invalid age");
```

### 6. Test Error Paths

Verify error paths match your frontend expectations:

```rust
#[test]
fn test_nested_validation_paths() {
    let mut booking = Booking::new();
    booking.rooms[0].adults = 10;  // Invalid

    let err = booking.validate().unwrap_err();

    assert_eq!(err.violations[0].path.to_string(), "rooms[0].adults");
    assert_eq!(err.violations[0].code, "out_of_range");
}
```

### 7. Fail-Slow Accumulation

Collect **all** errors before returning:

```rust
// GOOD: Fail-slow - collect all errors
impl Validate for User {
    fn validate(&self) -> Result<(), ValidationError> {
        let mut err = ValidationError::new();

        if let Err(e) = validate("name", &self.name, &name_rule) {
            err.extend(e);
        }

        if let Err(e) = validate("email", &self.email, &email_rule) {
            err.extend(e);
        }

        if let Err(e) = validate("age", &self.age, &age_rule) {
            err.extend(e);
        }

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

// [x] BAD: Fail-fast - only first error
impl Validate for User {
    fn validate(&self) -> Result<(), ValidationError> {
        validate("name", &self.name, &name_rule)?;  // Stops here on error!
        validate("email", &self.email, &email_rule)?;
        validate("age", &self.age, &age_rule)?;
        Ok(())
    }
}
```

**Why fail-slow matters:**
- Users see **all** validation errors at once
- Better UX - fix all issues in one attempt
- Reduces frustration from incremental error discovery

### 8. Display Implementation

ValidationError implements `Display` for logging:

```rust
let err = ValidationError::single("email", "invalid", "Invalid email");

// Automatic formatting
println!("{}", err);  // "Validation error: Invalid email"

// Multiple errors
let mut err = ValidationError::new();
err.push("email", "invalid", "Invalid email");
err.push("age", "required", "Age required");

println!("{}", err);  // "Validation failed with 2 errors"
```

## See Also

- [Core Concepts]CORE_CONCEPTS.md - Valid-by-construction types and domain modeling
- [Manual Validation]MANUAL_VALIDATION.md - Implementing Validate trait manually
- [HTTP Integration]HTTP_INTEGRATION.md - Framework adapters and error responses
- [Rules Reference]RULES.md - Complete list of validation rules and error codes