diff --git a/.changeset/39036-page-related-list-columns-mix-error.md b/.changeset/39036-page-related-list-columns-mix-error.md new file mode 100644 index 00000000000..3ef6c321883 --- /dev/null +++ b/.changeset/39036-page-related-list-columns-mix-error.md @@ -0,0 +1,10 @@ +--- +'@objectstack/spec': patch +--- + +fix(spec): a list page's or a related list's `columns` array that mixes field names and column objects is refused with a sentence that names the mix + +Clause-②: no +Metadata-protocol: none + +A list page's own `interfaceConfig.columns` and a `record:related_list` element's `columns` take one spelling per list, as a list view's `columns` does: every entry a field name (`['name', 'amount']`), or every entry a column object (`[{ field: 'name' }, { field: 'amount', width: 120 }]`). A mixed array such as `['name', { field: 'amount' }]` was already refused, and still is, but the refusal read only `Invalid input`, with a branch detail such as `columns.0: expected string, received object` that sounds as if column objects were not allowed. Both now answer with the list view's sentence: the array mixes the two spellings, and the fix is to write the plain entries as objects too (`'name'` becomes `{ field: 'name' }`). Every array that parsed before still parses, and every other `columns` refusal reads as before. diff --git a/packages/metadata-protocol/src/protocol.save-union-issues.test.ts b/packages/metadata-protocol/src/protocol.save-union-issues.test.ts index f227c0749be..3d97c6cb6e5 100644 --- a/packages/metadata-protocol/src/protocol.save-union-issues.test.ts +++ b/packages/metadata-protocol/src/protocol.save-union-issues.test.ts @@ -567,6 +567,30 @@ describe('#35400 an explained union head is dropped only when it says nothing of expect(err.issues.some((i: any) => i.path === 'confirmText.dialect')).toBe(true); }); + it('a list view `columns` mixing names and objects keeps its head: the mix sentence leads, ahead of the branch entries', async () => { + // The spec's `columns` union carries its own `error` for a mix + // (`list-columns-mix.ts`); this walker keeps such a head by the same + // rule `zodIssuesToFields` follows, so the save door's 422 opens on the + // sentence and not on `columns.0: expected string, received object`. + const { protocol, rows } = makeProtocol(); + const body = { name: 'task_list', object: 'task', viewKind: 'list', type: 'grid', columns: ['title', { field: 'status' }] }; + + const err = await rejection(save(protocol, body)); + + expect(err.code).toBe('INVALID_METADATA'); + expect(err.status).toBe(422); + expect(rows.size).toBe(0); + const head = err.issues.findIndex(isHeadAt('columns')); + expect(head, JSON.stringify(err.issues)).toBeGreaterThanOrEqual(0); + expect(err.issues[head].message).not.toBe(ZOD_UNION_DEFAULT_MESSAGE); + expect(err.issues[head].message).toContain('`columns` mixes field-name strings and column objects'); + // …first among the `columns` entries: nothing located below it precedes it. + expect(head).toBe(err.issues.findIndex((i: any) => i.path === 'columns' || i.path.startsWith('columns.'))); + // CONTROL — the same view in one spelling saves. + await save(protocol, { ...body, columns: ['title', 'status'] }); + expect(rows.size).toBe(1); + }); + it('CONTROL — a bare head nothing below explains stays: `label: 42` on a view', async () => { const { protocol } = makeProtocol(); const body = { name: 'task_list', object: 'task', viewKind: 'list', type: 'grid', columns: [{ field: 'title' }], label: 42 }; diff --git a/packages/spec/src/ui/component.zod.ts b/packages/spec/src/ui/component.zod.ts index 9ed1e4b3a80..94c95c11198 100644 --- a/packages/spec/src/ui/component.zod.ts +++ b/packages/spec/src/ui/component.zod.ts @@ -76,6 +76,7 @@ import { ACTION_TARGET_ALIASES } from './action-target-aliases'; import { ON_SUCCESS_EXECUTORS, onSuccessBesideExecutorRefusal } from './action-on-success-executor'; import { refuseDeclarativeUpdateContradictions } from './action-declarative-update'; import { navigationTargetRefusal } from './navigation-target'; +import { listViewColumnsUnionError } from './list-columns-mix'; import { ACTION_CONFIRM_TEXT_TEMPLATE_DESCRIBE, ACTION_ERROR_MESSAGE_TEMPLATE_DESCRIBE, @@ -1760,7 +1761,9 @@ export const RecordRelatedListProps = strictObject({ * * ⛔ The two arms are EXCLUSIVE, and the `describe()` below says so because * the schema enforces it: `['name', { field: 'amount' }]` matches neither - * `z.array(z.string())` nor `z.array(ListColumnSchema)` and is refused. + * `z.array(z.string())` nor `z.array(ListColumnSchema)` and is refused, with + * the list view's own sentence naming the mix and the repair + * (`list-columns-mix.ts`). * * ⛔ The sibling `field.relatedListColumns` (`field.zod.ts`) is NOT widened * with it — that key is child field-name STRINGS only (#9227), and the @@ -1770,7 +1773,7 @@ export const RecordRelatedListProps = strictObject({ columns: z.union([ z.array(z.string()), // field names z.array(ListColumnSchema), // the saved view's own per-column decoration - ]).optional().describe('Fields to display in the related list — either plain field-name strings, or the same per-column entries a saved list view declares (`ListColumn`: `field`, plus `label`, `width`, `align`, `hidden`, `sortable`, `summary`, …). A view-supplied list may arrive in the `ListColumn` spelling: objectui composes a saved view\'s `columns` onto this block verbatim, and this key declares the SAME union as `listViews[].columns`. One spelling per list — the two arms are exclusive, so an array mixing strings and column objects is refused. Optional: when omitted, columns derive from the related object\'s highlightFields / default list columns (a related list is just another surface that lists that object). Override chain: child highlightFields → field-level relatedListColumns (field-name strings only) → this inline list.'), + ], { error: listViewColumnsUnionError }).optional().describe('Fields to display in the related list — either plain field-name strings, or the same per-column entries a saved list view declares (`ListColumn`: `field`, plus `label`, `width`, `align`, `hidden`, `sortable`, `summary`, …). A view-supplied list may arrive in the `ListColumn` spelling: objectui composes a saved view\'s `columns` onto this block verbatim, and this key declares the SAME union as `listViews[].columns`. One spelling per list — the two arms are exclusive, so an array mixing strings and column objects is refused. Optional: when omitted, columns derive from the related object\'s highlightFields / default list columns (a related list is just another surface that lists that object). Override chain: child highlightFields → field-level relatedListColumns (field-name strings only) → this inline list.'), sort: z.union([ z.string(), z.array(strictObject({ diff --git a/packages/spec/src/ui/list-columns-mix.ts b/packages/spec/src/ui/list-columns-mix.ts new file mode 100644 index 00000000000..de8ca5ce7e8 --- /dev/null +++ b/packages/spec/src/ui/list-columns-mix.ts @@ -0,0 +1,48 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +// The refusal for a `columns` array that mixes the two spellings, shared by the +// three declarations of one key: the list view's `columns` (`view.zod.ts`), the +// list page's own `interfaceConfig.columns` (`page.zod.ts`) and the +// `record:related_list` `columns` prop (`component.zod.ts`). All three declare +// the same exclusive `z.union([z.array(z.string()), z.array(ListColumnSchema)])`. +// +// Reached by relative import only, and deliberately NOT re-exported from +// `ui/index.ts` (which `export *`s the three zod files): sharing one sentence +// between three declarations is no reason to publish it. +// +// Declared with `//` on purpose — the `LIST_VIEW_EXPORT_PDF_RETIRED` placement +// note applies: build-docs takes a file's first JSDoc per exported symbol, and +// this module-local constant needs no doc page. +// +// The two arms of `columns` are EXCLUSIVE and stay so: objectui's grid, its +// column spelling diagnostics and the Studio column inspectors all read the +// whole array in the spelling of its FIRST entry, so a mixed array accepted +// here would have its other-spelling entries dropped or misread downstream (a +// list page hands its own `columns` to that same grid). What was wrong was the +// refusal: zod folds a failed union into a bare `Invalid input`, and the wire +// mapper then surfaced one branch's `columns.0: expected string, received +// object` — a sentence that reads as if column objects were illegal, which +// costs an AI author a wasted round. With this message the union's own head +// says what is wrong and how to repair it, and the wire mappers keep a head +// that carries the schema's own sentence. +const LIST_VIEW_COLUMNS_MIXED = + '`columns` mixes field-name strings and column objects, and one list takes one spelling: ' + + "either every entry is a field name (`['name', 'amount']`) or every entry is a column object " + + "(`[{ field: 'name' }, { field: 'amount', width: 120 }]`). To configure some columns, write " + + "the plain ones as objects too: `'name'` becomes `{ field: 'name' }`."; + +/** + * The `columns` union's own error: {@link LIST_VIEW_COLUMNS_MIXED} when the + * authored array holds at least one string AND at least one object entry, + * otherwise `undefined`, so every other failure keeps zod's default message + * with the branch detail nested beneath it (the same contract as + * `exportOptionsPdfUnionError`). It names a mix only; it never changes which + * arrays parse. + */ +export const listViewColumnsUnionError = (issue: { input?: unknown }): string | undefined => { + const input = issue.input; + if (!Array.isArray(input)) return undefined; + const hasName = input.some((entry) => typeof entry === 'string'); + const hasObject = input.some((entry) => typeof entry === 'object' && entry !== null && !Array.isArray(entry)); + return hasName && hasObject ? LIST_VIEW_COLUMNS_MIXED : undefined; +}; diff --git a/packages/spec/src/ui/page.zod.ts b/packages/spec/src/ui/page.zod.ts index f3959538b2f..3dfa19d3685 100644 --- a/packages/spec/src/ui/page.zod.ts +++ b/packages/spec/src/ui/page.zod.ts @@ -21,6 +21,7 @@ import { import { lazySchema } from '../shared/lazy-schema'; import { strictObject } from '../shared/strict-object'; import { ruleArrayFilterError } from './filter-rule-array'; +import { listViewColumnsUnionError } from './list-columns-mix'; import { MetadataProtectionFields } from '../kernel/metadata-protection.zod'; import { OverrideDeclarationFields } from '../kernel/override-declaration.zod'; import { PAGE_TYPE_DEFAULT, misplacedDefaultPageRefusal } from './default-record-page'; @@ -977,7 +978,8 @@ export const InterfacePageConfigSchema = lazySchema(() => strictObject({ // ADR-0047 (revised): the page carries its OWN view metadata — columns, sort // and base filter are defined directly here (Airtable parity: there is no // "inherit from a named view" concept). The page IS the view definition. - columns: z.union([z.array(z.string()), z.array(ListColumnSchema)]).optional() + // The same exclusive union as the list view's `columns`, with the same refusal for a mix. + columns: z.union([z.array(z.string()), z.array(ListColumnSchema)], { error: listViewColumnsUnionError }).optional() .describe('Columns shown by the page. Blank = all object fields. Defined directly on the page (no view inheritance).'), sort: z.array(SortItemSchema).optional() .describe('Default sort order for the page, defined directly on the page.'), diff --git a/packages/spec/src/ui/view-columns-mix.test.ts b/packages/spec/src/ui/view-columns-mix.test.ts index 5047827bfc3..66199a6fe2e 100644 --- a/packages/spec/src/ui/view-columns-mix.test.ts +++ b/packages/spec/src/ui/view-columns-mix.test.ts @@ -24,11 +24,16 @@ * 3. The stored-view write door (`ViewMetadataSchema`, path `config.columns`) * and the wire mapper both carry that sentence first. * 4. Cost direction: a failure that is NOT a mix keeps zod's default message. + * 5. The two sibling declarations of the same union — a list page's own + * `interfaceConfig.columns` and the `record:related_list` `columns` prop — + * answer a mix with the same sentence, each pinned on its own union. */ import { describe, expect, it } from 'vitest'; import { zodIssuesToFields } from '../api/zod-issues-to-fields'; import { ListViewSchema, ViewMetadataSchema } from './view.zod'; +import { InterfacePageConfigSchema, PageSchema } from './page.zod'; +import { ComponentPropsMap, RecordRelatedListProps } from './component.zod'; const ZOD_BARE = 'Invalid input'; @@ -129,3 +134,78 @@ describe('list view `columns`: a failure that is not a mix keeps zod\'s message' expect(JSON.stringify(r.error.issues)).toMatch(NAMES_THE_MIX); }); }); + +/** + * The two sibling declarations of the same key carry the same exclusive union + * and share the same refusal (`list-columns-mix.ts`): a list page's own + * `interfaceConfig.columns` (handed to the same grid, which reads the whole + * array in the spelling of its first entry) and the `record:related_list` + * `columns` prop (the union `listViews[].columns` declares). Each union is + * pinned on its own, so dropping the `error` option from one of them reds + * that union's pins and leaves the others green. + */ +const SIBLINGS: ReadonlyArray<{ + readonly name: string; + readonly parse: (columns: unknown) => { success: boolean; error?: { issues: ReadonlyArray<{ code: string; path: PropertyKey[]; message: string }> } }; +}> = [ + { + name: 'list page `interfaceConfig.columns`', + parse: (columns) => InterfacePageConfigSchema.safeParse({ source: 'crm_lead', columns }), + }, + { + name: '`record:related_list` `columns`', + parse: (columns) => RecordRelatedListProps.safeParse({ objectName: 'task', relationshipField: 'account_id', columns }), + }, +]; + +describe.each(SIBLINGS)('$name: the accept set is unchanged and a mix names itself', ({ parse }) => { + it('all field names parse; all column objects parse', () => { + expect(parse(['name', 'amount']).success).toBe(true); + expect(parse([{ field: 'name' }, { field: 'amount', width: 120 }]).success).toBe(true); + }); + + it.each(MIXES)('a mix with %s is still refused, with the sentence on the union head at `columns`', (_label, columns) => { + const r = parse(columns); + expect(r.success).toBe(false); + if (r.success) return; + const head = r.error!.issues.find((issue) => issue.path.join('.') === 'columns'); + expect(head, JSON.stringify(r.error!.issues)).toBeDefined(); + expect(head!.code).toBe('invalid_union'); + expect(head!.message).toMatch(NAMES_THE_MIX); + expect(head!.message).toMatch(NAMES_THE_REPAIR); + }); + + it('a failure that is not a mix keeps zod\'s message', () => { + const r = parse(['name', 3]); + expect(r.success).toBe(false); + if (r.success) return; + const head = r.error!.issues.find((issue) => issue.path.join('.') === 'columns'); + expect(head, JSON.stringify(r.error!.issues)).toBeDefined(); + expect(head!.message).toBe(ZOD_BARE); + }); +}); + +describe('the doors in front of the two siblings carry the sentence', () => { + const listPage = (columns: unknown) => ({ + name: 'lead_list', + label: 'Leads', + type: 'list', + interfaceConfig: { source: 'crm_lead', columns }, + }); + + it('`PageSchema` (the door `os validate` and the page save parse) refuses a mix at `interfaceConfig.columns`', () => { + const r = PageSchema.safeParse(listPage(['name', { field: 'amount' }])); + expect(r.success).toBe(false); + if (r.success) return; + const head = r.error.issues.find((issue) => issue.path.join('.') === 'interfaceConfig.columns'); + expect(head, JSON.stringify(r.error.issues)).toBeDefined(); + expect(head!.message).toMatch(NAMES_THE_MIX); + // CONTROL: the same page with one spelling is accepted by the same door. + expect(PageSchema.safeParse(listPage(['name', 'amount'])).success).toBe(true); + expect(PageSchema.safeParse(listPage([{ field: 'name' }, { field: 'amount' }])).success).toBe(true); + }); + + it('`ComponentPropsMap[\'record:related_list\']` (the schema the component-props lint judges a page element by) is the pinned union', () => { + expect(ComponentPropsMap['record:related_list']).toBe(RecordRelatedListProps); + }); +}); diff --git a/packages/spec/src/ui/view.zod.ts b/packages/spec/src/ui/view.zod.ts index d5cdebfd118..41eaa06140f 100644 --- a/packages/spec/src/ui/view.zod.ts +++ b/packages/spec/src/ui/view.zod.ts @@ -107,6 +107,8 @@ import { AUDIENCE_REQUIRED_PERMISSIONS_DESCRIPTION } from './audience-required-p // and reached by relative import only: an empty state's `link.url` is judged by // it, never by a second predicate. import { navigationTargetRefusal } from './navigation-target'; +// The `columns` mix refusal, shared with the page and related-list declarations of the same key. +import { listViewColumnsUnionError } from './list-columns-mix'; export { HttpMethodSubsetSchema, HttpRequestSchema }; @@ -3556,43 +3558,6 @@ const VIEW_LIST_COLUMN_STATE_RETIRED = + 'row, and that layout is no longer applied. Delete the key. To ship a column order, list `columns` in that ' + 'order (or set `fieldOrder`); to ship a width, set `width` on the column.'; -// The refusal for a list view `columns` array that mixes the two spellings. -// Declared with `//` on purpose — the `LIST_VIEW_EXPORT_PDF_RETIRED` placement -// note applies: build-docs takes a file's first JSDoc per exported symbol, and -// this module-local constant needs no doc page. -// -// The two arms of `columns` are EXCLUSIVE and stay so: objectui's grid, its -// column spelling diagnostics and the Studio column inspectors all read the -// whole array in the spelling of its FIRST entry, so a mixed array accepted -// here would have its other-spelling entries dropped or misread downstream. -// What was wrong was the refusal: zod folds a failed union into a bare -// `Invalid input`, and the wire mapper then surfaced one branch's -// `columns.0: expected string, received object` — a sentence that reads as if -// column objects were illegal, which costs an AI author a wasted round. With -// this message the union's own head says what is wrong and how to repair it, -// and the wire mapper keeps a head that carries the schema's own sentence. -const LIST_VIEW_COLUMNS_MIXED = - '`columns` mixes field-name strings and column objects, and one list takes one spelling: ' - + "either every entry is a field name (`['name', 'amount']`) or every entry is a column object " - + "(`[{ field: 'name' }, { field: 'amount', width: 120 }]`). To configure some columns, write " - + "the plain ones as objects too: `'name'` becomes `{ field: 'name' }`."; - -/** - * The `columns` union's own error: {@link LIST_VIEW_COLUMNS_MIXED} when the - * authored array holds at least one string AND at least one object entry, - * otherwise `undefined`, so every other failure keeps zod's default message - * with the branch detail nested beneath it (the same contract as - * `exportOptionsPdfUnionError`). It names a mix only; it never changes which - * arrays parse. - */ -const listViewColumnsUnionError = (issue: { input?: unknown }): string | undefined => { - const input = issue.input; - if (!Array.isArray(input)) return undefined; - const hasName = input.some((entry) => typeof entry === 'string'); - const hasObject = input.some((entry) => typeof entry === 'object' && entry !== null && !Array.isArray(entry)); - return hasName && hasObject ? LIST_VIEW_COLUMNS_MIXED : undefined; -}; - /** * List View Schema (Expanded) * Defines how a collection of records is displayed to the user.