domainstack-actix 1.0.0

Actix-web extractors for domainstack: DomainJson<T> with automatic validation and structured error responses
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
# domainstack-actix

[![Blackwell Systems™](https://raw.githubusercontent.com/blackwell-systems/blackwell-docs-theme/main/badge-trademark.svg)](https://github.com/blackwell-systems)
[![Crates.io](https://img.shields.io/crates/v/domainstack-actix.svg)](https://crates.io/crates/domainstack-actix)
[![Documentation](https://docs.rs/domainstack-actix/badge.svg)](https://docs.rs/domainstack-actix)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-blue.svg)](https://github.com/blackwell-systems/domainstack/blob/main/LICENSE-MIT)

**Actix-web extractors for the [domainstack](https://crates.io/crates/domainstack) full-stack validation ecosystem**

One-line DTO→Domain extraction with automatic structured error responses. Define validation once, get type-safe handlers and UI-friendly errors.

## Hero Example

```rust
use actix_web::{post, web, App, HttpServer};
use domainstack::prelude::*;
use domainstack_actix::{DomainJson, ErrorResponse};
use domainstack_derive::Validate;
use serde::{Deserialize, Serialize};

// DTO: What the client sends
#[derive(Deserialize)]
struct CreateBookingDto {
    guest_email: String,
    rooms: u8,
    nights: u8,
    promo_code: Option<String>,
}

// Domain: Valid-by-construction with derive macro
#[derive(Debug, Serialize, Validate)]
#[validate(check = "self.rooms > 0 || self.nights > 0", message = "Booking must have rooms or nights")]
struct Booking {
    #[validate(email)]
    #[validate(max_len = 255)]
    guest_email: String,

    #[validate(range(min = 1, max = 10))]
    rooms: u8,

    #[validate(range(min = 1, max = 30))]
    nights: u8,

    #[validate(alphanumeric)]
    #[validate(length(min = 4, max = 20))]
    promo_code: Option<String>,
}

impl TryFrom<CreateBookingDto> for Booking {
    type Error = ValidationError;
    fn try_from(dto: CreateBookingDto) -> Result<Self, Self::Error> {
        let booking = Self {
            guest_email: dto.guest_email,
            rooms: dto.rooms,
            nights: dto.nights,
            promo_code: dto.promo_code,
        };
        booking.validate()?;
        Ok(booking)
    }
}

// Handler: ONE LINE - extraction, validation, conversion all handled
type BookingJson = DomainJson<Booking, CreateBookingDto>;

#[post("/bookings")]
async fn create_booking(
    BookingJson { domain: booking, .. }: BookingJson,
) -> Result<web::Json<Booking>, ErrorResponse> {
    // booking is GUARANTEED valid - use with confidence!
    Ok(web::Json(booking))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(create_booking))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
```

**Send invalid data:**
```bash
curl -X POST http://localhost:8080/bookings \
  -H "Content-Type: application/json" \
  -d '{"guest_email": "bad", "rooms": 0, "nights": 50}'
```

**Get structured, UI-friendly errors:**
```json
{
  "code": "VALIDATION",
  "status": 400,
  "message": "Validation failed with 3 errors",
  "details": {
    "fields": {
      "guest_email": [{"code": "invalid_email", "message": "Invalid email format"}],
      "rooms": [{"code": "out_of_range", "message": "Must be between 1 and 10"}],
      "nights": [{"code": "out_of_range", "message": "Must be between 1 and 30"}]
    }
  }
}
```

**Your frontend can map these directly to form fields. No parsing. No guessing.**

## Installation

```toml
[dependencies]
domainstack-actix = "1.0"
domainstack = { version = "1.0", features = ["derive", "regex"] }
domainstack-derive = "1.0"
serde = { version = "1", features = ["derive"] }
actix-web = "4"
```

## What You Get

### `DomainJson<T, Dto>` Extractor

Deserializes JSON → validates DTO → converts to domain type → returns 400 on failure.

**Before (manual):**
```rust
#[post("/users")]
async fn create_user(dto: web::Json<UserDto>) -> Result<web::Json<User>, actix_web::Error> {
    // 20+ lines of validation boilerplate
    let user = User::try_from(dto.into_inner())
        .map_err(|_| actix_web::error::ErrorBadRequest("Validation failed"))?;
    Ok(web::Json(user))
}
```

**After (domainstack-actix):**
```rust
type UserJson = DomainJson<User, UserDto>;

#[post("/users")]
async fn create_user(
    UserJson { domain: user, .. }: UserJson
) -> Result<web::Json<User>, ErrorResponse> {
    Ok(web::Json(user))  // user is valid!
}
```

### `ErrorResponse` Wrapper

Implements `ResponseError` for `error_envelope::Error` with structured field-level errors.

**Error Response Format:**
```json
{
  "code": "VALIDATION",
  "message": "Validation failed with 2 errors",
  "status": 400,
  "retryable": false,
  "details": {
    "fields": {
      "name": [
        {
          "code": "min_length",
          "message": "Must be at least 2 characters",
          "meta": {"min": "2"}
        }
      ],
      "age": [
        {
          "code": "out_of_range",
          "message": "Must be between 18 and 120",
          "meta": {"min": "18", "max": "120"}
        }
      ]
    }
  }
}
```

## When to Use Which Extractor

### `DomainJson<T, Dto>` - Domain-First (Recommended)

**Validation happens during DTO→Domain conversion** (`TryFrom`).

```rust
type UserJson = DomainJson<User, UserDto>;

#[post("/users")]
async fn create_user(
    UserJson { domain: user, .. }: UserJson
) -> Result<web::Json<User>, ErrorResponse> {
    // user is valid domain object
    Ok(web::Json(save_user(user).await?))
}
```

**Use when:**
- You have domain models with business invariants
- You want valid-by-construction types  
- You need domain logic separate from HTTP concerns
- You'll add async validation later (database checks, uniqueness, etc.)

**DTOs don't need `#[derive(Validate)]`** - validation lives in `TryFrom`.

### `ValidatedJson<Dto>` - DTO-First

**Validation happens on the DTO immediately after deserialization**.

```rust
use domainstack::Validate;

#[derive(Deserialize, Validate)]
struct QuickDto {
    #[validate(length(min = 2, max = 50))]
    name: String,
}

#[post("/quick")]
async fn quick_endpoint(
    ValidatedJson(dto): ValidatedJson<QuickDto>
) -> web::Json<QuickDto> {
    // dto has been validated, but not converted to domain
    web::Json(dto)
}
```

**Use when:**
- You're building simple CRUD endpoints
- You don't need domain conversion
- You want request-shape validation only
- DTOs are your domain (for simple services)

**DTOs must `#[derive(Validate)]`** - validation attributes drive behavior.

## Usage Patterns

### 1. Type Alias Pattern (Recommended)

Define one alias per endpoint/command:

```rust
use domainstack_actix::DomainJson;

type CreateUserJson = DomainJson<CreateUserCommand, CreateUserDto>;
type UpdateUserJson = DomainJson<UpdateUserCommand, UpdateUserDto>;

#[post("/users")]
async fn create_user(
    CreateUserJson { domain: cmd, .. }: CreateUserJson
) -> Result<web::Json<User>, ErrorResponse> {
    Ok(web::Json(user_service.create(cmd).await?))
}
```

### 2. Inline Pattern

For one-off handlers:

```rust
#[post("/users")]
async fn create_user(
    DomainJson { domain: user, .. }: DomainJson<User, UserDto>
) -> Result<web::Json<User>, ErrorResponse> {
    Ok(web::Json(user))
}
```

### 3. ErrorResponse Conversion

`ErrorResponse` implements `From<ValidationError>` and `From<error_envelope::Error>`:

```rust
#[post("/data")]
async fn handler() -> Result<web::Json<Data>, ErrorResponse> {
    let validated = validate_something()?;  // ValidationError
    let data = fetch_data().await?;         // error_envelope::Error
    Ok(web::Json(data))
}
```

## Domain Modeling

### DTO → Domain Conversion

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

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

// Domain: Private fields, valid-by-construction
pub struct CreateUserCommand {
    name: String,      // Private!
    email: Email,      // Validated newtype
    age: u8,
}

impl TryFrom<CreateUserDto> for CreateUserCommand {
    type Error = ValidationError;

    fn try_from(dto: CreateUserDto) -> Result<Self, Self::Error> {
        let mut err = ValidationError::new();

        // Validate name
        if let Err(e) = validate("name", dto.name.as_str(), 
                                 &rules::min_len(1).and(rules::max_len(50))) {
            err.extend(e);
        }

        // Convert email (newtype with validation)
        let email = Email::new(dto.email)
            .map_err(|e| e.prefixed("email"))?;

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

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

        Ok(Self {
            name: dto.name,
            email,
            age: dto.age,
        })
    }
}
```

### Handler

```rust
type CreateUserJson = DomainJson<CreateUserCommand, CreateUserDto>;

#[post("/users")]
async fn create_user(
    CreateUserJson { domain: cmd, .. }: CreateUserJson
) -> Result<web::Json<UserId>, ErrorResponse> {
    let user_id = user_service.create(cmd).await?;
    Ok(web::Json(user_id))
}
```

## Design Notes

### Why `DomainJson<T, Dto>` instead of `DomainJson<T>`?

**Problem:** Rust cannot reliably infer the DTO type when multiple `TryFrom` implementations exist:

```rust
impl TryFrom<UserDto> for User { ... }
impl<T> TryFrom<T> for User where T: Into<User> { ... }  // Ambiguous!
```

**Solution:** Explicit DTO type parameter keeps compilation deterministic:

```rust
DomainJson<User, UserDto>  // Unambiguous
```

**Ergonomics:** Use type aliases to keep handlers clean:

```rust
type UserJson = DomainJson<User, UserDto>;
```

This is a deliberate design choice, not a compromise. It avoids:
- Marker trait boilerplate
- Coherence/inference games
- Unpredictable compilation errors

### Why `ErrorResponse` wrapper?

**Problem:** Rust's orphan rules prevent implementing foreign traits on foreign types:

```rust
impl ResponseError for error_envelope::Error { ... }  // [x] Not allowed
//   ^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^
//   foreign trait     foreign type
```

**Solution:** Newtype wrapper implements `ResponseError`:

```rust
pub struct ErrorResponse(pub error_envelope::Error);

impl ResponseError for ErrorResponse { ... }  // Allowed
impl From<error_envelope::Error> for ErrorResponse { ... }
impl From<ValidationError> for ErrorResponse { ... }
```

This is the canonical Rust pattern for working around orphan rules.

### Why no DTO field in `DomainJson`?

The DTO is consumed during validation via `TryFrom`. Keeping it would require:

1. **Cloning** - Extra allocations for every request
2. **Security risk** - Encourages reading unvalidated fields
3. **Conceptual confusion** - Breaks the DTO→Domain boundary

If you need the original request data for logging/debugging, capture it before the extractor:

```rust
#[post("/users")]
async fn create_user(
    req: HttpRequest,  // Capture first if needed
    UserJson { domain: user, .. }: UserJson,
) -> Result<web::Json<User>, ErrorResponse> {
    tracing::debug!("Request: {:?}", req);
    Ok(web::Json(user))
}
```

## Error Handling

### Automatic 400 Responses

Validation errors automatically return HTTP 400 with structured field details:

```rust
POST /users {"name": "", "age": 200}

// Response: 400 Bad Request
{
  "code": "VALIDATION",
  "message": "Validation failed with 2 errors",
  "details": {
    "fields": {
      "name": [{"code": "min_length", "message": "Must be at least 1 characters"}],
      "age": [{"code": "out_of_range", "message": "Must be between 18 and 120"}]
    }
  }
}
```

### Malformed JSON

```rust
POST /users {invalid json

// Response: 400 Bad Request
{
  "code": "BAD_REQUEST",
  "message": "Invalid JSON: ..."
}
```

## Testing

```rust
use actix_web::{test, web, App};

#[actix_rt::test]
async fn test_validation() {
    let app = test::init_service(
        App::new().route("/", web::post().to(create_user))
    ).await;

    let req = test::TestRequest::post()
        .uri("/")
        .set_json(&serde_json::json!({"name": "", "age": 200}))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), 400);

    let body: serde_json::Value = test::read_body_json(resp).await;
    assert_eq!(body["message"], "Validation failed with 2 errors");
}
```

## Complete Example

```rust
use actix_web::{get, post, web, App, HttpServer};
use domainstack::prelude::*;
use domainstack_actix::{DomainJson, ErrorResponse};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
pub struct CreateBookingDto {
    pub guest_name: String,
    pub email: String,
    pub nights: u8,
}

#[derive(Debug, Clone)]
pub struct Email(String);

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

pub struct CreateBookingCommand {
    guest_name: String,
    email: Email,
    nights: u8,
}

impl TryFrom<CreateBookingDto> for CreateBookingCommand {
    type Error = ValidationError;

    fn try_from(dto: CreateBookingDto) -> Result<Self, Self::Error> {
        let mut err = ValidationError::new();

        if let Err(e) = validate("guest_name", dto.guest_name.as_str(),
                                 &rules::min_len(1)) {
            err.extend(e);
        }

        let email = match Email::new(dto.email) {
            Ok(email) => Some(email),
            Err(e) => {
                err.extend(e.prefixed("email"));
                None
            }
        };

        if let Err(e) = validate("nights", &dto.nights, &rules::range(1, 30)) {
            err.extend(e);
        }

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

        Ok(Self {
            guest_name: dto.guest_name,
            email: email.unwrap(),
            nights: dto.nights,
        })
    }
}

#[derive(Serialize)]
pub struct Booking {
    pub id: String,
    pub guest_name: String,
}

type CreateBookingJson = DomainJson<CreateBookingCommand, CreateBookingDto>;

#[post("/bookings")]
async fn create_booking(
    CreateBookingJson { domain: cmd, .. }: CreateBookingJson,
) -> Result<web::Json<Booking>, ErrorResponse> {
    let booking = Booking {
        id: "123".to_string(),
        guest_name: cmd.guest_name,
    };
    Ok(web::Json(booking))
}

#[get("/health")]
async fn health() -> &'static str {
    "OK"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(health)
            .service(create_booking)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
```

## License

Apache 2.0

## Author

Dayna Blackwell - [blackwellsystems@protonmail.com](mailto:blackwellsystems@protonmail.com)