domainstack-schema 1.0.0

OpenAPI schema generation for domainstack validation types
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# OpenAPI 3.0 Capabilities Reference

**Version:** domainstack-schema v0.8.0
**OpenAPI Version:** 3.0.0
**Last Updated:** 2025-12-24

This document provides a comprehensive reference of OpenAPI 3.0 features supported by `domainstack-schema`.

---

## Table of Contents

1. [Feature Coverage Matrix]#feature-coverage-matrix
2. [Supported Features (Detailed)]#supported-features-detailed
3. [Unsupported Features]#unsupported-features
4. [Workarounds for Limitations]#workarounds-for-limitations
5. [Best Practices]#best-practices
6. [Complete Examples]#complete-examples

---

## Feature Coverage Matrix

### OpenAPI Specification Components

| Component | Support Level | Notes |
|-----------|--------------|-------|
| **openapi** | Full | Fixed at "3.0.0" |
| **info** | Partial | Title, version, description only |
| **servers** | None | Out of scope |
| **paths** | None | Out of scope - use framework adapters |
| **components/schemas** | Full | **Core focus of this crate** |
| **components/responses** | None | Out of scope |
| **components/parameters** | None | Out of scope |
| **components/examples** | None | Out of scope |
| **components/requestBodies** | None | Out of scope |
| **components/headers** | None | Out of scope |
| **components/securitySchemes** | None | Out of scope |
| **components/links** | None | Out of scope |
| **components/callbacks** | None | Out of scope |
| **security** | None | Out of scope |
| **tags** | None | Out of scope |
| **externalDocs** | None | Out of scope |

**Summary:** This crate focuses exclusively on **schema generation** (components/schemas). For full API documentation, integrate with framework-specific tools.

---

### Schema Object Features

#### Core Schema Types

| Feature | Support | API | Example |
|---------|---------|-----|---------|
| **type: string** | Full | `Schema::string()` | Basic string type |
| **type: number** | Full | `Schema::number()` | Floating point numbers |
| **type: integer** | Full | `Schema::integer()` | Integer numbers |
| **type: boolean** | Full | `Schema::boolean()` | Boolean values |
| **type: array** | Full | `Schema::array(items)` | Arrays with item schema |
| **type: object** | Full | `Schema::object()` | Objects with properties |
| **type: null** | Partial | Use `Option<T>` | Via nullable in JSON |

#### String Constraints

| Constraint | Support | API | OpenAPI Field |
|------------|---------|-----|---------------|
| **minLength** | Full | `.min_length(n)` | `minLength` |
| **maxLength** | Full | `.max_length(n)` | `maxLength` |
| **pattern** | Full | `.pattern(regex)` | `pattern` |
| **format** | Full | `.format(format)` | `format` |
| **enum** | Full | `.enum_values(&[...])` | `enum` |

**Supported formats:** `email`, `date`, `date-time`, `password`, `uuid`, `uri`, `hostname`, `ipv4`, `ipv6`, `byte`, `binary`, or custom.

#### Numeric Constraints

| Constraint | Support | API | OpenAPI Field |
|------------|---------|-----|---------------|
| **minimum** | Full | `.minimum(n)` | `minimum` |
| **maximum** | Full | `.maximum(n)` | `maximum` |
| **exclusiveMinimum** | None | - | Not supported |
| **exclusiveMaximum** | None | - | Not supported |
| **multipleOf** | Full | `.multiple_of(n)` | `multipleOf` |

**Workaround for exclusive:** Use `minimum(n + 1)` for integers, document in description.

#### Array Constraints

| Constraint | Support | API | OpenAPI Field |
|------------|---------|-----|---------------|
| **items** | Full | `Schema::array(schema)` | `items` |
| **minItems** | Full | `.min_items(n)` | `minItems` |
| **maxItems** | Full | `.max_items(n)` | `maxItems` |
| **uniqueItems** | Full | `.unique_items(true)` | `uniqueItems` |
| **contains** | None | - | Not supported |

#### Object Constraints

| Constraint | Support | API | OpenAPI Field |
|------------|---------|-----|---------------|
| **properties** | Full | `.property(name, schema)` | `properties` |
| **required** | Full | `.required(&[...])` | `required` |
| **additionalProperties** | Partial | Manual JSON | Limited support |
| **minProperties** | None | - | Not supported |
| **maxProperties** | None | - | Not supported |
| **propertyNames** | None | - | Not supported |
| **patternProperties** | None | - | Not supported |

