|
| 1 | +--- |
| 2 | +title: Field Value |
| 3 | +description: Field Value protocol schemas |
| 4 | +--- |
| 5 | + |
| 6 | +{/* ⚠️ AUTO-GENERATED — DO NOT EDIT. Run build-docs.ts to regenerate. Hand-written docs live in the module folders under content/docs/. */} |
| 7 | + |
| 8 | +Field runtime VALUE-shape contract (ADR-0104 D1). |
| 9 | + |
| 10 | +`FieldSchema` owns what a field *definition* looks like; this module owns |
| 11 | + |
| 12 | +what a field's runtime *value* looks like — the shape the write path |
| 13 | + |
| 14 | +accepts, drivers persist, and an unexpanded API read returns. Before this |
| 15 | + |
| 16 | +module the knowledge lived as private, hand-copied type sets in objectql's |
| 17 | + |
| 18 | +record-validator, rest's import-coerce, driver-sql, and verify; adding one |
| 19 | + |
| 20 | +multi-capable or JSON-shaped type meant updating four lists or silently |
| 21 | + |
| 22 | +corrupting data. Those consumers now derive from the classes below. |
| 23 | + |
| 24 | +Two canonical forms exist per field (ADR-0104 D1): |
| 25 | + |
| 26 | +- `stored` — the storage/wire form (e.g. lookup ⇒ record-id string, |
| 27 | + |
| 28 | +`date` ⇒ `YYYY-MM-DD`, select ⇒ option code). |
| 29 | + |
| 30 | +- `expanded` — the enriched `$expand` read form (lookup ⇒ the related |
| 31 | + |
| 32 | +record object). For types without an expansion, |
| 33 | + |
| 34 | +expanded ≡ stored. |
| 35 | + |
| 36 | +"Reality wins": where the deployed stored shape is coherent, the contract |
| 37 | + |
| 38 | +adopts it — deployed data is a wire contract we don't get to rewrite by |
| 39 | + |
| 40 | +editing Zod. This is why `currency` is a bare number (not the never-consumed |
| 41 | + |
| 42 | +`CurrencyValueSchema` object) and `location` is `\{lat, lng\}` (what field-zoo |
| 43 | + |
| 44 | +stores), not the never-consumed `\{latitude, longitude\}` shape. |
| 45 | + |
| 46 | +Purity: schemas/constants/derivation only — no runtime logic, no caching |
| 47 | + |
| 48 | +(Prime Directive #2). Consumers cache `valueSchemaFor` results per field |
| 49 | + |
| 50 | +definition; building a Zod schema per write is the one performance trap |
| 51 | + |
| 52 | +this contract has (ADR-0104 performance budget). |
| 53 | + |
| 54 | +<Callout type="info"> |
| 55 | +**Source:** `packages/spec/src/data/field-value.zod.ts` |
| 56 | +</Callout> |
| 57 | + |
| 58 | +## TypeScript Usage |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { AddressValue, CalendarDateValue, ClockTimeValue, FileLikeValue, InstantValue, LocationValue, ReferenceIdValue } from '@objectstack/spec/data'; |
| 62 | +import type { AddressValue, CalendarDateValue, ClockTimeValue, FileLikeValue, InstantValue, LocationValue, ReferenceIdValue } from '@objectstack/spec/data'; |
| 63 | + |
| 64 | +// Validate data |
| 65 | +const result = AddressValue.parse(data); |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## AddressValue |
| 71 | + |
| 72 | +### Properties |
| 73 | + |
| 74 | +| Property | Type | Required | Description | |
| 75 | +| :--- | :--- | :--- | :--- | |
| 76 | +| **street** | `string` | optional | Street address | |
| 77 | +| **city** | `string` | optional | City name | |
| 78 | +| **state** | `string` | optional | State/Province | |
| 79 | +| **postalCode** | `string` | optional | Postal/ZIP code | |
| 80 | +| **country** | `string` | optional | Country name or code | |
| 81 | +| **countryCode** | `string` | optional | ISO country code (e.g., US, GB) | |
| 82 | +| **formatted** | `string` | optional | Formatted address string | |
| 83 | + |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## FileLikeValue |
| 94 | + |
| 95 | +### Union Options |
| 96 | + |
| 97 | +This schema accepts one of the following structures: |
| 98 | + |
| 99 | +#### Option 1 |
| 100 | + |
| 101 | +Type: `string` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +#### Option 2 |
| 106 | + |
| 107 | +### Properties |
| 108 | + |
| 109 | +| Property | Type | Required | Description | |
| 110 | +| :--- | :--- | :--- | :--- | |
| 111 | +| **url** | `string` | optional | | |
| 112 | +| **name** | `string` | optional | | |
| 113 | +| **size** | `number` | optional | | |
| 114 | +| **alt** | `string` | optional | | |
| 115 | +| **duration** | `number` | optional | | |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## LocationValue |
| 126 | + |
| 127 | +### Properties |
| 128 | + |
| 129 | +| Property | Type | Required | Description | |
| 130 | +| :--- | :--- | :--- | :--- | |
| 131 | +| **lat** | `number` | ✅ | Latitude | |
| 132 | +| **lng** | `number` | ✅ | Longitude | |
| 133 | +| **altitude** | `number` | optional | Altitude in meters | |
| 134 | +| **accuracy** | `number` | optional | Accuracy in meters | |
| 135 | + |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | + |
| 140 | +--- |
| 141 | + |
0 commit comments