backbone-mail 0.2.32

Odoo mail core port — message/notification/followers/activity/alias/sms queue (schema: messaging)
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
# Type System Reference

Complete reference for all types supported in the Backbone Schema System.

> **Note**: This is a deep reference. For day-to-day field declaration, see [RULE_FORMAT_MODELS.md]./RULE_FORMAT_MODELS.md. The Field Types and Field Attributes tables there cover the 90% case. Read this document when you need composition rules, value-object construction, or proto / SQL mappings.

## Table of Contents

- [Primitive Types]#primitive-types
- [Special Types]#special-types
- [Collection Types]#collection-types
- [Custom Types]#custom-types
- [Shared Type Composition]#shared-type-composition
- [Value Objects & Typed IDs]#value-objects--typed-ids
- [Type Naming Conventions]#type-naming-conventions
- [Type Mappings]#type-mappings
- [Nullability]#nullability
- [Type Coercion]#type-coercion
- [Validation Attributes Quick Reference]#validation-attributes-quick-reference

---

## Primitive Types

### String Types

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `string` | Variable length text | `String` | `TEXT` | `string` |
| `string(n)` | Fixed max length | `String` | `VARCHAR(n)` | `string` |
| `text` | Long text | `String` | `TEXT` | `string` |
| `char(n)` | Fixed length | `String` | `CHAR(n)` | `string` |

```yaml
fields:
  name:
    type: string                 # TEXT

  code:
    type: string
    attributes: ["@max(10)"]     # VARCHAR(10)

  description:
    type: text                   # TEXT

  country:
    type: string
    attributes: ["@length(2)"]   # CHAR(2)
```

### Numeric Types

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `int` | 32-bit integer | `i32` | `INTEGER` | `int32` |
| `int8` | 8-bit integer | `i8` | `SMALLINT` | `int32` |
| `int16` | 16-bit integer | `i16` | `SMALLINT` | `int32` |
| `int32` | 32-bit integer | `i32` | `INTEGER` | `int32` |
| `int64` | 64-bit integer | `i64` | `BIGINT` | `int64` |
| `uint` | Unsigned 32-bit | `u32` | `INTEGER` | `uint32` |
| `uint64` | Unsigned 64-bit | `u64` | `BIGINT` | `uint64` |
| `float` | 32-bit float | `f32` | `REAL` | `float` |
| `float64` | 64-bit float | `f64` | `DOUBLE PRECISION` | `double` |
| `decimal` | Exact decimal | `Decimal` | `NUMERIC` | `string` |

```yaml
fields:
  count:
    type: int                    # INTEGER

  big_count:
    type: int64                  # BIGINT

  price:
    type: decimal                # NUMERIC

  amount:
    type: float64                # DOUBLE PRECISION

  percentage:
    type: float                  # REAL
```

### Decimal Precision

```yaml
fields:
  price:
    type: decimal
    attributes: ["@precision(10, 2)"]    # NUMERIC(10, 2)

  rate:
    type: decimal
    attributes: ["@precision(5, 4)"]     # NUMERIC(5, 4)

  amount:
    type: decimal
    attributes: ["@precision(19, 4)"]    # NUMERIC(19, 4) - money
```

### Boolean

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `bool` | True/False | `bool` | `BOOLEAN` | `bool` |

```yaml
fields:
  is_active:
    type: bool                   # BOOLEAN

  verified:
    type: bool
    attributes: ["@default(false)"]
```

### Date and Time

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `datetime` | Date and time with TZ | `DateTime<Utc>` | `TIMESTAMPTZ` | `google.protobuf.Timestamp` |
| `date` | Date only | `NaiveDate` | `DATE` | `string` |
| `time` | Time only | `NaiveTime` | `TIME` | `string` |
| `timestamp` | Unix timestamp | `i64` | `BIGINT` | `int64` |
| `duration` | Time duration | `Duration` | `INTERVAL` | `google.protobuf.Duration` |

```yaml
fields:
  created_at:
    type: datetime               # TIMESTAMPTZ

  birth_date:
    type: date                   # DATE

  start_time:
    type: time                   # TIME

  expires_at:
    type: timestamp              # BIGINT (unix)

  ttl:
    type: duration               # INTERVAL
```