#### Schema Composition (v0.8+)

| Feature | Support | API | OpenAPI Field |
|---------|---------|-----|---------------|
| **anyOf** | Full | `Schema::any_of(vec![...])` | `anyOf` |
| **allOf** | Full | `Schema::all_of(vec![...])` | `allOf` |
| **oneOf** | Full | `Schema::one_of(vec![...])` | `oneOf` |
| **not** | None | - | Not supported |

#### Schema Metadata (v0.8+)

| Feature | Support | API | OpenAPI Field |
|---------|---------|-----|---------------|
| **title** | Via name | Schema name becomes title | Implicit |
| **description** | Full | `.description(text)` | `description` |
| **default** | Full | `.default(value)` | `default` |
| **example** | Full | `.example(value)` | `example` |
| **examples** | Full | `.examples(vec![...])` | `examples` |
| **deprecated** | Full | `.deprecated(true)` | `deprecated` |
| **readOnly** | Full | `.read_only(true)` | `readOnly` |
| **writeOnly** | Full | `.write_only(true)` | `writeOnly` |
| **externalDocs** | None | - | Not supported |

#### Advanced Features

| Feature | Support | API | OpenAPI Field |
|---------|---------|-----|---------------|
| **$ref** | Full | `Schema::reference(name)` | `$ref` |
| **discriminator** | None | - | Not supported |
| **xml** | None | - | Not supported |
| **nullable** | Via Option | Use Rust `Option<T>` | Implicit |
| **Vendor Extensions (x-*)** | Full | `.extension(key, value)` | Custom `x-*` fields |

---

## Supported Features (Detailed)

### 1. Basic Schema Types

```rust
use domainstack_schema::Schema;

// String
let name = Schema::string()
    .min_length(1)
    .max_length(100)
    .description("User name");

// Integer with range
let age = Schema::integer()
    .minimum(0)
    .maximum(150);

// Number (float/double)
let price = Schema::number()
    .minimum(0.0)
    .maximum(1000000.0)
    .multiple_of(0.01);  // Penny precision

// Boolean
let active = Schema::boolean()
    .default(json!(true));

// Array of strings
let tags = Schema::array(Schema::string())
    .min_items(1)
    .max_items(10)
    .unique_items(true);

// Object with properties
let user = Schema::object()
    .property("name", Schema::string())
    .property("age", Schema::integer())
    .required(&["name"]);
```

### 2. String Formats

```rust
// Email validation
let email = Schema::string()
    .format("email")
    .description("Must be a valid email address");

// Date/DateTime
let birthdate = Schema::string()
    .format("date")
    .description("ISO 8601 date (YYYY-MM-DD)");

let created_at = Schema::string()
    .format("date-time")
    .description("ISO 8601 timestamp");

// UUID
let id = Schema::string()
    .format("uuid")
    .read_only(true);

// URI/URL
let website = Schema::string()
    .format("uri")
    .description("Full URL including protocol");

// Password (write-only)
let password = Schema::string()
    .format("password")
    .min_length(8)
    .write_only(true)
    .description("User password (never returned in responses)");

// IP addresses
let ipv4 = Schema::string().format("ipv4");
let ipv6 = Schema::string().format("ipv6");

// Hostname
let host = Schema::string().format("hostname");
```

### 3. Pattern Validation

```rust
// Regex patterns
let username = Schema::string()
    .pattern("^[a-zA-Z0-9_]{3,20}$")
    .description("Alphanumeric and underscores, 3-20 characters");

// Phone number
let phone = Schema::string()
    .pattern(r"^\+?[1-9]\d{1,14}$")
    .description("E.164 format phone number");

// Hex color
let color = Schema::string()
    .pattern("^#[0-9A-Fa-f]{6}$")
    .description("Hex color code (e.g., #FF5733)");
```

### 4. Enumerations

```rust
// String enum
let status = Schema::string()
    .enum_values(&["draft", "published", "archived"])
    .default(json!("draft"))
    .description("Post status");

// Integer enum
let priority = Schema::integer()
    .enum_values(&[1, 2, 3, 4, 5])
    .description("Priority level (1=low, 5=critical)");

// Mixed types (use anyOf for this)
let flexible = Schema::any_of(vec![
    Schema::string().enum_values(&["auto"]),
    Schema::integer().minimum(0).maximum(100),
]);
```

