|
| 1 | +--- |
| 2 | +"@objectstack/spec": minor |
| 3 | +--- |
| 4 | + |
| 5 | +fix(spec): `FieldSchema.defaultValue` is discriminated (literal / runtime token / CEL envelope) and each shape is validated on its own terms (#7127) |
| 6 | + |
| 7 | +`FieldSchema.defaultValue` was `z.unknown().optional()` — the same acceptance |
| 8 | +hole #6970 closed one layer up on action params, but NARROWER: a field default |
| 9 | +is polymorphic by design (a literal, a runtime token `NOW()` / `current_user`, |
| 10 | +or a CEL Expression envelope `{ dialect, source }`), so the vocabulary has to |
| 11 | +be subtracted BEFORE the literal can be judged. Running the value contract |
| 12 | +over the whole key judges a token's spelling as data — right only by accident |
| 13 | +(`'current_user'` passes a `user` field as a would-be record id; `'NOW()'` |
| 14 | +passes `text` as a plain string while the engine intercepts it and stores an |
| 15 | +ISO instant instead). |
| 16 | + |
| 17 | +Per the maintainer's sequenced ruling (2026-08-10), this lands in two steps |
| 18 | +inside one release: |
| 19 | + |
| 20 | +1. a shared **discriminator** (`@objectstack/spec/data`, |
| 21 | + `default-value-shape.ts`): the engine's own envelope predicate verbatim, |
| 22 | + the token predicates, and the shared literal-vs-stored-contract core — |
| 23 | + one module, two consumers. The #6970 action-param gate is refactored onto |
| 24 | + the shared core with zero behavior change. |
| 25 | +2. `FieldSchema.defaultValue` narrowed on top of it, in the engine's own |
| 26 | + discrimination order: |
| 27 | + - **absent** (`null`/`undefined`) → skipped; `''` is a real default |
| 28 | + (engine presence semantics, deliberately not the action-param rule); |
| 29 | + - **CEL envelope** → structural acceptance only (the result type is |
| 30 | + unknowable at parse time; a wrong one is an ADR-0032 runtime concern); |
| 31 | + - **runtime token** → per-token × per-type: `NOW()` on |
| 32 | + `datetime`/`date`/`time` (both resolvers and the docs already support |
| 33 | + all three); `current_user` on `user` or `lookup` with |
| 34 | + `reference: 'sys_user'` (#4560); no token on a multi-value field |
| 35 | + (both resolve to one scalar); |
| 36 | + - **literal** → the field's own stored value contract |
| 37 | + (ADR-0104 D1 `valueSchemaFor(def, 'stored')`) — the #6970 mechanics one |
| 38 | + layer down. |
| 39 | + |
| 40 | +Rejections are prescriptive: they name the field, its type, the offending |
| 41 | +value verbatim, why it cannot hold, and the legal alternatives — including a |
| 42 | +suggested token for predictable near-miss spellings (`'now'`, |
| 43 | +`'{current_user}'`), which are suggested but never silently widened into |
| 44 | +tokens (a genuinely-intended literal must stay storable). |
| 45 | + |
| 46 | +**Migration surface: zero.** All 371 shipped `defaultValue` declarations |
| 47 | +across objectstack + cloud were re-censused against the implemented gate — |
| 48 | +0 refusals (239 literals all pass their stored contracts; 131 × `NOW()` all |
| 49 | +on `datetime`; 1 × `current_user` on `user`). The only declarations anywhere |
| 50 | +that newly refuse are two hand-written docs samples that were already wrong |
| 51 | +today (stored verbatim / dropped by the SQL DDL), fixed in this change. |
| 52 | + |
| 53 | +**Stock compatibility.** As with #6970: stored metadata carrying a |
| 54 | +nonconforming default keeps loading (the read path runs no Zod validation); |
| 55 | +authoritative spec validation lives on the WRITE path and surfaces on reads |
| 56 | +as the advisory `_diagnostics` envelope. Loud at authoring, non-fatal at |
| 57 | +rest, no conversion owed — there is no mechanical rewrite for "the author |
| 58 | +meant something else". |
| 59 | + |
| 60 | +Also moved: `AddressSchema` is now declared in `field-value.zod.ts` (it IS |
| 61 | +the enforced address value contract) and re-exported from `field.zod.ts` |
| 62 | +unchanged — the move removes the one runtime ESM edge that would otherwise |
| 63 | +have closed an evaluation cycle between the two modules. |
0 commit comments