Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 9db9ff3

Browse files
objectstack-fleet[bot]claudeos-project-manager
authored
feat(types)!: a form view's subforms[].columns entry is the spec's InlineGridColumnSchema, by reference (objectui#11266) (#11618)
Fixes #11266 Clause-②: yes (narrowing) `@objectstack/spec` 17.6.0 judges `FormViewSchema.subforms[].columns` with its strict `InlineGridColumnSchema` (objectstack-ai/objectstack#20927, `bee75cebe6`). The `object-form` mirror here still held `z.array(z.any())`, so `objectui validate` accepted columns that `os validate` refuses. This PR makes one column the spec's schema, by reference, on the zod face and on the TypeScript face. Dispatched under claim `5981407176` (`domain:spec`, size S). Session: `https://claude.ai/code/session_01CPvhwGcirXqBGEdPSb72TZ`. ## What changes - `packages/types/src/zod/objectql.zod.ts`, the `object-form` mirror's `subforms[]` item: `columns` goes from `z.array(z.any())` to `z.array(stripImportedDefaults(SpecInlineGridColumnSchema))`, imported from `@objectstack/spec/data`. It crosses the objectui#8317 import boundary like every other spec read. The column schema carries no default, so the crossing is the identity and the member IS the spec's object. No hand-copied column shape. - `packages/types/src/objectql.ts`: `ObjectFormSchema`'s `subforms[].columns` goes from `any[]` to the spec's `InlineGridColumn` (its `z.input`, the authoring face), by a type-only import. `ObjectViewSchema['form']` follows, because it picks from `ObjectFormSchema`. - Docs: `content/docs/plugins/plugin-view.mdx` and `packages/plugin-view/README.md`, section "Child records (master-detail)", wrote `columns: ['product', 'quantity', 'price']`. Bare strings: `hydrateColumns` reads `col.name`, the spec refuses them, and with the TypeScript face narrowed that snippet no longer compiles under `check:doc-snippets`. Both now write `{ name: ... }` objects and say what a column is. ⚠️ These two files are outside the claim's file surface. They are added because this change makes them false: the snippet gate would red on them. - `.changeset/11266-subforms-columns-mirror.md`: `@object-ui/types: minor`, with a BREAKING banner, FROM / TO migration lines and the `Clause-②: yes (narrowing)` line. ## Premise readings (this worktree, base `2abec3a9`, which is `origin/main` at PR time) - `pnpm-lock.yaml` resolves `@objectstack/spec` at 17.6.0 only (two entries, both 17.6.0). The package `packages/types` resolves answers `17.6.0`. - `@objectstack/spec/data` exports `InlineGridColumnSchema`. - `FormViewSchema.safeParse` of a simple form whose `subforms[0].columns` holds COL: - COL `{ name: 'qty', bogusKey: 1 }` is refused: one `unrecognized_keys` issue at `subforms.0.columns.0`, keys `bogusKey`. - COL `{ name: 'qty' }` is accepted (the control). - Control version: 17.5.0, installed from npm into a scratch directory. Its form view ACCEPTS the bogus key (the `z.any()` era), so the probe can fire. ## The `currency` + `scale` finding - A column that DECLARES `type: 'currency'` and carries `scale` (`{ name: 'amount', type: 'currency', scale: 2 }`) is refused by the spec's `InlineGridColumnSchema` itself, at the zod level: one `custom` issue at path `scale`. **It is pinned.** Through the mirror the same column is refused at `form.subforms.0.columns.0.scale`, and the pin asserts that the message equals the spec schema's own message, read in the same run. No literal text is pinned. - An identity-only column carrying `scale` (`{ name: 'amount', scale: 2 }`) is ACCEPTED by the spec's column schema, at 17.5.0 and at 17.6.0. Its refusal is `defineStack`'s, which resolves `name` through `childObject` to a `currency` field. The mirror judges a document that does not carry the child object's fields, so it cannot reproduce that check. The pin records the mirror ACCEPTING it, so reaching for it later is a deliberate change. The render-time report from objectui#11238 (`reportCurrencyColumnScale` in `plugin-form`'s `hydrateColumns`) stays the backstop. No `plugin-form` behaviour changes: patch round 1 (`b1d035bb`) corrects only its prose, see Acceptance note 2. ## The `@objectstack/spec` range `packages/types/package.json` already reads `^17.6.0` on `origin/main`: objectui#11438 (PR #11531, the 17.6.0 lockfile move) raised it. So it does not move here. Would this change alone need 17.6.0? No. The mirror reads `InlineGridColumnSchema` only, and 17.5.0 already exports it with the same verdicts on the three probes: bogus key refused with `unrecognized_keys`, typed currency with `scale` refused at `scale`, `{ name }` accepted. What 17.6.0 adds is the spec's own `FormViewSchema` reading that schema. That is the other door's verdict, not this mirror's. `check:spec-floors` is green on the built artifact. ## The pin `packages/types/src/__tests__/object-form-subforms-columns-11266.test.ts`, 17 tests: - **By reference.** The mirror's column schema is `toBe` `stripImportedDefaults(SpecInlineGridColumnSchema)`, and `toBe` the spec object itself. - **On the tolerant face** (`safeValidateSchema`, which `objectui validate` runs) **and on the strict authoring face**, through the object-view `form` slot. That slot is the route an authored document has: `subforms` is a form-VIEW member, which the `object-form` row refuses in its `properties` bag. - `{ name }` is accepted: the lit control. - A bogus key is refused: one `unrecognized_keys` issue at the column, keys `bogusKey`. - Typed currency with `scale` is refused: one `custom` issue at the column's `scale`. - A bare string is refused: `invalid_type` at the column. - Identity-only with `scale` is accepted. - **The flat mirror** gives the same codes at the same column. - **One verdict across the two doors.** For each probe column, `safeValidateSchema` succeeds exactly when the spec's `FormViewSchema` does, read live. - **Type level** (judged by `tsc -p tsconfig.test.json` inside `type-check`): a declared column `Equal`s the spec's `InlineGridColumn`, and a string column is a `@ts-expect-error`. **Ablation.** The fix was committed first. The ablation used objectstack's `scripts/ablation-replace.mjs` in wrap mode, inside `os-verify-lock`. - Mutation: anchor `columns: z.array(stripImportedDefaults(SpecInlineGridColumnSchema)).optional()` became `columns: z.array(z.any()).optional()`. Anchor count went from 1 to 0, and the blob from `27ae77d7` to `d6b4ac83`. - Result: **11 failed, 6 passed (17)**. The six that stay green are the two controls, the two identity-only rows, and the two cross-door rows whose expected verdict is accept. - Restore: the blob after restore is `27ae77d7`, the same as the blob at HEAD, and `git diff HEAD` is empty. The pin rerun: **17 passed**. ## Tests moved - `imported-defaults-8317.test.ts`: `IMPORTED` gains `['InlineGridColumnSchema', SpecInlineGridColumnSchema]`. The census "every symbol the mirrors import is covered by the differential above" requires it. The row is measured as the identity (no default, no `z.lazy`). - No `zod-mirror-parity` `KnownDrift` or `SPEC_DERIVED_PAIRS` row moves: - `ObjectFormSchema` was already spec-derived. - Its `KnownDrift` entry names only the five runtime-slot handler keys. - The TypeScript and zod faces of `columns` now carry the same type. Before, it was `any[]` against `z.any()`, so the ledger saw nothing either way. - `object-form-unmirrored-members-6152.test.ts`: the `subforms` row has no columns in either value, keeps its verdicts, and is not edited. **Repo-wide sweep.** I grepped for `subforms` and `columns` across tests, docs, JSON and scripts. No test anywhere asserts that `objectui validate` accepts a bogus column or a typed-currency column with `scale`, so no pin flips. The two doc examples above were the only documents writing a column the narrowed faces refuse. ## Gate readings, at head `ce55b465` - `pnpm exec vitest run packages/types/` (under the lock): `Test Files 355 passed (355)`, `Tests 9492 passed (9492)`. - `packages/types` `type-check` (the build program, the examples program and the test program): exit 0. - The import side of the narrowed TypeScript face: `type-check` for `@object-ui/plugin-form`, `@object-ui/plugin-view` and `@object-ui/app-shell` exits 0, and each echoes `type-check: Done`. They ran against the closure that `check:doc-snippets --build-filter` names, built with pnpm, not turbo. - `vitest run packages/cli/` plus the `subforms`-adjacent consumer tests (`deriveMasterDetail.currencyScale-10783`, `subformHosts`, `MetadataProvider.merge`): 28 files, 390 tests, all passed. - `eslint --no-inline-config --format json` over the four touched TypeScript files: 4 files, 0 errors. The warnings are existing `no-explicit-any` hits in `objectql.ts` and `objectql.zod.ts`; the pin file has 0. - This is a narrowing of `pnpm lint`, and it is declared. Population: `eslint.config.js` lints `**/*.{ts,tsx}`, so the two edited `.md` / `.mdx` files are outside it. File count: 4, from the JSON output. Invariance: the config sets no `parserOptions.project` or `projectService`, so type-aware linting is off, and this diff cannot move the verdict on any untouched file. The full run belongs to CI. - `check:doc-snippets`: 777 of 777 blocks judged, 0 failed. - Exit 0 for each of: `check:doc-examples`, `check:doc-types`, `check:doc-fences`, `check:doc-example-ids`, `check:doc-example-readers`, `docs:check-links`. - `check:new-line-citations`: 0 new. `check:control-bytes`: OK. - Changeset checkers, exit 0 for each: `check:changeset-claims`, `check:pending-changeset-literals`, `changeset:check` (fixed and no-major), `check-changeset-presence` and `check-changeset-overwrite`. - Exit 0 for each of: `check:spec-symbols`, `check:spec-floors`, `check:installed-pin-claims`, `check:phantom-deps`, `check:unused-deps`, `check:readme-exports`, `check:test-path-roots`, `check:component-surface-parity`. - `check:spec-floors` was first refused with `no-artifact` on the unbuilt `@object-ui/plugin-tree`. I built that package and reran it. - `check-governed-queue-guard --test` over the seven paths: NOT GOVERNED. - **NOT MEASURED locally, left to CI:** the full `pnpm test`, the tree-wide `pnpm lint`, and `check:published-dist`. - **Patch round 1, at head `b1d035bb`:** `@object-ui/plugin-form` `type-check` passes, with its dependency closure built by pnpm, not turbo. `vitest run` on `deriveMasterDetail.currencyScale-10783.test.ts` passes, the renamed test among them. eslint over the two touched files reports no errors; its warnings are existing `no-explicit-any` in code. `check:new-line-citations` reports no new citation, `check:control-bytes` passes, and the changeset checkers exit 0. The readings above are at `ce55b465`; this round touched only the two `plugin-form` files. ## Acceptance notes 1. **The parse output.** `InlineGridColumnSchema`'s `readonlyWhen` and `requiredWhen` carry the spec's ExpressionInput pipe, so `safeValidateSchema` returns a column's string predicate as `{ dialect: 'cel', source }`. Measured: `readonlyWhen: 'record.locked'` comes back as the envelope. The input document is not changed. - This is the spec schema's own output, taken by reference. - Four other imported crossings already return transformed values. I counted them by walking `AnyComponentSchema`: a page's `slots.header` and its `visibleWhen`, `listViews` `exportOptions`, and an action `params` entry. - objectui#8347's Q6 = B ("a string stays a string") was ruled for `BaseSchema.visibleWhen`. Nothing extends it to imported subtrees. - The changeset states it. Flagged for the contract review. 2. **Stale prose, fixed in patch round 1 (`b1d035bb`).** Seat ruling: a PR fixes the sentences it makes false. - `reportCurrencyColumnScale`'s docblock in `packages/plugin-form/src/deriveMasterDetail.ts`, the `deriveMasterDetail.currencyScale-10783` test header and one of its test names said a subform's `columns` is `z.array(z.any())` in the `object-form` mirror and in the spec's `FormViewSchema`, and that the spec does not judge that path. - They now say both validators judge a declared column with `InlineGridColumnSchema`. They keep the report's reason: neither zod face sees an identity-only `{ name, scale }` column whose child field is a currency (only `defineStack` does, at publish, objectstack-ai/objectstack#20927), and nothing runs either validator between a stored or code-built form view and the render. - Comment and test-name bytes only. The TypeScript printer with `removeComments` prints `deriveMasterDetail.ts` identically before and after, and the test file differs only in that one test-name string. The old test name was referenced nowhere else in the tree. - Left as written: the released `@object-ui/plugin-form` and `@object-ui/fields` CHANGELOG text that says the same, which is history. 3. **Looser local copies.** `DrawerForm.tsx` and `ModalForm.tsx` in `plugin-form` declare their own `subforms` item types with `columns` as `any[]`. They receive values from the now-typed face, so nothing breaks, but they are looser than the spec. Carrier: none. Serial: objectui#11608's PR #11616 is still open. Its files (`packages/types/src/index.ts` and the objectui#8347 pin) are disjoint from these. `origin/main` has not moved since this branch was cut (`2abec3a9`), so the merge before opening was a no-op. --- _Generated by [Claude Code](https://claude.ai/code/session_01CPvhwGcirXqBGEdPSb72TZ)_ --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Leehom <pm@objectstack.ai>
1 parent 7c9a6b1 commit 9db9ff3

9 files changed

Lines changed: 297 additions & 18 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@object-ui/types': minor
3+
---
4+
5+
A form view's `subforms[].columns` entry is judged by `@objectstack/spec`'s `InlineGridColumnSchema` now, by reference, so `objectui validate` and `os validate` give one verdict on a column (objectui#11266).
6+
7+
BREAKING (`@object-ui/types`): the accept set of the tolerant face narrows. (The bump is `minor` by this repo's release model: objectui's major follows the `@objectstack` family major, and its own breaking changes ship as `minor` with the breaking semantics stated here.)
8+
9+
`@objectstack/spec` 17.6.0 holds `FormViewSchema.subforms[].columns` to its closed inline grid column schema (objectstack-ai/objectstack#20927). The `object-form` mirror still read `z.array(z.any())` there, so `objectui validate` accepted columns that `os validate` refuses.
10+
11+
What each face does now:
12+
13+
- **zod (`@object-ui/types/zod`).** NARROWS on the tolerant face (`safeValidateSchema`, which `objectui validate` runs) and on the strict authoring face, wherever `subforms` is read: the object-view `form` slot and the `object-form` mirror. A column with an undeclared key is refused at the column, with one `unrecognized_keys` issue naming the key. A column that declares `type: 'currency'` and carries `scale` is refused at that `scale`, in the spec's own words. A bare field-name string is refused at the column with `invalid_type`. Each verdict is the spec schema's, because the column is handed to it.
14+
- **TypeScript.** A `subforms[].columns` entry on `ObjectFormSchema` (and so on `ObjectViewSchema['form']`) is `InlineGridColumn` from `@objectstack/spec/data`, by reference, where it was `any`. A string column no longer compiles, and neither does an object literal with an undeclared column key.
15+
16+
**Migration.**
17+
18+
- FROM `columns: ['product', 'quantity']` → TO `columns: [{ name: 'product' }, { name: 'quantity' }]`
19+
- FROM a column carrying a key `InlineGridColumn` does not declare → TO the same column without that key. A column that declares no `type` takes its label, type and the rest from the child object's field.
20+
- FROM `{ name: 'amount', type: 'currency', scale: 2 }` → TO `{ name: 'amount', type: 'currency' }`. A currency amount's decimal places come from its currency's minor unit, not from the column.
21+
22+
Not refused here: a `scale` on a column that declares no `type` (`{ name: 'amount', scale: 2 }`) when its child field is a currency. Seeing that takes the child object's fields, which are not in the document the validator judges. `defineStack` refuses it at publish, and the master-detail form reports it at render.
23+
24+
The parse output is the spec schema's too: a column `readonlyWhen` or `requiredWhen` written as a string comes back from `safeValidateSchema` as the spec's `{ dialect: 'cel', source }` envelope. The document you pass in is not changed.
25+
26+
**Clause-②: yes (narrowing)**: a `subforms[].columns` entry that is not a valid `InlineGridColumn` used to parse with its value kept, and is now refused at the column.

‎content/docs/plugins/plugin-view.mdx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ const orderView: ObjectViewSchema = {
596596
{
597597
childObject: 'order_items',
598598
title: 'Line items',
599-
columns: ['product', 'quantity', 'price'],
599+
columns: [{ name: 'product' }, { name: 'quantity' }, { name: 'price' }],
600600
},
601601
],
602602
},
@@ -605,7 +605,10 @@ const orderView: ObjectViewSchema = {
605605

606606
Only `childObject` is required — the relationship field and the grid columns are
607607
derived from the child object's metadata unless you override them
608-
(`relationshipField`, `columns`).
608+
(`relationshipField`, `columns`). A column is `@objectstack/spec`'s
609+
`InlineGridColumn`: an object keyed by `name`, never a bare field name. One that
610+
declares no `type` takes its label, type and the rest from the child field;
611+
an undeclared column key is refused by the validator.
609612

610613
### View tabs
611614

‎packages/plugin-form/src/deriveMasterDetail.currencyScale-10783.test.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
* `@objectstack/spec` 17.5.0 refuses `scale` on an inline grid column that
1515
* declares `type: 'currency'`. A column that declares no `type`
1616
* (`{ name: 'amount', scale: 2 }`) still parses: `hydrateColumns` fills its
17-
* `type` from the child field at render time, which the spec cannot see, and
18-
* `objectui validate` cannot either (a subform's `columns` is
19-
* `z.array(z.any())`, and the child object's fields are not in the document).
17+
* `type` from the child field at render time. The spec's column schema cannot
18+
* see that, and `objectui validate` cannot either: it judges a subform's
19+
* `columns` with that same schema (objectui#11266), and the child object's
20+
* fields are not in the document. Only `defineStack` refuses it, at publish, by
21+
* resolving `name` against the child object the stack declares
22+
* (objectstack-ai/objectstack#20927); nothing in objectui does.
2023
* `GridField`'s `currencyWidth` no longer reads a currency column's `scale`, so
2124
* without a report here the key would be accepted everywhere and read by
2225
* nothing.
@@ -69,7 +72,7 @@ describe('hydrateColumns reports a `scale` on a column that hydrates to currency
6972
expect(computeRow(cols, { quantity: 3, unit_price: 1.2345 }, 'KWD').amount).toBe(3.704);
7073
});
7174

72-
it('a declared currency column carrying `scale` is reported too (the form-view `subforms[].columns` path the spec does not judge)', () => {
75+
it('a declared currency column carrying `scale` is reported too (a stored or code-built form view reaches the render without passing either validator)', () => {
7376
const warnings = spyWarn();
7477
hydrateColumns([{ name: 'amount', type: 'currency', computed: true, expr: 'quantity * unit_price', scale: 2 }], lineSchema('line_declared'));
7578
const reports = warnings().filter((m) => m.includes('`scale`'));

‎packages/plugin-form/src/deriveMasterDetail.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,22 @@ export function deriveColumns(
296296
* docblock says the reach stops there: an identity-only entry
297297
* (`{ name: 'amount', scale: 2 }`) takes its type from the child field at
298298
* render time, here, so it still parses. `objectui validate` cannot see it
299-
* either: a subform's `columns` is `z.array(z.any())` in the `object-form`
300-
* mirror, and the child object's fields are not in the document it judges.
301-
* This is the first place the child field is known, so the report is made
302-
* here — ⛔ never a silent drop.
299+
* either: the `object-form` mirror judges a subform's `columns` with the spec's
300+
* own `InlineGridColumnSchema` (objectui#11266), and the child object's fields
301+
* are not in the document it judges. Only `defineStack` refuses it, at publish, by resolving
302+
* `name` against the child object's fields: on a relationship field's
303+
* `inlineColumns` and on a form view's `subforms[].columns`, and only when the
304+
* stack declares that child object (objectstack-ai/objectstack#20927). Nothing
305+
* in objectui does that, and this is the first place in objectui the child
306+
* field is known, so the report is made here — ⛔ never a silent drop.
303307
*
304-
* The declared arm is reported too. A form view's `subforms[].columns` is
305-
* `z.array(z.any())` in the spec's `FormViewSchema`, so a typed currency column
306-
* carrying `scale` reaches this function unjudged on that path.
308+
* The declared arm is reported too. On a form view's `subforms[].columns` both
309+
* validators refuse a typed currency column carrying `scale`: the spec's
310+
* `FormViewSchema` holds each column to `InlineGridColumnSchema` from 17.6.0,
311+
* and the `object-form` mirror takes that schema by reference (objectui#11266).
312+
* Neither runs between a stored or code-built form view and this function, so
313+
* such a column can still arrive here, and it is reported rather than read in
314+
* silence.
307315
*
308316
* Once per column per page load (the `sectionFields.ts` convention): this runs
309317
* on every child-schema resolve. The first sentence is the spec's refusal with

‎packages/plugin-view/README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ const schema: ObjectViewSchema = {
697697
{
698698
childObject: 'order_items',
699699
title: 'Line items',
700-
columns: ['product', 'quantity', 'price'],
700+
columns: [{ name: 'product' }, { name: 'quantity' }, { name: 'price' }],
701701
},
702702
],
703703
},
@@ -706,7 +706,10 @@ const schema: ObjectViewSchema = {
706706

707707
Only `childObject` is required — the relationship field and the grid columns are
708708
derived from the child object's metadata unless you override them
709-
(`relationshipField`, `columns`).
709+
(`relationshipField`, `columns`). A column is `@objectstack/spec`'s
710+
`InlineGridColumn`: an object keyed by `name`, never a bare field name. One that
711+
declares no `type` takes its label, type and the rest from the child field;
712+
an undeclared column key is refused by the validator.
710713

711714
### View tabs
712715

‎packages/types/src/__tests__/imported-defaults-8317.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ import {
137137
checkDashboardWidgetStageOrder,
138138
checkDashboardWidgetMetricMeasureArity,
139139
} from '@objectstack/spec/ui';
140-
import { FieldSchema as SpecFieldSchema, SelectOptionSchema as SpecSelectOptionSchema } from '@objectstack/spec/data';
140+
import {
141+
FieldSchema as SpecFieldSchema,
142+
SelectOptionSchema as SpecSelectOptionSchema,
143+
// objectui#11266 — one `ObjectFormSchema.subforms[].columns` entry.
144+
InlineGridColumnSchema as SpecInlineGridColumnSchema,
145+
} from '@objectstack/spec/data';
141146
import {
142147
EvaluatedExpressionInputSchema as SpecEvaluatedExpressionInputSchema,
143148
EvaluatedExpressionSchema as SpecEvaluatedExpressionSchema,
@@ -372,6 +377,11 @@ const IMPORTED: Array<readonly [string, z.ZodType]> = [
372377
// spec's `.default(false)` —
373378
// exactly what this boundary exists to keep out of a parse output.
374379
['FieldSchema', SpecFieldSchema],
380+
// objectui#11266: one `ObjectFormSchema.subforms[].columns` entry is the spec's
381+
// inline grid column, crossed through this boundary. It carries no default and
382+
// reaches no `z.lazy`, so the strip is the identity function: the row is here
383+
// because the census below requires every imported symbol to be measured.
384+
['InlineGridColumnSchema', SpecInlineGridColumnSchema],
375385
] as const;
376386

377387
/** The subset that actually carries an imported default — where the strip does work. */
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectui#11266 — one `ObjectFormSchema.subforms[].columns` entry is the spec's
11+
* `InlineGridColumnSchema`, by reference.
12+
*
13+
* `@objectstack/spec` 17.6.0 judges `FormViewSchema.subforms[].columns` with its
14+
* closed inline grid column schema (objectstack#20927). The mirror held
15+
* `z.array(z.any())` there, so `objectui validate` accepted a column with an
16+
* undeclared key, and a typed `currency` column carrying `scale`, both of which
17+
* `os validate` refuses. Two authoring doors gave two verdicts on one document.
18+
*
19+
* An authored document reaches `subforms` through the object-view `form` slot:
20+
* `subforms` is a form-VIEW member, which the `object-form` row refuses in its
21+
* `properties` bag (objectui#10859 batch 4), and the slot is this mirror minus
22+
* its identity keys. So the face rows below author it there. The mirror rows
23+
* parse the flat node as `ObjectForm` reads it.
24+
*
25+
* ## What is pinned
26+
*
27+
* - BY REFERENCE: a column's schema IS the spec schema as it crosses the
28+
* import boundary, so a faithful hand copy turns this red. The TypeScript
29+
* face IS the spec's `InlineGridColumn` (judged by `tsc -p tsconfig.test.json`).
30+
* - THE VERDICTS, on the tolerant face (`safeValidateSchema`, which
31+
* `objectui validate` runs) and on the strict authoring face. An undeclared
32+
* column key is refused at the column, with the key named. A `scale` on a
33+
* column that DECLARES `type: 'currency'` is refused at that `scale`. A bare
34+
* field-name string is refused at the column. A column `{ name }` is
35+
* accepted: the lit control.
36+
* - ONE VERDICT ACROSS THE TWO DOORS: each probe column gets the verdict the
37+
* spec's own `FormViewSchema` gives it, read live in the same run.
38+
*
39+
* ⛔ Not refused here, and pinned as ACCEPTED so that reaching for it is a
40+
* deliberate change: a `scale` on an identity-only column (`{ name, scale }`)
41+
* whose child field is a currency. Refusing it takes the child object's fields,
42+
* which are not in the document this mirror judges. The spec's column schema
43+
* accepts it too; `defineStack` refuses it at publish, by resolving `name`
44+
* through `childObject`, and `plugin-form`'s `hydrateColumns` reports it at
45+
* render (objectui#10783).
46+
*/
47+
48+
import { describe, it, expect } from 'vitest';
49+
import {
50+
InlineGridColumnSchema as SpecInlineGridColumnSchema,
51+
type InlineGridColumn as SpecInlineGridColumn,
52+
} from '@objectstack/spec/data';
53+
import { FormViewSchema as SpecFormViewSchema } from '@objectstack/spec/ui';
54+
import type { ObjectFormSchema as DeclaredObjectFormSchema } from '../objectql';
55+
import { ObjectFormSchema as ObjectFormMirror } from '../zod/objectql.zod.js';
56+
import { safeValidateSchema, StrictAnyComponentSchema } from '../zod/index.zod.js';
57+
import { stripImportedDefaults } from '../zod/imported-defaults.js';
58+
59+
/* ── Type level ───────────────────────────────────────────────────────────── */
60+
61+
type Equal<A, B> =
62+
(<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
63+
type Expect<T extends true> = T;
64+
65+
type DeclaredSubform = NonNullable<DeclaredObjectFormSchema['subforms']>[number];
66+
type DeclaredColumn = NonNullable<DeclaredSubform['columns']>[number];
67+
68+
// By reference: a declared column IS the spec's authoring type. A local
69+
// restatement, or the `any` this replaced, turns this red.
70+
export type _ColumnIsTheSpecInput = Expect<Equal<DeclaredColumn, SpecInlineGridColumn>>;
71+
72+
export const identityOnlyColumnIsAuthorable: DeclaredSubform = {
73+
childObject: 'order_line',
74+
columns: [{ name: 'quantity' }, { name: 'amount', type: 'currency', label: 'Amount' }],
75+
};
76+
export const stringColumnIsRefused: DeclaredSubform = {
77+
childObject: 'order_line',
78+
// @ts-expect-error a column is an object keyed by `name`, never a bare field-name string
79+
columns: ['quantity'],
80+
};
81+
82+
/* ── Runtime ──────────────────────────────────────────────────────────────── */
83+
84+
const CHILD = 'order_line';
85+
86+
/** An authored object-view whose form declares one subform with one column. */
87+
const viewWith = (column: unknown) => ({
88+
type: 'object-view',
89+
objectName: 'order',
90+
form: { subforms: [{ childObject: CHILD, columns: [column] }] },
91+
});
92+
/** The flat `object-form` node as `ObjectForm` reads it, with the same subform. */
93+
const flatWith = (column: unknown) => ({
94+
type: 'object-form',
95+
objectName: 'order',
96+
mode: 'create',
97+
subforms: [{ childObject: CHILD, columns: [column] }],
98+
});
99+
/** The spec's form view with the same subform: the other door's verdict. */
100+
const specFormViewWith = (column: unknown) => ({
101+
type: 'simple',
102+
subforms: [{ childObject: CHILD, columns: [column] }],
103+
});
104+
105+
const VALID = { name: 'quantity' };
106+
const BOGUS_KEY = { name: 'quantity', bogusKey: 1 };
107+
const TYPED_CURRENCY_WITH_SCALE = { name: 'amount', type: 'currency', scale: 2 };
108+
const IDENTITY_ONLY_WITH_SCALE = { name: 'amount', scale: 2 };
109+
const BARE_STRING = 'quantity';
110+
111+
/** The column's own position on the object-view face. */
112+
const VIEW_COLUMN = ['form', 'subforms', 0, 'columns', 0];
113+
/** …and on the flat mirror. */
114+
const FLAT_COLUMN = ['subforms', 0, 'columns', 0];
115+
116+
type Face = { name: string; parse: (doc: unknown) => ReturnType<typeof safeValidateSchema> };
117+
const FACES: ReadonlyArray<Face> = [
118+
{ name: 'tolerant (`safeValidateSchema`, which `objectui validate` runs)', parse: (doc) => safeValidateSchema(doc) },
119+
{ name: 'strict authoring face', parse: (doc) => StrictAnyComponentSchema.safeParse(doc) as ReturnType<typeof safeValidateSchema> },
120+
];
121+
122+
/** One column's schema on the mirror, unwrapped from `subforms`' and `columns`' `optional` / `array`. */
123+
const mirrorColumnSchema = () => ObjectFormMirror.shape.subforms.unwrap().element.shape.columns.unwrap().element;
124+
125+
describe('objectui#11266 — `subforms[].columns` is the spec\'s `InlineGridColumnSchema`, by reference', () => {
126+
it('a column\'s schema IS the spec schema as it crosses the import boundary', () => {
127+
expect(mirrorColumnSchema()).toBe(stripImportedDefaults(SpecInlineGridColumnSchema));
128+
// The crossing is the identity here (the column schema carries no default),
129+
// so the member is the spec's own object.
130+
expect(mirrorColumnSchema()).toBe(SpecInlineGridColumnSchema);
131+
});
132+
133+
describe.each(FACES)('on the $name', ({ parse }) => {
134+
it('CONTROL: a column `{ name }` is accepted', () => {
135+
const parsed = parse(viewWith(VALID));
136+
expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true);
137+
});
138+
139+
it('an undeclared column key is refused at the column, with the key named', () => {
140+
const parsed = parse(viewWith(BOGUS_KEY));
141+
expect(parsed.success).toBe(false);
142+
expect(parsed.error!.issues).toHaveLength(1);
143+
const [issue] = parsed.error!.issues;
144+
expect(issue.code).toBe('unrecognized_keys');
145+
expect(issue.path).toEqual(VIEW_COLUMN);
146+
expect((issue as { keys?: string[] }).keys).toEqual(['bogusKey']);
147+
});
148+
149+
it('a `scale` on a column that declares `type: \'currency\'` is refused at that `scale`', () => {
150+
const parsed = parse(viewWith(TYPED_CURRENCY_WITH_SCALE));
151+
expect(parsed.success).toBe(false);
152+
expect(parsed.error!.issues).toHaveLength(1);
153+
const [issue] = parsed.error!.issues;
154+
expect(issue.code).toBe('custom');
155+
expect(issue.path).toEqual([...VIEW_COLUMN, 'scale']);
156+
// The refusal is the spec's own, sentence included: the same column
157+
// judged by the spec's schema in this run yields the same message.
158+
const spec = SpecInlineGridColumnSchema.safeParse(TYPED_CURRENCY_WITH_SCALE);
159+
expect(spec.success).toBe(false);
160+
expect(issue.message).toBe(spec.error!.issues[0].message);
161+
});
162+
163+
it('a bare field-name string is refused at the column', () => {
164+
const parsed = parse(viewWith(BARE_STRING));
165+
expect(parsed.success).toBe(false);
166+
expect(parsed.error!.issues).toHaveLength(1);
167+
const [issue] = parsed.error!.issues;
168+
expect(issue.code).toBe('invalid_type');
169+
expect(issue.path).toEqual(VIEW_COLUMN);
170+
});
171+
172+
it('⛔ an identity-only column with `scale` is accepted: the currency is the child field\'s, which this document does not carry', () => {
173+
const parsed = parse(viewWith(IDENTITY_ONLY_WITH_SCALE));
174+
expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true);
175+
});
176+
});
177+
178+
it('the flat mirror gives the same verdicts at the same column', () => {
179+
expect(ObjectFormMirror.safeParse(flatWith(VALID)).success).toBe(true);
180+
181+
const bogus = ObjectFormMirror.safeParse(flatWith(BOGUS_KEY));
182+
expect(bogus.success).toBe(false);
183+
expect(bogus.error!.issues.map((i) => [i.code, i.path])).toEqual([['unrecognized_keys', FLAT_COLUMN]]);
184+
185+
const currency = ObjectFormMirror.safeParse(flatWith(TYPED_CURRENCY_WITH_SCALE));
186+
expect(currency.success).toBe(false);
187+
expect(currency.error!.issues.map((i) => [i.code, i.path])).toEqual([['custom', [...FLAT_COLUMN, 'scale']]]);
188+
});
189+
190+
it.each([
191+
['a column `{ name }`', VALID, true],
192+
['an undeclared column key', BOGUS_KEY, false],
193+
['a typed currency column with `scale`', TYPED_CURRENCY_WITH_SCALE, false],
194+
['an identity-only column with `scale`', IDENTITY_ONLY_WITH_SCALE, true],
195+
['a bare field-name string', BARE_STRING, false],
196+
] as const)('one verdict across the two doors: %s', (_label, column, expected) => {
197+
// The other door, read live: the spec's form view judging the same column.
198+
const spec = SpecFormViewSchema.safeParse(specFormViewWith(column));
199+
expect(spec.success).toBe(expected);
200+
expect(safeValidateSchema(viewWith(column)).success).toBe(spec.success);
201+
});
202+
});

0 commit comments

Comments
 (0)