### 5. Schema Composition

#### anyOf - Union Types

```rust
// Field can be string OR integer
let id = Schema::any_of(vec![
    Schema::string().format("uuid"),
    Schema::integer().minimum(1),
])
.description("User ID: UUID string or positive integer");

// Nullable by union
let optional_name = Schema::any_of(vec![
    Schema::string(),
    Schema::object().property("type", Schema::string().enum_values(&["null"])),
]);
```

#### allOf - Schema Inheritance

```rust
// Base schema
let base_entity = Schema::object()
    .property("id", Schema::string().format("uuid").read_only(true))
    .property("createdAt", Schema::string().format("date-time").read_only(true))
    .required(&["id", "createdAt"]);

// Extended schema
let user = Schema::all_of(vec![
    Schema::reference("BaseEntity"),
    Schema::object()
        .property("email", Schema::string().format("email"))
        .property("name", Schema::string())
        .required(&["email", "name"]),
])
.description("User extends BaseEntity with email and name");
```

#### oneOf - Discriminated Unions

```rust
// Payment methods with different shapes
let payment = Schema::one_of(vec![
    Schema::object()
        .property("type", Schema::string().enum_values(&["card"]))
        .property("cardNumber", Schema::string().pattern(r"^\d{16}$"))
        .property("cvv", Schema::string().pattern(r"^\d{3,4}$"))
        .required(&["type", "cardNumber", "cvv"])
        .description("Credit card payment"),

    Schema::object()
        .property("type", Schema::string().enum_values(&["paypal"]))
        .property("email", Schema::string().format("email"))
        .required(&["type", "email"])
        .description("PayPal payment"),

    Schema::object()
        .property("type", Schema::string().enum_values(&["cash"]))
        .property("amount", Schema::number().minimum(0))
        .required(&["type", "amount"])
        .description("Cash payment"),
])
.description("Payment method (exactly one type)");
```

### 6. Metadata & Documentation

```rust
let settings = Schema::object()
    .description("User preferences and configuration")
    .property(
        "theme",
        Schema::string()
            .enum_values(&["light", "dark", "auto"])
            .default(json!("auto"))
            .example(json!("dark"))
            .description("UI color theme")
    )
    .property(
        "language",
        Schema::string()
            .default(json!("en"))
            .examples(vec![json!("en"), json!("es"), json!("fr"), json!("de")])
            .description("Preferred language (ISO 639-1 code)")
    )
    .property(
        "fontSize",
        Schema::integer()
            .minimum(10)
            .maximum(24)
            .default(json!(14))
            .example(json!(16))
            .description("Font size in pixels")
    );
```

### 7. Request/Response Modifiers

```rust
let user_schema = Schema::object()
    .description("User account")

    // Read-only: Server-generated, never accepted in requests
    .property(
        "id",
        Schema::string()
            .format("uuid")
            .read_only(true)
            .description("Auto-generated user ID")
    )
    .property(
        "createdAt",
        Schema::string()
            .format("date-time")
            .read_only(true)
            .description("Account creation timestamp")
    )
    .property(
        "updatedAt",
        Schema::string()
            .format("date-time")
            .read_only(true)
            .description("Last update timestamp")
    )

    // Write-only: Accepted in requests, never returned
    .property(
        "password",
        Schema::string()
            .format("password")
            .min_length(8)
            .write_only(true)
            .description("User password (min 8 characters)")
    )

    // Regular fields
    .property(
        "email",
        Schema::string()
            .format("email")
            .description("User email address")
    )
    .property(
        "name",
        Schema::string()
            .min_length(1)
            .max_length(100)
            .description("Full name")
    )

    // Deprecated field
    .property(
        "username",
        Schema::string()
            .deprecated(true)
            .description("DEPRECATED: Use 'email' instead")
    )

    .required(&["email", "name", "password"]);
```

### 8. Schema References

