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
//! Type-state validation markers for compile-time validation guarantees.
//!
//! This module provides marker types that enable tracking validation state at the type level,
//! ensuring validated data cannot be confused with unvalidated data at compile time.
//!
//! # Overview
//!
//! Phantom types allow you to encode validation state in the type system with **zero runtime cost**.
//! The compiler enforces that only validated data can be used where validated data is expected.
//!
//! # Basic Usage
//!
//! ```rust
//! use domainstack::{ValidationError, validate, rules, typestate::{Validated, Unvalidated}};
//! use std::marker::PhantomData;
//!
//! // Domain type with validation state
//! pub struct Email<State = Unvalidated> {
//!     value: String,
//!     _state: PhantomData<State>,
//! }
//!
//! // Only unvalidated emails can be created
//! impl Email<Unvalidated> {
//!     pub fn new(value: String) -> Self {
//!         Self {
//!             value,
//!             _state: PhantomData,
//!         }
//!     }
//!
//!     // Transform to validated state
//!     pub fn validate(self) -> Result<Email<Validated>, ValidationError> {
//!         validate("email", self.value.as_str(), &rules::contains("@"))?;
//!         Ok(Email {
//!             value: self.value,
//!             _state: PhantomData,
//!         })
//!     }
//! }
//!
//! // Methods available for any state
//! impl<State> Email<State> {
//!     pub fn as_str(&self) -> &str {
//!         &self.value
//!     }
//! }
//!
//! // Only validated emails can be used in certain contexts
//! fn send_email(email: Email<Validated>) {
//!     println!("Sending to: {}", email.as_str());
//! }
//!
//! // Example usage
//! let email = Email::new("user@example.com".to_string());
//! // send_email(email); // [x] Compile error: expected Email<Validated>, found Email<Unvalidated>
//!
//! let validated = email.validate().unwrap();
//! send_email(validated); // Compiles!
//! ```
//!
//! # Benefits
//!
//! 1. **Compile-time safety** - Cannot accidentally use unvalidated data
//! 2. **Zero runtime cost** - PhantomData has zero size, optimizes away completely
//! 3. **Self-documenting** - Function signatures make validation requirements explicit
//! 4. **Progressive adoption** - Can be added to existing types without breaking changes
//!
//! # Advanced Patterns
//!
//! ## Builder Pattern with Validation
//!
//! ```rust
//! use domainstack::{ValidationError, validate, rules, typestate::{Validated, Unvalidated}};
//! use std::marker::PhantomData;
//!
//! pub struct User<State = Unvalidated> {
//!     username: String,
//!     email: String,
//!     age: u8,
//!     _state: PhantomData<State>,
//! }
//!
//! impl User<Unvalidated> {
//!     pub fn builder() -> UserBuilder {
//!         UserBuilder::default()
//!     }
//!
//!     pub fn validate(self) -> Result<User<Validated>, ValidationError> {
//!         let mut errors = ValidationError::default();
//!
//!         if let Err(e) = validate("username", self.username.as_str(), &rules::min_len(3)) {
//!             errors.extend(e);
//!         }
//!         if let Err(e) = validate("email", self.email.as_str(), &rules::contains("@")) {
//!             errors.extend(e);
//!         }
//!         if let Err(e) = validate("age", &self.age, &rules::range(0, 120)) {
//!             errors.extend(e);
//!         }
//!
//!         if errors.is_empty() {
//!             Ok(User {
//!                 username: self.username,
//!                 email: self.email,
//!                 age: self.age,
//!                 _state: PhantomData,
//!             })
//!         } else {
//!             Err(errors)
//!         }
//!     }
//! }
//!
//! #[derive(Default)]
//! pub struct UserBuilder {
//!     username: Option<String>,
//!     email: Option<String>,
//!     age: Option<u8>,
//! }
//!
//! impl UserBuilder {
//!     pub fn username(mut self, username: String) -> Self {
//!         self.username = Some(username);
//!         self
//!     }
//!
//!     pub fn email(mut self, email: String) -> Self {
//!         self.email = Some(email);
//!         self
//!     }
//!
//!     pub fn age(mut self, age: u8) -> Self {
//!         self.age = Some(age);
//!         self
//!     }
//!
//!     pub fn build(self) -> User<Unvalidated> {
//!         User {
//!             username: self.username.unwrap_or_default(),
//!             email: self.email.unwrap_or_default(),
//!             age: self.age.unwrap_or(0),
//!             _state: PhantomData,
//!         }
//!     }
//! }
//! ```
//!
//! ## Database Operations
//!
//! ```rust,ignore
//! use domainstack::typestate::{Validated, Unvalidated};
//! use std::marker::PhantomData;
//!
//! pub struct User<State = Unvalidated> {
//!     username: String,
//!     email: String,
//!     _state: PhantomData<State>,
//! }
//!
//! // Only accept validated users for database operations
//! async fn save_to_database(user: User<Validated>) -> Result<i64, DatabaseError> {
//!     // Compiler guarantees user is validated!
//!     sqlx::query("INSERT INTO users (username, email) VALUES (?, ?)")
//!         .bind(&user.username)
//!         .bind(&user.email)
//!         .execute(&pool)
//!         .await
//! }
//!
//! // Only accept validated users for business logic
//! fn send_welcome_email(user: &User<Validated>) {
//!     // Compiler guarantees user is validated!
//!     email_service.send(&user.email, "Welcome!");
//! }
//! ```
//!
//! # Design Principles
//!
//! 1. **Opt-in** - Add to types that benefit from compile-time validation tracking
//! 2. **Zero-cost** - PhantomData compiles to zero bytes
//! 3. **Ergonomic** - Default to `Unvalidated` state for ease of construction
//! 4. **Explicit** - Validation boundaries are clear in function signatures
//!
//! # When to Use
//!
//! Use phantom types when:
//! - Data flows through multiple functions/layers
//! - Validation is expensive or has side effects (DB queries, API calls)
//! - You want compile-time guarantees that validation occurred
//! - Multiple code paths could accidentally bypass validation
//!
//! Don't use phantom types when:
//! - Type is only used in one place immediately after validation
//! - Validation is trivial and cheap to repeat
//! - The type already uses "valid-by-construction" pattern (validation in constructor)
//!
//! # Performance
//!
//! Phantom types have **zero runtime cost**:
//! - `PhantomData<T>` has size 0 bytes
//! - No memory overhead
//! - No CPU overhead
//! - Optimized away completely by the compiler
//!
//! ```rust
//! use std::marker::PhantomData;
//! use domainstack::typestate::{Validated, Unvalidated};
//!
//! struct Data<State> {
//!     value: String,
//!     _state: PhantomData<State>,
//! }
//!
//! // Both are the same size in memory!
//! assert_eq!(
//!     std::mem::size_of::<Data<Unvalidated>>(),
//!     std::mem::size_of::<Data<Validated>>()
//! );
//! assert_eq!(
//!     std::mem::size_of::<Data<Unvalidated>>(),
//!     std::mem::size_of::<String>()  // Just the string, no overhead!
//! );
//! ```