### Identifiers

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `uuid` | UUID v4 | `Uuid` | `UUID` | `string` |
| `cuid` | CUID | `String` | `VARCHAR(25)` | `string` |
| `ulid` | ULID | `String` | `VARCHAR(26)` | `string` |

```yaml
fields:
  id:
    type: uuid
    attributes: ["@id", "@default(uuid)"]

  public_id:
    type: cuid
    attributes: ["@default(cuid)"]

  sort_id:
    type: ulid
    attributes: ["@default(ulid)"]
```

---

## Special Types

### Validated String Types

These types are shortcuts that combine a base type with built-in validation:

| Type | Validation | Example | Equivalent |
|------|------------|---------|------------|
| `email` | Email format | `user@example.com` | `string @email` |
| `url` | URL format | `https://example.com` | `string @url` |
| `phone` | Phone format | `+1-555-555-5555` | `string @phone` |
| `ip` | IP address | `192.168.1.1` | `string @ip` |
| `ipv4` | IPv4 address | `192.168.1.1` | `string @ipv4` |
| `ipv6` | IPv6 address | `::1` | `string @ipv6` |
| `slug` | URL-safe string | `my-blog-post` | `string @slug` |
| `username` | Alphanumeric + underscore | `john_doe` | `string @alpha_dash` |
| `domain` | Domain name | `example.com` | `string @domain` |
| `hostname` | Hostname | `server-01.local` | `string @hostname` |
| `mac_address` | MAC address | `00:1B:44:11:3A:B7` | `string @mac_address` |
| `hex_color` | Hex color | `#ff5733` | `string @hex_color` |
| `credit_card` | Credit card number | `4111111111111111` | `string @credit_card` |
| `iban` | IBAN number | `DE89370400440532013000` | `string @iban` |
| `isbn` | ISBN-10 or ISBN-13 | `978-3-16-148410-0` | `string @isbn` |
| `country_code` | ISO 3166-1 alpha-2 | `US` | `string @country_code` |
| `currency_code` | ISO 4217 | `USD` | `string @currency_code` |
| `language_code` | ISO 639-1 | `en` | `string @language_code` |
| `locale` | Locale code | `en_US` | `string @locale` |
| `timezone` | Timezone | `America/New_York` | `string @timezone` |

```yaml
fields:
  email:
    type: email
    attributes: ["@required"]     # Validates email format

  website:
    type: url?                    # Validates URL format

  phone:
    type: phone?                  # Validates phone format

  ip_address:
    type: ip?                     # Validates IP format

  country:
    type: country_code            # ISO country code

  currency:
    type: currency_code           # ISO currency code
```

These are shortcuts for:

```yaml
fields:
  email:
    type: string
    attributes: ["@email", "@required"]

  website:
    type: string?
    attributes: ["@url"]

  country:
    type: string
    attributes: ["@country_code"]
```

### Password Type

Special type for password fields with built-in security:

```yaml
fields:
  password:
    type: password
    attributes: ["@required"]    # Implies @sensitive @hashed
```

Equivalent to:

```yaml
fields:
  password:
    type: string
    attributes: ["@min(8)", "@password", "@sensitive", "@hashed", "@required"]
```

### File Types

Types for file handling (typically used with external storage):

| Type | Description | Validation |
|------|-------------|------------|
| `file` | Generic file reference | Valid file path/URL |
| `image` | Image file reference | Valid image (jpg, png, gif, webp, svg) |
| `document` | Document file reference | Valid document (pdf, doc, docx, etc.) |

```yaml
fields:
  avatar:
    type: image?                 # Image file URL

  resume:
    type: document?              # Document file URL

  attachment:
    type: file?                  # Any file URL
```

With size and dimension constraints:

```yaml
fields:
  avatar:
    type: image
    attributes: ["@max_size(2097152)", "@dimensions(min_width: 100, max_width: 500)"]

  thumbnail:
    type: image
    attributes: ["@aspect_ratio(1:1)", "@max_size(102400)"]

  document:
    type: file
    attributes: ["@extensions(pdf, docx)", "@max_size(10485760)"]
```

### Binary Types

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `bytes` | Binary data | `Vec<u8>` | `BYTEA` | `bytes` |
| `blob` | Large binary | `Vec<u8>` | `BYTEA` | `bytes` |

```yaml
fields:
  avatar:
    type: bytes?                 # Binary image data

  document:
    type: blob?                  # Large binary file
```

### Structured Types