```rust
use domainstack_schema::{OpenApiBuilder, ToSchema};

// Define reusable schemas
struct Address { /* ... */ }
impl ToSchema for Address {
    fn schema_name() -> &'static str { "Address" }
    fn schema() -> Schema {
        Schema::object()
            .property("street", Schema::string())
            .property("city", Schema::string())
            .property("zipCode", Schema::string())
            .required(&["street", "city", "zipCode"])
    }
}

struct User { /* ... */ }
impl ToSchema for User {
    fn schema_name() -> &'static str { "User" }
    fn schema() -> Schema {
        Schema::object()
            .property("name", Schema::string())
            // Reference to Address schema
            .property("address", Schema::reference("Address"))
            // Array of references
            .property("previousAddresses",
                Schema::array(Schema::reference("Address"))
            )
    }
}

// Build spec - references auto-resolved
let spec = OpenApiBuilder::new("API", "1.0.0")
    .register::<Address>()
    .register::<User>()
    .build();
```

### 9. Vendor Extensions

```rust
use serde_json::json;

// For validations that don't map to OpenAPI
let date_range = Schema::object()
    .property("startDate", Schema::string().format("date"))
    .property("endDate", Schema::string().format("date"))
    .required(&["startDate", "endDate"])

    // Cross-field validation via vendor extension
    .extension("x-domainstack-validations", json!({
        "cross_field": [{
            "rule": "endDate > startDate",
            "message": "End date must be after start date",
            "code": "invalid_date_range"
        }]
    }))

    // Custom metadata for your tools
    .extension("x-ui-hints", json!({
        "widget": "date-range-picker",
        "allowPast": false
    }));

// Conditional validation
let order = Schema::object()
    .property("total", Schema::number().minimum(0))
    .property("couponCode", Schema::string())
    .extension("x-domainstack-validations", json!({
        "conditional": [{
            "when": "couponCode.isPresent()",
            "then": {
                "rule": "total > 10.00",
                "message": "Coupon requires minimum $10 order"
            }
        }]
    }));

// Database-level validations
let user = Schema::object()
    .property("email", Schema::string().format("email"))
    .extension("x-domainstack-validations", json!({
        "async": [{
            "rule": "unique_in_db",
            "field": "email",
            "message": "Email already registered"
        }]
    }));
```

---

## Unsupported Features

### OpenAPI 3.0 Features NOT Supported

These features are intentionally out of scope for `domainstack-schema`:

#### 1. API Operations & Paths

```yaml
# NOT SUPPORTED - Use framework adapters instead
paths:
  /users:
    get:
      summary: List users
      responses:
        '200':
          description: Success
```

**Why:** Path/operation definitions are framework-specific. Use:
- `domainstack-axum` for Axum
- `domainstack-actix` for Actix-web
- Or integrate with `utoipa`, `aide`, etc.

#### 2. Security Schemes

```yaml
# NOT SUPPORTED
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
```

**Why:** Authentication is framework/app-specific.

#### 3. Advanced Schema Features

- **discriminator**: Polymorphism hints
  - **Workaround:** Use `oneOf` with type field in each variant

- **not**: Schema negation
  - **Workaround:** Document in description, validate in code

- **exclusiveMinimum/Maximum**: Strict inequalities
  - **Workaround:** Use `minimum(n + 1)` for integers, document for floats

- **xml**: XML-specific metadata
  - **Not needed:** Focus is JSON APIs

#### 4. Object Constraints

- **minProperties/maxProperties**: Property count limits
  - **Workaround:** Document in description, validate in code

- **propertyNames**: Pattern for property names
  - **Use case rare:** Define explicit properties instead

- **patternProperties**: Dynamic property patterns
  - **Workaround:** Use `additionalProperties` with description

---

## Workarounds for Limitations

### 1. Nullable Types

**OpenAPI 3.0 has `nullable: true`**, but we represent this idiomatically in Rust:

```rust
// Instead of Schema with nullable: true
// Use Rust's Option<T>
struct User {
    required_field: String,
    optional_field: Option<String>,  // This is nullable
}

impl ToSchema for User {
    fn schema() -> Schema {
        Schema::object()
            .property("requiredField", Schema::string())
            .property("optionalField", Schema::string())  // Not in required
            .required(&["requiredField"])  // Only required field listed
    }
}

// For union with null, use anyOf
let nullable_string = Schema::any_of(vec![
    Schema::string(),
    Schema::object(),  // Represents null in JSON
]);
```

### 2. Exclusive Minimum/Maximum

```rust
// For integers: use minimum(n + 1)
let positive = Schema::integer()
    .minimum(1)  // equivalent to exclusiveMinimum: 0
    .description("Positive integer (> 0)");

// For floats: document in description
let price = Schema::number()
    .minimum(0.01)  // Close enough
    .description("Price (must be > 0.00)");
```

