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
//! # domainstack-actix
//!
//! Actix-web integration for domainstack validation with automatic DTO→Domain conversion.
//!
//! This crate provides Actix-web extractors that automatically deserialize, validate, and convert
//! DTOs to domain types—returning structured error responses on validation failure.
//!
//! ## What it provides
//!
//! - **`DomainJson<T, Dto>`** - Extract JSON, validate, and convert DTO to domain type in one step
//! - **`ValidatedJson<Dto>`** - Extract and validate a DTO without domain conversion
//! - **`ErrorResponse`** - Automatic structured error responses with field-level details
//!
//! ## Example - DomainJson
//!
//! ```rust,no_run
//! use actix_web::{post, web, App, HttpServer};
//! use domainstack::prelude::*;
//! use domainstack_actix::{DomainJson, ErrorResponse};
//! use serde::Deserialize;
//!
//! #[derive(Deserialize)]
//! struct CreateUserDto {
//!     name: String,
//!     age: u8,
//! }
//!
//! struct User {
//!     name: String,
//!     age: u8,
//! }
//!
//! impl TryFrom<CreateUserDto> for User {
//!     type Error = domainstack::ValidationError;
//!
//!     fn try_from(dto: CreateUserDto) -> Result<Self, Self::Error> {
//!         validate("name", dto.name.as_str(), &rules::min_len(2).and(rules::max_len(50)))?;
//!         validate("age", &dto.age, &rules::range(18, 120))?;
//!         Ok(Self { name: dto.name, age: dto.age })
//!     }
//! }
//!
//! // Type alias for cleaner handler signatures
//! type UserJson = DomainJson<User, CreateUserDto>;
//!
//! #[post("/users")]
//! async fn create_user(
//!     UserJson { domain: user, .. }: UserJson
//! ) -> Result<web::Json<String>, ErrorResponse> {
//!     // user is guaranteed valid here!
//!     Ok(web::Json(format!("Created: {}", user.name)))
//! }
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//!     HttpServer::new(|| App::new().service(create_user))
//!         .bind(("127.0.0.1", 8080))?
//!         .run()
//!         .await
//! }
//! ```
//!
//! ## Example - ValidatedJson
//!
//! ```rust,ignore
//! use actix_web::{post, web};
//! use domainstack::Validate;
//! use domainstack_actix::{ValidatedJson, ErrorResponse};
//!
//! #[derive(Debug, Validate, serde::Deserialize)]
//! struct UserDto {
//!     #[validate(length(min = 2, max = 50))]
//!     name: String,
//!
//!     #[validate(range(min = 18, max = 120))]
//!     age: u8,
//! }
//!
//! #[post("/users")]
//! async fn create_user(
//!     ValidatedJson(dto): ValidatedJson<UserDto>
//! ) -> Result<web::Json<UserDto>, ErrorResponse> {
//!     // dto is guaranteed valid here!
//!     Ok(web::Json(dto))
//! }
//! ```
//!
//! ## Error Response Format
//!
//! On validation failure, returns a 400 Bad Request with structured errors:
//!
//! ```json
//! {
//!   "code": "VALIDATION",
//!   "status": 400,
//!   "message": "Validation failed with 2 errors",
//!   "details": {
//!     "fields": {
//!       "name": [{"code": "min_length", "message": "Must be at least 2 characters"}],
//!       "age": [{"code": "out_of_range", "message": "Must be between 18 and 120"}]
//!     }
//!   }
//! }
//! ```

use actix_web::{error::ResponseError, web, FromRequest, HttpRequest, HttpResponse};
use domainstack::ValidationError;
use futures::future::{ready, Ready};
use std::marker::PhantomData;

pub struct DomainJson<T, Dto = ()> {
    pub domain: T,
    _dto: PhantomData<Dto>,
}

impl<T, Dto> DomainJson<T, Dto> {
    pub fn new(domain: T) -> Self {
        Self {
            domain,
            _dto: PhantomData,
        }
    }
}

pub struct ErrorResponse(pub error_envelope::Error);

impl From<error_envelope::Error> for ErrorResponse {
    fn from(err: error_envelope::Error) -> Self {
        ErrorResponse(err)
    }
}

impl From<ValidationError> for ErrorResponse {
    fn from(err: ValidationError) -> Self {
        use domainstack_envelope::IntoEnvelopeError;
        ErrorResponse(err.into_envelope_error())
    }
}

impl<T, Dto> FromRequest for DomainJson<T, Dto>
where
    Dto: serde::de::DeserializeOwned,
    T: TryFrom<Dto, Error = ValidationError>,
{
    type Error = ErrorResponse;
    type Future = Ready<Result<Self, Self::Error>>;

    fn from_request(req: &HttpRequest, payload: &mut actix_web::dev::Payload) -> Self::Future {
        let json_fut = web::Json::<Dto>::from_request(req, payload);

        // Note: Using block_on() here is required by Actix-web's synchronous extractor pattern.
        // FromRequest returns a Ready<T> future (not async), so we must synchronously extract
        // the JSON. This is the standard pattern for Actix-web 4.x extractors.
        // Performance note: This blocks the current task but does not block the async runtime.
        // For truly async extraction, consider using web::Json::from_request directly in your
        // handler and calling into_domain() on the DTO.
        ready(match futures::executor::block_on(json_fut) {
            Ok(web::Json(dto)) => domainstack_http::into_domain(dto)
                .map(DomainJson::new)
                .map_err(ErrorResponse),
            Err(e) => Err(ErrorResponse(error_envelope::Error::bad_request(format!(
                "Invalid JSON: {}",
                e
            )))),
        })
    }
}