| Type | Description | Rust | PostgreSQL | Proto |
|------|-------------|------|------------|-------|
| `json` | JSON data | `serde_json::Value` | `JSONB` | `google.protobuf.Struct` |
| `jsonb` | Binary JSON | `serde_json::Value` | `JSONB` | `google.protobuf.Struct` |

```yaml
fields:
  metadata:
    type: json?                  # Arbitrary JSON

  settings:
    type: jsonb?                 # Binary JSON (faster queries)

  config:
    type: json
    attributes: ["@default({})"] # Default empty object
```

---

## Collection Types

### Arrays

Use `[]` suffix for array types:

```yaml
fields:
  tags:
    type: string[]               # Array of strings

  scores:
    type: int[]                  # Array of integers

  coordinates:
    type: float[]                # Array of floats
```

| Schema | Rust | PostgreSQL | Proto |
|--------|------|------------|-------|
| `string[]` | `Vec<String>` | `TEXT[]` | `repeated string` |
| `int[]` | `Vec<i32>` | `INTEGER[]` | `repeated int32` |
| `uuid[]` | `Vec<Uuid>` | `UUID[]` | `repeated string` |

### Maps

```yaml
fields:
  labels:
    type: map<string, string>    # Key-value pairs

  counts:
    type: map<string, int>       # String to int map
```

| Schema | Rust | PostgreSQL | Proto |
|--------|------|------------|-------|
| `map<K, V>` | `HashMap<K, V>` | `JSONB` | `map<K, V>` |

---

## Custom Types

Custom types allow you to define reusable field structures. They can be used in three ways:
1. **Extends** - Inject fields as table columns (type inheritance)
2. **JSONB field** - Store as JSONB column with validation
3. **Type composition** - Combine multiple types into one

### Defining Custom Types

Define reusable composite types in `index.model.yaml`:

```yaml
# In index.model.yaml

shared_types:
  Money:
    amount:
      type: decimal
      attributes: ["@precision(19, 4)"]
    currency:
      type: string
      attributes: ["@default(USD)", "@max(3)"]

  Address:
    street:
      type: string
      attributes: ["@required", "@max(255)"]
    street2:
      type: string?
      attributes: ["@max(255)"]
    city:
      type: string
      attributes: ["@required", "@max(100)"]
    state:
      type: string?
      attributes: ["@max(100)"]
    country:
      type: string
      attributes: ["@required", "@default(ID)", "@max(2)"]
    postal_code:
      type: string?
      attributes: ["@max(20)"]

  Coordinates:
    latitude:
      type: float64
      attributes: ["@range(-90, 90)"]
    longitude:
      type: float64
      attributes: ["@range(-180, 180)"]

  ContactInfo:
    email:
      type: email
      attributes: ["@required"]
    phone:
      type: phone?
    fax:
      type: phone?

  # Base audit types
  Timestamps:
    created_at:
      type: datetime
      attributes: ["@default(now)"]
    updated_at:
      type: datetime
      attributes: ["@updated_at"]
    deleted_at:
      type: datetime?

  Actors:
    created_by:
      type: uuid?
    updated_by:
      type: uuid?
    deleted_by:
      type: uuid?

  # Type composition - combine multiple types
  Metadata: [Timestamps, Actors]
```

### Type Composition

You can compose types from other types using array syntax:

```yaml
shared_types:
  Timestamps:
    created_at:
      type: datetime
      attributes: ["@default(now)"]
    updated_at:
      type: datetime
      attributes: ["@updated_at"]

  Actors:
    created_by: uuid?
    updated_by: uuid?

  # Metadata = Timestamps + Actors (6 fields total)
  Metadata: [Timestamps, Actors]
```

The composed type `Metadata` will contain all fields from both `Timestamps` and `Actors`.

### Using Custom Types

#### 1. Type Inheritance with `extends`

Use `extends` to inject shared type fields as **direct table columns**:

```yaml
models:
  - name: Order
    collection: orders
    extends: [Metadata]  # All Metadata fields become columns

    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]

      total:
        type: decimal
```

**Result:** The `orders` table will have columns: `id`, `total`, `created_at`, `updated_at`, `deleted_at`, `created_by`, `updated_by`, `deleted_by`

**Key behaviors:**
- Extended fields are injected as **direct columns**
- Explicit fields override inherited fields with the same name
- Multiple types can be extended: `extends: [Timestamps, Actors]`