### 3. Conditional Schemas

```rust
// Use oneOf with explicit variants
let contact = Schema::one_of(vec![
    Schema::object()
        .property("type", Schema::string().enum_values(&["email"]))
        .property("address", Schema::string().format("email"))
        .required(&["type", "address"]),

    Schema::object()
        .property("type", Schema::string().enum_values(&["phone"]))
        .property("number", Schema::string().pattern(r"^\+?[0-9]{10,}$"))
        .required(&["type", "number"]),
])
.description("Contact method: email or phone");
```

### 4. Dependencies Between Fields

```rust
// Use vendor extensions to document
let billing = Schema::object()
    .property("method", Schema::string().enum_values(&["card", "invoice"]))
    .property("cardNumber", Schema::string())
    .property("billingAddress", Schema::reference("Address"))
    .extension("x-field-dependencies", json!({
        "cardNumber": {
            "requiredWhen": { "method": "card" },
            "message": "Card number required when method is 'card'"
        },
        "billingAddress": {
            "requiredWhen": { "method": "invoice" },
            "message": "Billing address required for invoices"
        }
    }))
    .required(&["method"]);
```

---

## Best Practices

### 1. Schema Naming

```rust
// GOOD: Clear, consistent naming
impl ToSchema for User {
    fn schema_name() -> &'static str { "User" }  // Singular, PascalCase
}

impl ToSchema for UserSettings {
    fn schema_name() -> &'static str { "UserSettings" }
}

// BAD: Inconsistent naming
fn schema_name() -> &'static str { "user" }  // lowercase
fn schema_name() -> &'static str { "Users" }  // Plural
fn schema_name() -> &'static str { "user_settings" }  // snake_case
```

### 2. Description Quality

```rust
// GOOD: Descriptive, helpful
Schema::string()
    .format("email")
    .description("User's primary email address for login and notifications")

Schema::integer()
    .minimum(0)
    .maximum(5)
    .description("Priority level: 0 (lowest) to 5 (critical)")

// BAD: Redundant or missing
Schema::string()
    .description("Email")  // Just restates the field name

Schema::integer()
    .minimum(0)
    .maximum(5)
    // No description - what do the numbers mean?
```

### 3. Use Appropriate Types

```rust
// GOOD: Semantic types
let id = Schema::string().format("uuid");
let created_at = Schema::string().format("date-time");
let price = Schema::number();  // Can have decimals

// BAD: Generic types
let id = Schema::string();  // Missing format hint
let created_at = Schema::string();  // Missing format
let price = Schema::integer();  // Loses cents!
```

### 4. Required vs Optional Fields

```rust
// GOOD: Clear requirements
Schema::object()
    .property("id", Schema::string())          // Required
    .property("email", Schema::string())       // Required
    .property("phone", Schema::string())       // Optional
    .property("middleName", Schema::string())  // Optional
    .required(&["id", "email"])  // Explicit list

// BAD: Everything required or unclear
Schema::object()
    // ... properties ...
    .required(&["id", "email", "phone", "middleName"])  // Too strict

// Or worse: no required at all
Schema::object()
    // ... properties ...
    // Missing .required() - nothing is required!
```

### 5. readOnly vs writeOnly

```rust
// GOOD: Appropriate modifiers
Schema::object()
    .property("id",
        Schema::string()
            .read_only(true)  // Server generates, never accept from client
    )
    .property("password",
        Schema::string()
            .write_only(true)  // Accept in request, never return
    )
    .property("email",
        Schema::string()  // Both read and write
    )

// BAD: Contradictory modifiers
Schema::string()
    .read_only(true)
    .write_only(true)  // Impossible! Can't be both
```

### 6. Vendor Extensions Naming

```rust
// GOOD: Prefixed with x-, clear purpose
.extension("x-domainstack-validations", json!({ /* ... */ }))
.extension("x-ui-hints", json!({ /* ... */ }))
.extension("x-api-gateway-config", json!({ /* ... */ }))

// BAD: Missing x- prefix (invalid OpenAPI)
.extension("custom-data", json!({ /* ... */ }))  // Must start with x-
.extension("validation", json!({ /* ... */ }))   // Reserved word
```

### 7. Examples for Better DX