/// Marker type indicating that data has **not** been validated.
///
/// This is typically the default state for types with validation state tracking.
///
/// # Example
///
/// ```rust
/// use domainstack::typestate::Unvalidated;
/// use std::marker::PhantomData;
///
/// pub struct Email<State = Unvalidated> {
///     value: String,
///     _state: PhantomData<State>,
/// }
///
/// impl Email<Unvalidated> {
///     pub fn new(value: String) -> Self {
///         Self {
///             value,
///             _state: PhantomData,
///         }
///     }
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Unvalidated;

/// Marker type indicating that data **has been** validated.
///
/// Types in this state have passed all validation rules and are guaranteed
/// to be valid at the point of state transition.
///
/// # Example
///
/// ```rust
/// use domainstack::{ValidationError, validate, rules, typestate::{Validated, Unvalidated}};
/// use std::marker::PhantomData;
///
/// pub struct Email<State = Unvalidated> {
///     value: String,
///     _state: PhantomData<State>,
/// }
///
/// impl Email<Unvalidated> {
///     pub fn validate(self) -> Result<Email<Validated>, ValidationError> {
///         validate("email", self.value.as_str(), &rules::contains("@"))?;
///         Ok(Email {
///             value: self.value,
///             _state: PhantomData,
///         })
///     }
/// }
///
/// // Only accept validated emails
/// fn send_email(email: Email<Validated>) {
///     println!("Sending to: {}", email.value);
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Validated;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{rules, validate, ValidationError};
    use std::marker::PhantomData;

    // Example domain type using phantom types
    #[derive(Debug)]
    struct Email<State = Unvalidated> {
        value: String,
        _state: PhantomData<State>,
    }

    impl Email<Unvalidated> {
        fn new(value: String) -> Self {
            Self {
                value,
                _state: PhantomData,
            }
        }

        #[allow(clippy::result_large_err)]
        fn validate(self) -> Result<Email<Validated>, ValidationError> {
            // Use simple validation that doesn't require regex feature
            validate("email", self.value.as_str(), &rules::contains("@"))?;
            Ok(Email {
                value: self.value,
                _state: PhantomData,
            })
        }
    }

    impl<State> Email<State> {
        fn as_str(&self) -> &str {
            &self.value
        }
    }

    // Function that only accepts validated emails
    fn send_email(email: &Email<Validated>) -> String {
        format!("Sending to: {}", email.as_str())
    }

    #[test]
    fn test_unvalidated_marker_properties() {
        // Marker types are zero-sized
        assert_eq!(std::mem::size_of::<Unvalidated>(), 0);

        // They implement common traits
        let u1 = Unvalidated;
        let u2 = Unvalidated;
        assert_eq!(u1, u2);
        assert_eq!(format!("{:?}", u1), "Unvalidated");
    }

    #[test]
    fn test_validated_marker_properties() {
        // Marker types are zero-sized
        assert_eq!(std::mem::size_of::<Validated>(), 0);

        // They implement common traits
        let v1 = Validated;
        let v2 = Validated;
        assert_eq!(v1, v2);
        assert_eq!(format!("{:?}", v1), "Validated");
    }

    #[test]
    fn test_phantom_types_zero_cost() {
        // Both Email<Unvalidated> and Email<Validated> have the same size
        assert_eq!(
            std::mem::size_of::<Email<Unvalidated>>(),
            std::mem::size_of::<Email<Validated>>()
        );

        // They're the same size as just the String field
        assert_eq!(
            std::mem::size_of::<Email<Unvalidated>>(),
            std::mem::size_of::<String>()
        );
    }

    #[test]
    fn test_validation_state_transition() {
        // Create unvalidated email
        let email = Email::new("user@example.com".to_string());

        // Cannot send unvalidated email (would not compile):
        // send_email(&email);  // [x] Type error

        // Validate to transition state
        let validated = email.validate().expect("Should be valid");

        // Can send validated email
        let result = send_email(&validated);
        assert_eq!(result, "Sending to: user@example.com");
    }

    #[test]
    fn test_validation_failure_preserves_unvalidated_state() {
        // Create invalid email
        let email = Email::new("not-an-email".to_string());

        // Validation fails
        let result = email.validate();
        assert!(result.is_err());

        // Original email was moved, so we can't use it again
        // This is correct - failed validation consumes the value
    }

    #[test]
    fn test_methods_work_across_states() {
        let unvalidated = Email::new("user@example.com".to_string());
        assert_eq!(unvalidated.as_str(), "user@example.com");

        let validated = unvalidated.validate().unwrap();
        assert_eq!(validated.as_str(), "user@example.com");
    }

    // Example with struct having multiple fields
    #[derive(Debug)]
    struct User<State = Unvalidated> {
        username: String,
        email: String,
        age: u8,
        _state: PhantomData<State>,
    }

    impl User<Unvalidated> {
        fn new(username: String, email: String, age: u8) -> Self {
            Self {
                username,
                email,
                age,
                _state: PhantomData,
            }
        }

        #[allow(clippy::result_large_err)]
        fn validate(self) -> Result<User<Validated>, ValidationError> {
            let mut errors = ValidationError::default();

            if let Err(e) = validate("username", self.username.as_str(), &rules::min_len(3)) {
                errors.extend(e);
            }
            // Use simple validation that doesn't require regex feature
            if let Err(e) = validate("email", self.email.as_str(), &rules::contains("@")) {
                errors.extend(e);
            }
            if let Err(e) = validate("age", &self.age, &rules::range(0, 120)) {
                errors.extend(e);
            }

            if errors.is_empty() {
                Ok(User {
                    username: self.username,
                    email: self.email,
                    age: self.age,
                    _state: PhantomData,
                })
            } else {
                Err(errors)
            }
        }
    }

    impl<State> User<State> {
        fn username(&self) -> &str {
            &self.username
        }
    }

    fn register_user(user: User<Validated>) -> String {
        format!("Registered: {}", user.username())
    }

    #[test]
    fn test_multi_field_validation() {
        let user = User::new("alice".to_string(), "alice@example.com".to_string(), 25);
        let validated = user.validate().expect("Should be valid");
        let result = register_user(validated);
        assert_eq!(result, "Registered: alice");
    }

    #[test]
    fn test_multi_field_validation_failure() {
        // Invalid: username too short, invalid email, age out of range
        let user = User::new("ab".to_string(), "not-email".to_string(), 150);
        let result = user.validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert_eq!(errors.violations.len(), 3);
    }

    #[test]
    fn test_partial_validation_failure() {
        // Only username is invalid
        let user = User::new("ab".to_string(), "alice@example.com".to_string(), 25);
        let result = user.validate();

        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert_eq!(errors.violations.len(), 1);
        assert!(errors.violations[0].path.to_string().contains("username"));
    }
}