#### 2. Shared Type as JSONB Field

Use a shared type name as the field type to store as **JSONB**:

```yaml
models:
  - name: Order
    collection: orders

    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]

      shipping:
        type: Address              # Stored as JSONB column
        attributes: ["@required"]

      billing:
        type: Address?             # Optional JSONB column
```

**Generated SQL:**
```sql
CREATE TABLE orders (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    shipping JSONB NOT NULL CHECK (
        shipping ? 'street' AND
        shipping ? 'city' AND
        shipping ? 'country'
    ),
    billing JSONB
);
```

**Key behaviors:**
- The column type is `JSONB`
- PostgreSQL CHECK constraints enforce required fields
- Non-required fields in the type don't generate CHECK constraints
- The `@jsonb_type` and `@jsonb_schema` attributes are added internally

### File-Level Types

Define types at the file level (same level as `models:` and `enums:`) for reuse across multiple models:

```yaml
# File-level types - reusable across all models in this file
types:
  - name: Address
    fields:
      street:
        type: string
        attributes: ["@required"]
      city:
        type: string
        attributes: ["@required"]
      zip:
        type: string?

  - name: LineItem
    fields:
      product_id:
        type: uuid
        attributes: ["@required"]
      quantity:
        type: int
        attributes: ["@required", "@positive"]
      unit_price:
        type: decimal
        attributes: ["@precision(10, 2)"]

models:
  - name: Customer
    collection: customers
    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]
      billing_address: Address     # Reuse file-level type
      shipping_address: Address    # Same type, different field

  - name: Order
    collection: orders
    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]
      items:
        type: LineItem[]           # Reuse as array
      delivery: Address            # Same Address type
```

**Key behaviors:**
- File-level types are defined alongside `models:` and `enums:`
- They can be reused across **all models** in the same file
- They work the same as shared types (stored as JSONB)
- File-level types take precedence over shared types with the same name
- More flexible than model-local types for multi-model files

### Type with Validation

```yaml
shared_types:
  Password:
    value:
      type: string
      attributes: ["@min(8)", "@max(128)", "@sensitive", "@hashed"]

  Percentage:
    value:
      type: float
      attributes: ["@range(0, 100)"]

  PositiveAmount:
    value:
      type: decimal
      attributes: ["@precision(19, 4)", "@min(0)"]
```

### JSONB Validation

When using shared types as JSONB fields, validation is enforced at the database level:

| Attribute | PostgreSQL CHECK |
|-----------|------------------|
| `@required` | `column ? 'field_name'` |
| Non-required (optional) | No CHECK constraint |

**Example:**
```yaml
shared_types:
  Settings:
    theme:
      type: string
      attributes: ["@required"]     # Will have CHECK
    notifications:
      type: bool                    # No CHECK (optional)
    language:
      type: string
      attributes: ["@default(en)"]  # No CHECK (has default)
```

**Generated CHECK:**
```sql
CHECK (settings ? 'theme')
```

---

## Shared Type Composition

Shared types are reusable field bundles defined once in `index.model.yaml` and reused across many models. There are two ways to bring them into a model:

1. **`extends:` (column injection)** — fields become individual table columns.
2. **JSONB field with `@audit_metadata`** — fields are stored as a single JSONB column.

### Defining Shared Types

```yaml
# index.model.yaml
shared_types:
  Timestamps:
    created_at:
      type: datetime
      attributes: ["@default(now)"]
    updated_at:
      type: datetime
      attributes: ["@updated_at"]
    deleted_at: datetime?

  Actors:
    created_by:
      type: uuid?
      attributes: ["@foreign_key(User.id)"]
    updated_by:
      type: uuid?
      attributes: ["@foreign_key(User.id)"]
    deleted_by:
      type: uuid?
      attributes: ["@foreign_key(User.id)"]

  # Composition: combine other shared types
  Metadata: [Timestamps, Actors]

  # Domain-specific bundles
  Money:
    amount:
      type: decimal
      attributes: ["@precision(18,2)", "@non_negative"]
    currency:
      type: string
      attributes: ["@default('IDR')", "@length(3)"]

  GeoLocation:
    latitude:
      type: float?
      attributes: ["@range(-90, 90)"]
    longitude:
      type: float?
      attributes: ["@range(-180, 180)"]
```