```rust
// GOOD: Realistic, helpful examples
Schema::string()
    .format("email")
    .example(json!("user@example.com"))

Schema::object()
    .property("name", Schema::string().example(json!("Alice Johnson")))
    .property("age", Schema::integer().example(json!(28)))
    .property("tags", Schema::array(Schema::string())
        .examples(vec![
            json!(["rust", "api", "backend"]),
            json!(["frontend", "react"]),
        ])
    )

// BAD: Unhelpful examples
Schema::string()
    .format("email")
    .example(json!("string"))  // Not a real email!

Schema::integer()
    .minimum(18)
    .example(json!(0))  // Violates constraints!
```

---

## Complete Examples

### Example 1: E-commerce Product API

```rust
use domainstack_schema::{OpenApiBuilder, Schema, ToSchema};
use serde_json::json;

#[allow(dead_code)]
struct Product {
    id: String,
    name: String,
    description: String,
    price: f64,
    category: String,
    tags: Vec<String>,
    in_stock: bool,
    variants: Vec<ProductVariant>,
}

impl ToSchema for Product {
    fn schema_name() -> &'static str { "Product" }

    fn schema() -> Schema {
        Schema::object()
            .description("Product catalog item")
            .property(
                "id",
                Schema::string()
                    .format("uuid")
                    .read_only(true)
                    .description("Unique product identifier")
            )
            .property(
                "name",
                Schema::string()
                    .min_length(1)
                    .max_length(200)
                    .example(json!("Premium Wireless Headphones"))
                    .description("Product name")
            )
            .property(
                "description",
                Schema::string()
                    .max_length(2000)
                    .example(json!("High-quality wireless headphones with noise cancellation"))
                    .description("Full product description")
            )
            .property(
                "price",
                Schema::number()
                    .minimum(0.01)
                    .maximum(1000000.0)
                    .multiple_of(0.01)
                    .example(json!(199.99))
                    .description("Price in USD")
            )
            .property(
                "category",
                Schema::string()
                    .enum_values(&["electronics", "clothing", "books", "home", "sports"])
                    .example(json!("electronics"))
                    .description("Product category")
            )
            .property(
                "tags",
                Schema::array(Schema::string())
                    .min_items(1)
                    .max_items(10)
                    .unique_items(true)
                    .examples(vec![
                        json!(["wireless", "audio", "premium"]),
                        json!(["sale", "featured"]),
                    ])
                    .description("Search tags")
            )
            .property(
                "inStock",
                Schema::boolean()
                    .default(json!(true))
                    .description("Whether product is currently in stock")
            )
            .property(
                "variants",
                Schema::array(Schema::reference("ProductVariant"))
                    .min_items(1)
                    .description("Available product variants (color, size, etc.)")
            )
            .required(&["id", "name", "price", "category", "tags", "variants"])
    }
}

#[allow(dead_code)]
struct ProductVariant {
    sku: String,
    color: String,
    size: String,
    stock_quantity: u32,
}

impl ToSchema for ProductVariant {
    fn schema_name() -> &'static str { "ProductVariant" }

    fn schema() -> Schema {
        Schema::object()
            .description("Product variant with specific attributes")
            .property(
                "sku",
                Schema::string()
                    .pattern("^[A-Z0-9]{8,12}$")
                    .example(json!("PRD12345678"))
                    .description("Stock Keeping Unit")
            )
            .property(
                "color",
                Schema::string()
                    .example(json!("Midnight Black"))
                    .description("Color option")
            )
            .property(
                "size",
                Schema::string()
                    .enum_values(&["XS", "S", "M", "L", "XL", "XXL"])
                    .description("Size option")
            )
            .property(
                "stockQuantity",
                Schema::integer()
                    .minimum(0)
                    .default(json!(0))
                    .description("Available stock quantity")
            )
            .required(&["sku", "color", "size", "stockQuantity"])
    }
}

fn main() {
    let spec = OpenApiBuilder::new("E-commerce API", "1.0.0")
        .description("Product catalog and inventory management")
        .register::<Product>()
        .register::<ProductVariant>()
        .build();

    println!("{}", spec.to_json().unwrap());
}
```

### Example 2: User Management with Authentication

