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
# Domain Modeling Patterns

**Recommended patterns for building valid-by-construction domain types with domainstack.**

## Table of Contents

- [Valid-by-Construction Types]#valid-by-construction-types
- [DTO → Domain Conversion]#dto--domain-conversion
- [Smart Constructors]#smart-constructors
- [Private Fields Pattern]#private-fields-pattern
- [Manual vs Derive]#manual-vs-derive
- [HTTP Integration]#http-integration

## Valid-by-Construction Types

The core principle: **domain types that can only exist in valid states**.

### The Pattern

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

// DTO - Public, for deserialization (untrusted)
#[derive(Deserialize)]
pub struct UserDto {
    pub name: String,
    pub age: u8,
    pub email: String,
}

// Domain - Private fields, enforced validity (trusted)
#[derive(Debug, Validate)]
pub struct User {
    #[validate(length(min = 2, max = 50))]
    name: String,     // Private - can't be set directly!

    #[validate(range(min = 18, max = 120))]
    age: u8,

    #[validate(nested)]
    email: Email,
}

impl User {
    // Smart constructor - validation enforced here
    pub fn new(name: String, age: u8, email_raw: String) -> Result<Self, ValidationError> {
        let email = Email::new(email_raw).map_err(|e| e.prefixed("email"))?;

        let user = Self { name, age, email };
        user.validate()?;  // One line - validates all fields!
        Ok(user)
    }

    // Getters only - no setters
    pub fn name(&self) -> &str { &self.name }
    pub fn age(&self) -> u8 { self.age }
    pub fn email(&self) -> &Email { &self.email }
}
```

### Key Benefits

- **Type safety** - Can't accidentally use unvalidated data
- **Single validation point** - Validate once at construction
- **Self-documenting** - Type signature requires `User`, not raw values
- **Compiler-enforced** - Can't bypass validation
- **Impossible states** - Invalid users literally cannot exist in memory

## DTO → Domain Conversion

Use `TryFrom` to enforce validation at boundaries:

### Basic Conversion

```rust
impl TryFrom<UserDto> for User {
    type Error = ValidationError;

    fn try_from(dto: UserDto) -> Result<Self, Self::Error> {
        User::new(dto.name, dto.age, dto.email)
    }
}

// Usage in HTTP handler
async fn create_user(Json(dto): Json<UserDto>) -> Result<Json<User>, Error> {
    let user = User::try_from(dto)
        .map_err(|e| e.into_envelope_error())?;
    // user is GUARANTEED valid here!
    Ok(Json(user))
}
```

### Why TryFrom?

**Benefits:**
- Standard library trait - idiomatic Rust
- Type signature enforces validation
- Works with `?` operator
- Self-documenting conversion

**[x] Alternatives to avoid:**
```rust
// BAD: Direct construction bypasses validation
let user = User {
    name: dto.name,
    age: dto.age,
    email: Email(dto.email),  // No validation!
};
```

## Smart Constructors

Functions that return `Result<T, ValidationError>` and enforce invariants.

### Newtype Pattern

```rust
pub struct Email(String);

impl Email {
    pub fn new(raw: String) -> Result<Self, ValidationError> {
        let rule = rules::email().and(rules::max_len(255));
        validate("email", raw.as_str(), &rule)?;
        Ok(Self(raw))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}
```

### Multi-Field Constructor

```rust
pub struct BookingRequest {
    check_in: NaiveDate,
    check_out: NaiveDate,
    rooms: Vec<Room>,
}

impl BookingRequest {
    pub fn new(
        check_in: NaiveDate,
        check_out: NaiveDate,
        rooms: Vec<Room>,
    ) -> Result<Self, ValidationError> {
        let booking = Self { check_in, check_out, rooms };
        booking.validate()?;  // Validates all fields + cross-field rules
        Ok(booking)
    }

    // Getters
    pub fn check_in(&self) -> NaiveDate { self.check_in }
    pub fn check_out(&self) -> NaiveDate { self.check_out }
    pub fn rooms(&self) -> &[Room] { &self.rooms }
}
```

### With Derive Macro

Use `#[derive(Validate)]` to eliminate boilerplate:

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

    #[validate(min_items = 1, max_items = 10)]
    #[validate(each_nested)]
    rooms: Vec<Room>,
}

impl BookingRequest {
    pub fn new(
        check_in: NaiveDate,
        check_out: NaiveDate,
        rooms: Vec<Room>,
    ) -> Result<Self, ValidationError> {
        let booking = Self { check_in, check_out, rooms };
        booking.validate()?;  // Validates fields + cross-field check!
        Ok(booking)
    }
}
```

## Private Fields Pattern

### Why Private Fields Matter

```rust
// [x] BAD: Public fields allow invalid states
pub struct User {
    pub email: String,  // Anyone can set this!
    pub age: u8,
}

// Nothing prevents this:
let mut user = User { email: "valid@example.com".to_string(), age: 25 };
user.email = "not-an-email".to_string();  // Now invalid!
user.age = 200;  // Also invalid!
```

```rust
// GOOD: Private fields enforce invariants
#[derive(Validate)]
pub struct User {
    #[validate(email, max_len = 255)]
    email: String,  // Private!

    #[validate(range(min = 18, max = 120))]
    age: u8,
}