### Using Shared Types as Columns (`extends`)

```yaml
models:
  - name: Order
    extends: [Metadata]   # injects created_at, updated_at, deleted_at, created_by, updated_by, deleted_by as columns
    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]
      total: decimal
```

The generated SQL has six extra columns. Use this when you want to query or index audit fields directly (`WHERE created_at > ...`).

### Using Shared Types as JSONB Fields

```yaml
models:
  - name: Order
    fields:
      id:
        type: uuid
        attributes: ["@id", "@default(uuid)"]
      total: decimal
      metadata:
        type: Metadata
        attributes: ["@audit_metadata"]
```

This produces a single `metadata JSONB` column. The `@audit_metadata` attribute also tells DTOs to **exclude** this field from Create/Update request bodies.

> **Special case**: The shared type literally named `Metadata` is treated as the audit-metadata type. The constant lives at `crates/backbone-schema/src/parser/yaml_parser/types.rs:14` (`AUDIT_METADATA_TYPE_NAME`).

### When to Use Which

| Use case | Pattern |
|----------|---------|
| You query or index audit fields | `extends: [Metadata]` |
| You want a clean schema with audit data tucked away | `metadata: Metadata` with `@audit_metadata` |
| You want to share validation rules and field shapes | Either; pick by query needs |
| You want fields excluded from Create/Update DTOs | `@audit_metadata` only |

---

## Value Objects & Typed IDs

The `value-object` generator turns plain types into newtype-wrapped Rust types with validation, conversion, and equality semantics.

### Typed IDs

Every entity gets a typed ID by default — `OrderId`, `UserId`, etc. — backed by `Uuid` but distinct at the type level. The generator emits:

- `From<Uuid>` and `Into<Uuid>`
- `From<&Uuid>` and `From<&str>` and `From<&String>` (recent additions)
- `AsRef<Uuid>` and `Deref<Target = Uuid>` so typed IDs work with anything that takes `&Uuid`
- `Display`, `Debug`, `serde::Serialize`, `serde::Deserialize`

This means you can pass `&order_id` to a function expecting `&Uuid`, and the type system still distinguishes `OrderId` from `UserId` so you can't accidentally swap them.

### Wrapper Value Objects

```yaml
# index.model.yaml
value_objects:
  Email:
    inner_type: String
    validation:
      pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
    methods:
      - name: domain
        returns: "&str"

  PhoneNumber:
    inner_type: String
    validation:
      pattern: "^\\+?[1-9]\\d{1,14}$"
```

Generated:

```rust
pub struct Email(String);

impl Email {
    pub fn try_new(value: impl Into<String>) -> Result<Self, ValidationError> { ... }
    pub fn domain(&self) -> &str { ... }
}

impl From<&str> for Email { /* with validation */ }
impl AsRef<str> for Email { ... }
```

### Composite Value Objects

```yaml
value_objects:
  Money:
    fields:
      amount:
        type: Decimal
        validation:
          min: 0
      currency:
        type: String
        validation:
          length: 3
    methods:
      - name: add
        params:
          other: Money
        returns: "Result<Money, CurrencyMismatch>"
```

Use composite VOs when two or more fields must travel together as a single concept (money, geo coordinates, address pairs).

---

## Type Naming Conventions

### Automatic PascalCase Conversion

When you define custom enum types in your schema, the code generator automatically converts them to **PascalCase** to follow Rust naming conventions.

**Schema Definition:**
```yaml
# In your model file
enums:
  - name: relationship_type
    values: [lead, customer, partner]

  - name: contact_type
    values: [email, phone, mail]

  - name: opportunity_status
    values: [open, won, lost]

models:
  - name: Contact
    fields:
      relationship:
        type: relationship_type    # lowercase in schema
      contact_method:
        type: contact_type         # lowercase in schema
      status:
        type: opportunity_status   # lowercase in schema
```

**Generated Rust Code:**
```rust
// Enum definitions - automatically converted to PascalCase
pub enum RelationshipType {
    Lead,
    Customer,
    Partner,
}

pub enum ContactType {
    Email,
    Phone,
    Mail,
}

pub enum OpportunityStatus {
    Open,
    Won,
    Lost,
}

// Entity struct uses PascalCase enum types
pub struct Contact {
    pub relationship: RelationshipType,    // PascalCase
    pub contact_method: ContactType,       // PascalCase
    pub status: OpportunityStatus,         // PascalCase
}
```