impl ResponseError for ErrorResponse {
    fn status_code(&self) -> actix_web::http::StatusCode {
        actix_web::http::StatusCode::from_u16(self.0.status)
            .unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR)
    }

    fn error_response(&self) -> HttpResponse {
        HttpResponse::build(self.status_code()).json(&self.0)
    }
}

impl std::fmt::Debug for ErrorResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ErrorResponse({:?})", self.0)
    }
}

impl std::fmt::Display for ErrorResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.message)
    }
}

pub struct ValidatedJson<Dto>(pub Dto);

impl<Dto> FromRequest for ValidatedJson<Dto>
where
    Dto: serde::de::DeserializeOwned + domainstack::Validate,
{
    type Error = ErrorResponse;
    type Future = Ready<Result<Self, Self::Error>>;

    fn from_request(req: &HttpRequest, payload: &mut actix_web::dev::Payload) -> Self::Future {
        let json_fut = web::Json::<Dto>::from_request(req, payload);

        // Note: See DomainJson implementation for explanation of block_on() usage.
        // This is the standard Actix-web 4.x extractor pattern.
        ready(match futures::executor::block_on(json_fut) {
            Ok(web::Json(dto)) => dto.validate().map(|_| ValidatedJson(dto)).map_err(|e| {
                use domainstack_envelope::IntoEnvelopeError;
                ErrorResponse(e.into_envelope_error())
            }),
            Err(e) => Err(ErrorResponse(error_envelope::Error::bad_request(format!(
                "Invalid JSON: {}",
                e
            )))),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{test, web, App};
    use domainstack::{prelude::*, Validate};

    // DTOs used with DomainJson are just serde shapes
    // Validation happens during TryFrom conversion to domain
    #[derive(Debug, Clone, serde::Deserialize)]
    struct UserDto {
        name: String,
        age: u8,
    }

    #[derive(Debug, serde::Serialize)]
    struct User {
        name: String,
        age: u8,
    }

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

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

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

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

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

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

    async fn create_user(
        DomainJson { domain: user, .. }: DomainJson<User, UserDto>,
    ) -> web::Json<User> {
        web::Json(user)
    }

    type UserJson = DomainJson<User, UserDto>;

    async fn create_user_with_alias(UserJson { domain: user, .. }: UserJson) -> web::Json<User> {
        web::Json(user)
    }

    async fn create_user_result_style(
        UserJson { domain: user, .. }: UserJson,
    ) -> Result<web::Json<User>, ErrorResponse> {
        Ok(web::Json(user))
    }

    #[actix_rt::test]
    async fn test_domain_json_valid() {
        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": "Alice", "age": 30}))
            .to_request();

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

    #[actix_rt::test]
    async fn test_domain_json_invalid() {
        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": "A", "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!(body["details"].is_object());
        assert_eq!(
            body["message"].as_str().unwrap(),
            "Validation failed with 2 errors"
        );

        let details = body["details"].as_object().unwrap();
        let fields = details["fields"].as_object().unwrap();

        assert!(fields.contains_key("name"));
        assert!(fields.contains_key("age"));
    }

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

        let req = test::TestRequest::post()
            .uri("/")
            .set_payload("{invalid json")
            .insert_header(("content-type", "application/json"))
            .to_request();

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

    #[actix_rt::test]
    async fn test_domain_json_missing_fields() {
        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": "Alice"}))
            .to_request();

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

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

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

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

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

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

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

    // ValidatedJson tests - DTOs that derive Validate
    #[derive(Debug, Clone, Validate, serde::Deserialize, serde::Serialize)]
    struct ValidatedUserDto {
        #[validate(length(min = 2, max = 50))]
        name: String,

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

    async fn accept_validated_dto(
        ValidatedJson(dto): ValidatedJson<ValidatedUserDto>,
    ) -> web::Json<ValidatedUserDto> {
        web::Json(dto)
    }

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

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

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

        let body: ValidatedUserDto = test::read_body_json(resp).await;
        assert_eq!(body.name, "Alice");
        assert_eq!(body.age, 30);
    }

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

        let req = test::TestRequest::post()
            .uri("/")
            .set_json(serde_json::json!({"name": "A", "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"].as_str().unwrap(),
            "Validation failed with 2 errors"
        );

        let details = body["details"].as_object().unwrap();
        let fields = details["fields"].as_object().unwrap();

        assert!(fields.contains_key("name"));
        assert!(fields.contains_key("age"));
    }

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

        let req = test::TestRequest::post()
            .uri("/")
            .set_payload("{invalid json")
            .insert_header(("content-type", "application/json"))
            .to_request();

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

    #[actix_rt::test]
    async fn test_error_response_debug() {
        let err = ErrorResponse(error_envelope::Error::bad_request("Test error"));
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("ErrorResponse"));
    }

    #[actix_rt::test]
    async fn test_error_response_display() {
        let err = ErrorResponse(error_envelope::Error::bad_request("Custom message"));
        let display_str = format!("{}", err);
        assert_eq!(display_str, "Custom message");
    }

    #[actix_rt::test]
    async fn test_error_response_status_code() {
        use actix_web::ResponseError;

        let err = ErrorResponse(error_envelope::Error::bad_request("Bad request"));
        assert_eq!(err.status_code().as_u16(), 400);

        let mut custom_err = error_envelope::Error::bad_request("Custom");
        custom_err.status = 422;
        let err = ErrorResponse(custom_err);
        assert_eq!(err.status_code().as_u16(), 422);
    }
}