impl User {
    pub fn new(email: String, age: u8) -> Result<Self, ValidationError> {
        let user = Self { email, age };
        user.validate()?;
        Ok(user)
    }

    // Read-only access
    pub fn email(&self) -> &str { &self.email }
    pub fn age(&self) -> u8 { self.age }

    // Validated mutation (if needed)
    pub fn update_email(&mut self, new_email: String) -> Result<(), ValidationError> {
        validate("email", new_email.as_str(), &rules::email())?;
        self.email = new_email;
        Ok(())
    }
}
```

### Benefits

- **Encapsulation** - Hide implementation details
- **Invariant preservation** - Can't be modified without validation
- **API evolution** - Change internal representation without breaking consumers
- **Forced validation** - Only way to create instance is through constructor

## Manual vs Derive

### When to Use Derive

Use `#[derive(Validate)]` for:
- Straightforward field validation
- Cross-field validation with `check` attribute
- Standard error messages
- Less boilerplate

```rust
#[derive(Validate)]
pub struct User {
    #[validate(length(min = 2, max = 50))]
    name: String,

    #[validate(range(min = 18, max = 120))]
    age: u8,

    #[validate(nested)]
    email: Email,
}

impl User {
    pub fn new(name: String, age: u8, email: Email) -> Result<Self, ValidationError> {
        let user = Self { name, age, email };
        user.validate()?;  // ← 1 line validates everything!
        Ok(user)
    }
}
```

### When to Use Manual

Use manual `impl Validate` for:
- Custom error codes and messages
- Conditional validation logic
- Complex business rules
- Early returns for performance

```rust
impl User {
    pub fn new(name: String, age: u8, email: String) -> Result<Self, ValidationError> {
        let mut err = ValidationError::new();

        // Custom error code and message
        let name_rule = rules::min_len(2)
            .and(rules::max_len(50))
            .code("invalid_name")
            .message("Name must be between 2 and 50 characters");
        if let Err(e) = validate("name", name.as_str(), &name_rule) {
            err.extend(e);
        }

        // Conditional validation
        let age_rule = if self.is_enterprise {
            rules::range(21, 120)  // Enterprise: 21+
        } else {
            rules::range(18, 120)  // Standard: 18+
        };
        if let Err(e) = validate("age", &age, &age_rule) {
            err.extend(e);
        }

        // Nested validation with custom prefix
        let email = Email::new(email).map_err(|e| {
            e.prefixed("email")
                .map_messages(|msg| format!("Email error: {}", msg))
        })?;

        if !err.is_empty() {
            return Err(err);
        }

        Ok(Self { name, age, email })
    }
}
```

### Comparison

| Aspect | Derive | Manual |
|--------|--------|--------|
| **Boilerplate** | Low | High |
| **Readability** | High | Medium |
| **Custom errors** | Limited | Full control |
| **Conditional logic** | Limited | Full control |
| **Performance** | Good | Optimizable (early returns) |
| **Type safety** | Compile-time | Runtime |

## HTTP Integration

### With Framework Adapters

Use framework-specific adapters for automatic validation:

```rust
use domainstack_axum::{DomainJson, ErrorResponse};

// Automatic DTO → Domain conversion with validation
async fn create_user(
    DomainJson(dto, user): DomainJson<UserDto, User>
) -> Result<Json<User>, ErrorResponse> {
    // `user` is already validated via TryFrom!
    Ok(Json(save_user(user).await?))
}
```

### With error-envelope

Convert `ValidationError` to structured HTTP responses:

```rust
use domainstack_envelope::IntoEnvelopeError;

async fn create_user(Json(dto): Json<UserDto>) -> Result<Json<User>, Error> {
    let user = User::try_from(dto)
        .map_err(|e| e.into_envelope_error())?;
    Ok(Json(user))
}
```

**Produces structured error responses:**

```json
{
  "code": "VALIDATION",
  "status": 400,
  "message": "Validation failed with 2 errors",
  "details": {
    "fields": {
      "email": [{"code": "invalid_email", "message": "Invalid email format"}],
      "age": [{"code": "out_of_range", "message": "Must be between 18 and 120"}]
    }
  }
}
```

### Manual Error Handling

For custom error responses:

```rust
async fn create_user(Json(dto): Json<UserDto>) -> Result<Json<User>, (StatusCode, Json<ErrorResponse>)> {
    let user = User::try_from(dto).map_err(|e| {
        let field_errors: HashMap<String, Vec<String>> = e
            .field_violations_map()
            .into_iter()
            .map(|(path, violations)| {
                let messages = violations.iter()
                    .map(|v| v.message.clone())
                    .collect();
                (path, messages)
            })
            .collect();

        let response = ErrorResponse {
            error: "Validation failed".to_string(),
            fields: field_errors,
        };

        (StatusCode::BAD_REQUEST, Json(response))
    })?;

    Ok(Json(user))
}
```

## See Also

- **[Core Concepts]CORE_CONCEPTS.md** - Foundation principles of domainstack
- **[Manual Validation]MANUAL_VALIDATION.md** - Implementing Validate trait manually
- **[Derive Macro]DERIVE_MACRO.md** - Using `#[derive(Validate)]`
- **[HTTP Integration]HTTP_INTEGRATION.md** - Framework adapters and error responses
- **[Error Handling]ERROR_HANDLING.md** - Working with ValidationError