**Key Benefits:**
- ✅ Follows Rust naming conventions (PascalCase for types)
- ✅ Prevents compilation errors from lowercase type names
- ✅ Maintains consistency across generated code
- ✅ You can use lowercase in schema (more readable)
- ✅ Generator handles conversion automatically

**Affected Generators:**
- **Entity generator** - PascalCase enum definitions
- **Validator generator** - PascalCase type references
- **Handler generator** - PascalCase in request/response types
- **DTO generator** - PascalCase in data transfer objects
- **gRPC generator** - PascalCase in proto definitions

**Module Visibility:**
All entity and enum modules are automatically declared as `pub mod` to allow external access from other modules:

```rust
// In entity/mod.rs
pub mod relationship_type;    // Public module
pub mod contact_type;         // Public module
pub mod opportunity_status;   // Public module
```

---

## Type Mappings

### Schema to Rust

| Schema Type | Rust Type |
|-------------|-----------|
| `string` | `String` |
| `int` | `i32` |
| `int64` | `i64` |
| `float` | `f32` |
| `float64` | `f64` |
| `decimal` | `rust_decimal::Decimal` |
| `bool` | `bool` |
| `datetime` | `chrono::DateTime<Utc>` |
| `date` | `chrono::NaiveDate` |
| `time` | `chrono::NaiveTime` |
| `uuid` | `uuid::Uuid` |
| `bytes` | `Vec<u8>` |
| `json` | `serde_json::Value` |
| `T?` | `Option<T>` |
| `T[]` | `Vec<T>` |
| `map<K, V>` | `std::collections::HashMap<K, V>` |
| Custom Type | Generated `struct` |
| Enum | Generated `enum` |

### Schema to PostgreSQL

| Schema Type | PostgreSQL Type |
|-------------|-----------------|
| `string` | `TEXT` |
| `string(n)` | `VARCHAR(n)` |
| `int` | `INTEGER` |
| `int64` | `BIGINT` |
| `float` | `REAL` |
| `float64` | `DOUBLE PRECISION` |
| `decimal` | `NUMERIC` |
| `decimal @precision(p, s)` | `NUMERIC(p, s)` |
| `bool` | `BOOLEAN` |
| `datetime` | `TIMESTAMPTZ` |
| `date` | `DATE` |
| `time` | `TIME` |
| `uuid` | `UUID` |
| `bytes` | `BYTEA` |
| `json` / `jsonb` | `JSONB` |
| `T?` | Column allows `NULL` |
| `T[]` | `T[]` (PostgreSQL array) |
| `map<K, V>` | `JSONB` |
| Custom Type | `JSONB` or embedded columns |
| Enum | `TEXT` with CHECK or custom ENUM |

### Schema to Proto

| Schema Type | Proto Type |
|-------------|------------|
| `string` | `string` |
| `int` | `int32` |
| `int64` | `int64` |
| `uint` | `uint32` |
| `uint64` | `uint64` |
| `float` | `float` |
| `float64` | `double` |
| `decimal` | `string` (for precision) |
| `bool` | `bool` |
| `datetime` | `google.protobuf.Timestamp` |
| `duration` | `google.protobuf.Duration` |
| `bytes` | `bytes` |
| `json` | `google.protobuf.Struct` |
| `T?` | `optional T` |
| `T[]` | `repeated T` |
| `map<K, V>` | `map<K, V>` |
| Custom Type | Generated `message` |
| Enum | Generated `enum` |

---

## Nullability

### Required vs Optional

```yaml
fields:
  email:
    type: string                 # Required (NOT NULL)

  nickname:
    type: string?                # Optional (NULL allowed)
```

### Default Values

```yaml
fields:
  status:
    type: string
    attributes: ["@default(active)"]     # Has default, technically optional

  count:
    type: int
    attributes: ["@default(0)"]          # Has default

  created_at:
    type: datetime
    attributes: ["@default(now)"]        # Auto-generated
```

### Nullability Rules

1. **No `?`, no `@default`** → Required, must be provided
2. **Has `?`** → Optional, NULL allowed
3. **Has `@default`** → Optional at creation, default applied if not provided
4. **Has both** → Optional, NULL or default value