```rust
use domainstack_schema::{OpenApiBuilder, Schema, ToSchema};
use serde_json::json;

#[allow(dead_code)]
struct UserRegistration {
    email: String,
    password: String,
    name: String,
}

impl ToSchema for UserRegistration {
    fn schema_name() -> &'static str { "UserRegistration" }

    fn schema() -> Schema {
        Schema::object()
            .description("User registration request")
            .property(
                "email",
                Schema::string()
                    .format("email")
                    .example(json!("user@example.com"))
                    .description("User email address (must be unique)")
            )
            .property(
                "password",
                Schema::string()
                    .format("password")
                    .min_length(8)
                    .max_length(128)
                    .write_only(true)
                    .description("Password (min 8 characters, will be hashed)")
                    .extension("x-validation-rules", json!({
                        "pattern": "Must contain uppercase, lowercase, and number",
                        "forbidden": ["password123", "12345678"]
                    }))
            )
            .property(
                "name",
                Schema::string()
                    .min_length(1)
                    .max_length(100)
                    .example(json!("Alice Johnson"))
                    .description("Full name")
            )
            .required(&["email", "password", "name"])
    }
}

#[allow(dead_code)]
struct UserProfile {
    id: String,
    email: String,
    name: String,
    avatar_url: Option<String>,
    created_at: String,
    updated_at: String,
}

impl ToSchema for UserProfile {
    fn schema_name() -> &'static str { "UserProfile" }

    fn schema() -> Schema {
        Schema::object()
            .description("User profile (response only)")
            .property(
                "id",
                Schema::string()
                    .format("uuid")
                    .read_only(true)
                    .example(json!("550e8400-e29b-41d4-a716-446655440000"))
                    .description("Unique user ID")
            )
            .property(
                "email",
                Schema::string()
                    .format("email")
                    .read_only(true)
                    .description("User email (cannot be changed)")
            )
            .property(
                "name",
                Schema::string()
                    .min_length(1)
                    .max_length(100)
                    .description("User full name")
            )
            .property(
                "avatarUrl",
                Schema::string()
                    .format("uri")
                    .description("Profile picture URL (optional)")
            )
            .property(
                "createdAt",
                Schema::string()
                    .format("date-time")
                    .read_only(true)
                    .example(json!("2025-01-15T10:30:00Z"))
                    .description("Account creation timestamp")
            )
            .property(
                "updatedAt",
                Schema::string()
                    .format("date-time")
                    .read_only(true)
                    .description("Last profile update timestamp")
            )
            .required(&["id", "email", "name", "createdAt", "updatedAt"])
    }
}

fn main() {
    let spec = OpenApiBuilder::new("User Management API", "1.0.0")
        .description("User registration and profile management")
        .register::<UserRegistration>()
        .register::<UserProfile>()
        .build();

    println!("{}", spec.to_json().unwrap());
}
```

---

## Validation Coverage

### What Gets Validated at Compile Time

- Schema structure (correct types)
- Method chaining correctness
- ToSchema trait implementation

### What Gets Validated at Runtime

- Example values matching schema constraints (not enforced - your responsibility)
- Reference validity (schema name exists)
- Required fields are defined as properties

### What You Need to Validate Manually

- Examples match constraints
- Descriptions are helpful
- All referenced schemas are registered
- Vendor extension structure

**Recommendation:** Always run generated OpenAPI through a validator like [Swagger Editor](https://editor.swagger.io/) or `openapi-spec-validator`.

---

## Performance Characteristics

| Operation | Time Complexity | Notes |
|-----------|----------------|-------|
| Schema creation | O(1) | Builder pattern, no validation |
| Adding properties | O(1) | HashMap insert |
| ToJSON serialization | O(n) | Where n = total schema nodes |
| Reference resolution | O(1) | HashMap lookup |

**Memory:** Schemas are small in memory (~100-500 bytes each). Building a 100-schema spec takes <50KB RAM.

---

## Version Compatibility

| domainstack-schema | OpenAPI Spec | Rust MSRV |
|-------------------|--------------|-----------|
| 0.7.0 | 3.0.0 | 1.76+ |
| 0.8.0 | 3.0.0 | 1.76+ |
| Future 1.0.0 | 3.0.0 / 3.1.0 | 1.76+ |

**OpenAPI 3.1.0 support:** Planned for post-1.0 (adds JSON Schema 2020-12 alignment).

---

## Next Steps

1. **Read the README** - Quick start guide
2. **Run examples** - `cargo run --example v08_features`
3. **Implement ToSchema** - For your domain types
4. **Validate output** - Use Swagger Editor
5. **Integrate with framework** - Use domainstack-axum or similar

For questions or issues: https://github.com/blackwell-systems/domainstack/issues