```yaml
fields:
  # Must provide at creation
  email:
    type: string
    attributes: ["@required"]

  # Optional, can be NULL
  nickname:
    type: string?

  # Optional at creation, defaults to 0
  login_count:
    type: int
    attributes: ["@default(0)"]

  # Can be NULL or defaults to "active"
  status:
    type: string?
    attributes: ["@default(active)"]
```

---

## Type Coercion

### Automatic Coercion

The schema system performs automatic type coercion where safe:

| Input | To Type | Result |
|-------|---------|--------|
| `"123"` | `int` | `123` |
| `123` | `string` | `"123"` |
| `"true"` | `bool` | `true` |
| `1` | `bool` | `true` |
| `0` | `bool` | `false` |

### Explicit Casting

In expressions (workflow rules, computed fields):

```yaml
computed:
  amount_string: "string(amount)"        # Cast decimal to string
  count_float: "float(count)"            # Cast int to float
  is_positive: "bool(amount > 0)"        # Expression to bool
```

---

## Validation Attributes Quick Reference

Complete list of all validation attributes organized by category.

### Size & Length
`@min(n)` `@max(n)` `@length(n)` `@range(min, max)` `@size(n)`

### String Format
`@email` `@url` `@phone` `@uuid` `@ulid` `@ip` `@ipv4` `@ipv6` `@mac_address` `@hex_color` `@json` `@ascii` `@slug`

### String Content
`@alpha` `@alpha_num` `@alpha_dash` `@lowercase` `@uppercase` `@pattern(regex)` `@not_pattern(regex)` `@starts_with(values)` `@ends_with(values)` `@doesnt_start_with(values)` `@doesnt_end_with(values)` `@contains(value)` `@doesnt_contain(value)`

### Numeric
`@integer` `@positive` `@negative` `@non_negative` `@non_positive` `@multiple_of(n)` `@digits(n)` `@digits_between(min, max)` `@decimal(places)` `@decimal_between(min, max)` `@precision(p, s)`

### Date & Time
`@after(date)` `@after_or_equal(date)` `@before(date)` `@before_or_equal(date)` `@date_format(format)` `@timezone` `@past` `@future`

### Enum & Choice
`@in(values)` `@not_in(values)` `@enum(EnumType)`

### Array
`@min_items(n)` `@max_items(n)` `@distinct` `@contains_all(values)` `@contains_any(values)`

### Cross-Field
`@same(field)` `@different(field)` `@gt(field)` `@gte(field)` `@lt(field)` `@lte(field)` `@required_with(fields)` `@required_without(fields)` `@required_if(field, value)` `@required_unless(field, value)` `@prohibited_if(field, value)` `@prohibited_unless(field, value)`

### Database
`@unique` `@unique_where(condition)` `@exists(table, column)` `@exists_where(condition)`

### Conditional
`@nullable` `@filled` `@present` `@sometimes` `@required`

### File & Image
`@file` `@image` `@mimes(types)` `@extensions(exts)` `@max_size(bytes)` `@min_size(bytes)` `@dimensions(constraints)` `@aspect_ratio(ratio)`

### Password
`@password` `@password_letters` `@password_mixed_case` `@password_numbers` `@password_symbols` `@password_uncompromised` `@current_password` `@confirmed`

### Financial
`@credit_card` `@iban` `@bic`

### Locale & Region
`@country_code` `@currency_code` `@language_code` `@locale` `@postal_code(country)`

### Network & Identifiers
`@domain` `@hostname` `@active_url` `@nik` `@npwp` `@isbn` `@issn` `@ean`

### Security
`@sensitive` `@encrypted` `@hashed`

### Documentation
`@description(text)` `@deprecated(reason)`

For detailed documentation of each attribute, see [RULE_FORMAT_MODELS.md — Field Attributes](./RULE_FORMAT_MODELS.md#field-attributes).

---

## Next Steps

- [RULE_FORMAT_MODELS.md]./RULE_FORMAT_MODELS.md — entity definition syntax
- [RULE_FORMAT_HOOKS.md]./RULE_FORMAT_HOOKS.md — entity rules, state machines, permissions
- [RULE_FORMAT_WORKFLOWS.md]./RULE_FORMAT_WORKFLOWS.md — multi-step business processes
- [GENERATION.md]./GENERATION.md - Code generation
- [EXAMPLES.md]./EXAMPLES.md - Complete examples