From 42bd76354724a047757397fe54ec49398fd366f5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:22:33 +0000 Subject: [PATCH 1/7] fix(metadata-protocol): carry a zod issue's params onto the 422 INVALID_METADATA issue entry Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .../src/protocol-metadata-issues.ts | 36 +++++++++++++++++++ .../spec/src/api/error-code-ledger.zod.ts | 8 +++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/metadata-protocol/src/protocol-metadata-issues.ts b/packages/metadata-protocol/src/protocol-metadata-issues.ts index 9fe01fbfa61..fce4f638039 100644 --- a/packages/metadata-protocol/src/protocol-metadata-issues.ts +++ b/packages/metadata-protocol/src/protocol-metadata-issues.ts @@ -20,6 +20,24 @@ export interface MetadataIssueEntry { path: string; message: string; code: string | undefined; + /** + * The zod issue's own `params`, copied verbatim when the issue carries + * them — absent otherwise, never an empty placeholder. + * + * A spec refinement that has a registered refusal code stamps it here + * (`params.code`, ADR-0112), beside the fields that say which fix applies: + * `CONTROLLED_BY_PARENT_MASTER_INVALID` with its `shape`, + * `PLUGIN_UI_REQUIRED_KEY_MISSING` with its `key`. zod raises every such + * refinement with its own issue code `'custom'`, so without this member a + * client of the save door's `422 INVALID_METADATA` had only that `'custom'` + * and the English `message` to go on, while a local `safeParse` (Studio's + * path) saw the registered code. + * + * Additive on purpose: `code` above stays zod's own issue code, so a client + * that matches `'custom'` today still matches it. Read the registered + * refusal code from `params.code`. + */ + params?: Record; } /** @@ -121,6 +139,18 @@ interface ZodIssueLike { code?: unknown; errors?: unknown; issues?: unknown; + params?: unknown; +} + +/** + * The issue's `params`, copied onto its entry, or `undefined` when it carries + * none: zod sets `params` only where a refinement passed one (`code: 'custom'` + * issues), and a non-object value there is not a params bag. + */ +function issueParamsOf(issue: ZodIssueLike): Record | undefined { + const params = issue?.params; + if (params === null || typeof params !== 'object' || Array.isArray(params)) return undefined; + return { ...(params as Record) }; } /** A zod issue path, normalised to the array zod always produces. */ @@ -302,6 +332,8 @@ function collectMetadataIssues( message: String(issue?.message ?? 'Invalid value'), code: issue?.code === undefined ? undefined : String(issue.code), }; + const params = issueParamsOf(issue); + if (params !== undefined) entry.params = params; if (!expandable) { const key = JSON.stringify([entry.path, entry.code, entry.message]); @@ -367,6 +399,10 @@ function collectMetadataIssues( * entry below explains it (#35400, on {@link collectMetadataIssues}), so * `issues[0]` of a refused view save is the first located cause, not that * rootless line. Read entries by `path` and `code`, never by index. + * + * Each entry also carries its issue's `params` when zod's issue has them + * ({@link MetadataIssueEntry.params}): that is where a spec refusal with a + * registered code says which one it is, since `code` is zod's `'custom'`. */ export function zodIssuesToMetadataIssues(issues: unknown): MetadataIssueEntry[] { if (!Array.isArray(issues)) return []; diff --git a/packages/spec/src/api/error-code-ledger.zod.ts b/packages/spec/src/api/error-code-ledger.zod.ts index af244f28e86..343ca879c73 100644 --- a/packages/spec/src/api/error-code-ledger.zod.ts +++ b/packages/spec/src/api/error-code-ledger.zod.ts @@ -1959,10 +1959,12 @@ export const ERROR_CODE_LEDGER = { // `ObjectSchema`'s refinement and `defineStack`'s extension-merge gate, never // on a thrown error, and not in the `message`; no `status` of its own. // Visible on the zod issue itself (any `safeParse`, the Studio settings - // tab's path, which keys its localized wording on it) and on `defineStack`'s - // `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues. NOT + // tab's path, which keys its localized wording on it), on `defineStack`'s + // `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues, and // on the metadata save door's `422 INVALID_METADATA`: `zodIssuesToMetadataIssues` - // keeps `path`, `message` and zod's `code` ('custom'), never `params`. + // copies the issue's `params` onto its entry, where this code is + // `params.code` beside `params.shape`; the entry's own `code` stays zod's + // issue code ('custom'). 'CONTROLLED_BY_PARENT_MASTER_INVALID', 'EXTERNAL_SCHEMA_MISMATCH', 'EXTERNAL_SCHEMA_MODE_VIOLATION', From 9fd2a2a62c118d42218027a784a21783078ef334 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:26:01 +0000 Subject: [PATCH 2/7] test(metadata-protocol): pin params on the 422 INVALID_METADATA entry Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .../src/protocol.save-union-issues.test.ts | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) 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 64bd9221cfa..b551d4bd99f 100644 --- a/packages/metadata-protocol/src/protocol.save-union-issues.test.ts +++ b/packages/metadata-protocol/src/protocol.save-union-issues.test.ts @@ -35,7 +35,8 @@ import { z } from 'zod'; // #5619 sank the two predicates into a package both sides already depend on. import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; import { applyConversionsToStoredItem } from '@objectstack/spec'; -import { getMetadataTypeSchema } from '@objectstack/spec/kernel'; +import { CONTROLLED_BY_PARENT_MASTER_INVALID } from '@objectstack/spec/data'; +import { getMetadataTypeSchema, PLUGIN_UI_REQUIRED_KEY_MISSING, PluginSchema } from '@objectstack/spec/kernel'; import { ObjectStackProtocolImplementation, zodIssuesToMetadataIssues } from './protocol.js'; import { ZOD_UNION_DEFAULT_MESSAGE } from './protocol-metadata-issues.js'; @@ -702,3 +703,82 @@ describe('#35500 a member that is itself a bare union of kinds explains nothing' expect(zodIssuesToMetadataIssues(issuesOf(schema, { u: { a: 1 } })).map((e) => [e.path, e.code])).toEqual([['u.a', 'invalid_type']]); }); }); + +/** + * #36594 — a spec refusal's registered code reaches the save door's 422. + * + * A spec refinement that owns an ADR-0112 code stamps it on its zod issue as + * `params.code`, beside the fields that say which fix applies; zod's own issue + * code for every such refinement is `'custom'`. The entry kept `path`, `message` + * and that `'custom'` and dropped `params`, so a client of the 422 could not + * tell one refusal from another except by its English sentence. The entry now + * carries the issue's `params` verbatim, and its `code` is unchanged. + * + * `err.issues` is the 422 body's `issues` member as sent: the REST error + * answer spreads it unchanged (`packages/rest/src/error-response.ts`). + */ +describe('#36594 the 422 INVALID_METADATA entry carries the zod issue\'s params', () => { + const detailWithReadonlyMaster = () => ({ + name: 'work_order_item', + label: 'Work Order Item', + sharingModel: 'controlled_by_parent', + fields: { + order: { name: 'order', type: 'master_detail', label: 'Order', reference: 'work_order', required: true, readonly: true }, + note: { name: 'note', type: 'text', label: 'Note' }, + }, + }); + + it('CONTROLLED_BY_PARENT_MASTER_INVALID: an object save names the code and its shape; `code` stays \'custom\'', async () => { + const { protocol, rows } = makeProtocol(); + + const err = await rejection(save(protocol, detailWithReadonlyMaster(), 'work_order_item', 'object')); + + expect(err.code).toBe('INVALID_METADATA'); + expect(err.status).toBe(422); + expect(err.issues).toEqual([{ + path: 'fields', + message: expect.any(String), + code: 'custom', + params: { code: CONTROLLED_BY_PARENT_MASTER_INVALID, shape: 'master-not-writable' }, + }]); + expect(rows.size).toBe(0); + }); + + it('PLUGIN_UI_REQUIRED_KEY_MISSING: the real `PluginSchema` refusal, through the door\'s mapper, names the code and the missing key', () => { + // No metadata type the save door accepts parses with `PluginSchema` on + // this tree (a type it does not know is refused `INVALID_REQUEST` before + // any schema runs), so this pins the one mapper every 422 INVALID_METADATA + // producer shares, fed the issues the real schema raises. + const parsed = PluginSchema.safeParse({ id: 'com.example.panel', name: 'Panel', version: '1.0.0', type: 'ui', slug: 'panel' }); + expect(parsed.success).toBe(false); + + const issues = zodIssuesToMetadataIssues(parsed.error!.issues); + + expect(issues).toEqual([{ + path: 'staticPath', + message: expect.stringMatching(new RegExp(`^${PLUGIN_UI_REQUIRED_KEY_MISSING}: `)), + code: 'custom', + params: { code: PLUGIN_UI_REQUIRED_KEY_MISSING, key: 'staticPath' }, + }]); + }); + + it('CONTROL — an issue zod raised without params gets no `params` member', async () => { + const { protocol } = makeProtocol(); + const body = { ...detailWithReadonlyMaster(), sharingModel: 'controlled_by_parent', label: 42 }; + + const err = await rejection(save(protocol, body, 'work_order_item', 'object')); + + expect(err.status).toBe(422); + const label = err.issues.find((i: any) => i.path === 'label'); + expect(label).toEqual({ path: 'label', message: expect.any(String), code: 'invalid_type' }); + expect(Object.prototype.hasOwnProperty.call(label, 'params')).toBe(false); + }); + + it('the mapper copies `params` and does not hand out the issue\'s own object', () => { + const params = { code: 'SOME_REGISTERED_CODE', detail: 'x' }; + const [entry] = zodIssuesToMetadataIssues([{ code: 'custom', path: ['a'], message: 'm', params }]); + + expect(entry).toEqual({ path: 'a', message: 'm', code: 'custom', params }); + expect(entry.params).not.toBe(params); + }); +}); From e28da1178a059140ad81ea1eadf3a935bbc67ed5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:26:39 +0000 Subject: [PATCH 3/7] chore(changeset): params on the 422 INVALID_METADATA issue entry Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .changeset/36594-metadata-issue-params.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .changeset/36594-metadata-issue-params.md diff --git a/.changeset/36594-metadata-issue-params.md b/.changeset/36594-metadata-issue-params.md new file mode 100644 index 00000000000..df9d569536b --- /dev/null +++ b/.changeset/36594-metadata-issue-params.md @@ -0,0 +1,16 @@ +--- +'@objectstack/metadata-protocol': patch +'@objectstack/spec': patch +--- + +fix(metadata-protocol): a `422 INVALID_METADATA` issue entry carries its zod issue's `params`, so a client of the save door can read the registered refusal code + +Clause-②: yes (widening) +Metadata-protocol: none + +A spec rule that owns a registered error code stamps it on its zod issue as `params.code`, next to the fields that say which fix applies: `CONTROLLED_BY_PARENT_MASTER_INVALID` with `shape`, `PLUGIN_UI_REQUIRED_KEY_MISSING` with `key`. zod reports every such rule with its own issue code `'custom'`. The metadata save door's `422 INVALID_METADATA` built each `issues[]` entry from `path`, `message` and that `'custom'` only, so a client of the door (anything that does not parse locally the way Studio does) could not tell which rule refused the body except by its English message. + +- **`@objectstack/metadata-protocol`.** `MetadataIssueEntry` gains an optional `params` member: a copy of the zod issue's own `params`, present only when the issue has them. `zodIssuesToMetadataIssues` fills it, so every producer that maps through it carries it: the save door's 422, the package commit and client failure paths, and the read-path diagnostics. An object save refused for a `controlled_by_parent` master now answers `{ path: 'fields', code: 'custom', message, params: { code: 'CONTROLLED_BY_PARENT_MASTER_INVALID', shape: 'master-not-writable' } }`. Read the registered code from `params.code`. +- **`@objectstack/spec`.** The ledger note on `CONTROLLED_BY_PARENT_MASTER_INVALID` now says the save door's 422 carries the code on `params.code`. + +Nothing to migrate. The entry's `code` is unchanged (still zod's issue code, `'custom'` for these rules), and an issue without `params` gets no `params` member, so a client that matches `code` or compares whole entries for other issues reads exactly what it read before. From 4522e973697887081c7ca921c980fac7333e7a51 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:29:05 +0000 Subject: [PATCH 4/7] docs(spec): the CONTROLLED_BY_PARENT_MASTER_INVALID docblock names params.code on the save door's 422 Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- packages/spec/src/data/controlled-by-parent-master.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/spec/src/data/controlled-by-parent-master.ts b/packages/spec/src/data/controlled-by-parent-master.ts index 40df5d9f602..9f6843d876b 100644 --- a/packages/spec/src/data/controlled-by-parent-master.ts +++ b/packages/spec/src/data/controlled-by-parent-master.ts @@ -116,11 +116,12 @@ export type ControlledByParentMasterShape = * that ships in `dist` is the published face, door or no door). It has no * `status` of its own, and it is NOT in the `message`. Where a reader can see * it: on the zod issue itself (any `ObjectSchema` / `ObjectStackDefinitionSchema` - * `safeParse`, which is the Studio tab's path), and on `defineStack`'s - * `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues. Where - * it is not: the metadata save door's `422 INVALID_METADATA` — its `issues[]` - * entries (`zodIssuesToMetadataIssues`, `@objectstack/metadata-protocol`) carry - * `path`, `message` and zod's own issue `code` (`'custom'`), never `params`. + * `safeParse`, which is the Studio tab's path), on `defineStack`'s + * `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues, and on + * the metadata save door's `422 INVALID_METADATA`, whose `issues[]` entries + * (`zodIssuesToMetadataIssues`, `@objectstack/metadata-protocol`) carry the + * issue's `params` beside `path`, `message` and zod's own issue `code` + * (`'custom'`): read the code there from `params.code`. */ export const CONTROLLED_BY_PARENT_MASTER_INVALID = 'CONTROLLED_BY_PARENT_MASTER_INVALID' as const; From 796b3f7746efb15945cf7b424e2d1991310856d5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:30:08 +0000 Subject: [PATCH 5/7] chore(changeset): name the docblock fix Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .changeset/36594-metadata-issue-params.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/36594-metadata-issue-params.md b/.changeset/36594-metadata-issue-params.md index df9d569536b..8ef77453d68 100644 --- a/.changeset/36594-metadata-issue-params.md +++ b/.changeset/36594-metadata-issue-params.md @@ -11,6 +11,6 @@ Metadata-protocol: none A spec rule that owns a registered error code stamps it on its zod issue as `params.code`, next to the fields that say which fix applies: `CONTROLLED_BY_PARENT_MASTER_INVALID` with `shape`, `PLUGIN_UI_REQUIRED_KEY_MISSING` with `key`. zod reports every such rule with its own issue code `'custom'`. The metadata save door's `422 INVALID_METADATA` built each `issues[]` entry from `path`, `message` and that `'custom'` only, so a client of the door (anything that does not parse locally the way Studio does) could not tell which rule refused the body except by its English message. - **`@objectstack/metadata-protocol`.** `MetadataIssueEntry` gains an optional `params` member: a copy of the zod issue's own `params`, present only when the issue has them. `zodIssuesToMetadataIssues` fills it, so every producer that maps through it carries it: the save door's 422, the package commit and client failure paths, and the read-path diagnostics. An object save refused for a `controlled_by_parent` master now answers `{ path: 'fields', code: 'custom', message, params: { code: 'CONTROLLED_BY_PARENT_MASTER_INVALID', shape: 'master-not-writable' } }`. Read the registered code from `params.code`. -- **`@objectstack/spec`.** The ledger note on `CONTROLLED_BY_PARENT_MASTER_INVALID` now says the save door's 422 carries the code on `params.code`. +- **`@objectstack/spec`.** The `CONTROLLED_BY_PARENT_MASTER_INVALID` docblock and its ledger note now say the save door's 422 carries the code on `params.code`. Nothing to migrate. The entry's `code` is unchanged (still zod's issue code, `'custom'` for these rules), and an issue without `params` gets no `params` member, so a client that matches `code` or compares whole entries for other issues reads exactly what it read before. From 53c0af57fb1c67d3aaf1f158df339c8ecc8be29f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 13:20:03 +0000 Subject: [PATCH 6/7] fix(metadata-protocol): params only on the save door's 422, only a registered refusal's JSON-scalar members Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .../src/protocol-metadata-issues.ts | 94 ++++++++++----- .../metadata-protocol/src/protocol-save.ts | 5 +- .../src/protocol.save-union-issues.test.ts | 111 ++++++++++++------ 3 files changed, 146 insertions(+), 64 deletions(-) diff --git a/packages/metadata-protocol/src/protocol-metadata-issues.ts b/packages/metadata-protocol/src/protocol-metadata-issues.ts index fce4f638039..49c9d698bc1 100644 --- a/packages/metadata-protocol/src/protocol-metadata-issues.ts +++ b/packages/metadata-protocol/src/protocol-metadata-issues.ts @@ -1,5 +1,7 @@ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. +import { ErrorCode } from '@objectstack/spec/api'; + // [#25467 E0] Part of the module preamble of `protocol.ts`, moved here verbatim: the metadata // issue entry and the Zod-issue walk that turns a parse failure into located metadata issues. // @@ -21,23 +23,35 @@ export interface MetadataIssueEntry { message: string; code: string | undefined; /** - * The zod issue's own `params`, copied verbatim when the issue carries - * them — absent otherwise, never an empty placeholder. + * The registered refusal a spec rule stamped on the issue, on the save + * door's `422 INVALID_METADATA` only: `params.code` is the ADR-0112 code + * (one {@link ErrorCode} accepts) and its siblings say which fix applies — + * `CONTROLLED_BY_PARENT_MASTER_INVALID` with its `shape`. zod raises every + * such rule with its own issue code `'custom'`, so without this member a + * client of the door had only that `'custom'` and the English `message`, + * while a local `safeParse` (Studio's path) saw the registered code. + * + * The contract, and why each limit is there: * - * A spec refinement that has a registered refusal code stamps it here - * (`params.code`, ADR-0112), beside the fields that say which fix applies: - * `CONTROLLED_BY_PARENT_MASTER_INVALID` with its `shape`, - * `PLUGIN_UI_REQUIRED_KEY_MISSING` with its `key`. zod raises every such - * refinement with its own issue code `'custom'`, so without this member a - * client of the save door's `422 INVALID_METADATA` had only that `'custom'` - * and the English `message` to go on, while a local `safeParse` (Studio's - * path) saw the registered code. + * - **Only where asked for** ({@link ZodIssuesToMetadataIssuesOptions}). + * The save door asks. The read path (`_diagnostics.errors[]`) and the + * seed refusal (`seedApplied.issues[]`) do not: their published spec + * schemas (`MetadataValidationResultSchema`, the publish response) + * declare the entry as exactly `path` / `message` / `code`. + * - **Only a registered refusal.** An issue whose `params.code` is not a + * string {@link ErrorCode} accepts gets no member: `params` is also where + * spec rules keep internal markers (`readAsWritten`, `refusalHead`, + * `retiredComponentType`) that are not a wire vocabulary. + * - **Only JSON scalars** (a string, a finite number, a boolean). Any other + * value is left out, so answering never throws on a value + * `JSON.stringify` cannot write, and the copy shares nothing with the + * issue. * - * Additive on purpose: `code` above stays zod's own issue code, so a client - * that matches `'custom'` today still matches it. Read the registered - * refusal code from `params.code`. + * Absent otherwise, never an empty placeholder. Additive: `code` above + * stays zod's own issue code, so a client that matches `'custom'` today + * still matches it. Read the registered code from `params.code`. */ - params?: Record; + params?: Record; } /** @@ -143,14 +157,34 @@ interface ZodIssueLike { } /** - * The issue's `params`, copied onto its entry, or `undefined` when it carries - * none: zod sets `params` only where a refinement passed one (`code: 'custom'` - * issues), and a non-object value there is not a params bag. + * The registered refusal an issue carries on `params`, as + * {@link MetadataIssueEntry.params} states it: `undefined` unless `params` is + * a plain object whose `code` is a string {@link ErrorCode} accepts, and then + * only its JSON-scalar members. */ -function issueParamsOf(issue: ZodIssueLike): Record | undefined { +function refusalParamsOf(issue: ZodIssueLike): Record | undefined { const params = issue?.params; if (params === null || typeof params !== 'object' || Array.isArray(params)) return undefined; - return { ...(params as Record) }; + const code = (params as { code?: unknown }).code; + if (typeof code !== 'string' || !ErrorCode.safeParse(code).success) return undefined; + const out: Record = {}; + for (const [key, value] of Object.entries(params as Record)) { + if (typeof value === 'string' || typeof value === 'boolean' + || (typeof value === 'number' && Number.isFinite(value))) { + out[key] = value; + } + } + return out; +} + +/** How {@link zodIssuesToMetadataIssues} renders its entries. */ +export interface ZodIssuesToMetadataIssuesOptions { + /** + * Carry each issue's registered refusal on {@link MetadataIssueEntry.params}. + * The save door's `422 INVALID_METADATA` sets it; a producer whose entry + * shape a spec schema declares closed leaves it off. + */ + readonly refusalParams?: boolean; } /** A zod issue path, normalised to the array zod always produces. */ @@ -311,6 +345,7 @@ function collectMetadataIssues( depth: number, seen: Set, out: MetadataIssueEntry[], + refusalParams: boolean, ): void { const path = [...parentPath, ...issuePathOf(issue)]; const branches: readonly (readonly ZodIssueLike[])[] = @@ -332,7 +367,7 @@ function collectMetadataIssues( message: String(issue?.message ?? 'Invalid value'), code: issue?.code === undefined ? undefined : String(issue.code), }; - const params = issueParamsOf(issue); + const params = refusalParams ? refusalParamsOf(issue) : undefined; if (params !== undefined) entry.params = params; if (!expandable) { @@ -347,7 +382,7 @@ function collectMetadataIssues( const below: MetadataIssueEntry[] = []; for (const branch of selectUnionBranches(branches)) { for (const nested of branch) { - collectMetadataIssues(nested, path, depth + 1, seen, below); + collectMetadataIssues(nested, path, depth + 1, seen, below, refusalParams); } } // The head stays ahead of what it explains unless BOTH hold: something @@ -360,7 +395,7 @@ function collectMetadataIssues( out.push(entry); // Unranked, and every one of them: see {@link CONTAINER_ISSUE_CODES}. for (const nested of contained) { - collectMetadataIssues(nested, path, depth + 1, seen, out); + collectMetadataIssues(nested, path, depth + 1, seen, out, refusalParams); } } @@ -400,17 +435,22 @@ function collectMetadataIssues( * `issues[0]` of a refused view save is the first located cause, not that * rootless line. Read entries by `path` and `code`, never by index. * - * Each entry also carries its issue's `params` when zod's issue has them - * ({@link MetadataIssueEntry.params}): that is where a spec refusal with a - * registered code says which one it is, since `code` is zod's `'custom'`. + * With `refusalParams` (the save door's 422), an entry also carries its + * issue's registered refusal ({@link MetadataIssueEntry.params}): that is where + * a spec rule with a registered code says which one it is, since `code` is + * zod's `'custom'`. Without it every entry is exactly `path` / `message` / + * `code`, the shape the read path and the seed refusal publish. */ -export function zodIssuesToMetadataIssues(issues: unknown): MetadataIssueEntry[] { +export function zodIssuesToMetadataIssues( + issues: unknown, + options: ZodIssuesToMetadataIssuesOptions = {}, +): MetadataIssueEntry[] { if (!Array.isArray(issues)) return []; const out: MetadataIssueEntry[] = []; for (const issue of issues) { // A fresh `seen` per top-level issue: de-duplication is about one // union's branches agreeing, never about two independent issues. - collectMetadataIssues(issue as ZodIssueLike, [], 0, new Set(), out); + collectMetadataIssues(issue as ZodIssueLike, [], 0, new Set(), out, options.refusalParams === true); } return out; } diff --git a/packages/metadata-protocol/src/protocol-save.ts b/packages/metadata-protocol/src/protocol-save.ts index 9aa7c074415..8095a8f8cb9 100644 --- a/packages/metadata-protocol/src/protocol-save.ts +++ b/packages/metadata-protocol/src/protocol-save.ts @@ -1665,7 +1665,10 @@ export class ProtocolSaveFacet { // every other body is judged as stored. const parsed = schema.safeParse(flowGateVerdictBody ?? request.item); if (!parsed.success) { - const issues = zodIssuesToMetadataIssues(parsed.error.issues); + // [#36594] The 422 alone carries each issue's registered + // refusal (`params.code` and its siblings); the read path + // and the seed refusal publish a closed entry shape. + const issues = zodIssuesToMetadataIssues(parsed.error.issues, { refusalParams: true }); // [#10524 → commit d806081dd] The findings clause is rendered PER // FACE — see {@link specValidationFindings}. #10524's // headline is applied on the faces that carry the same 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 b551d4bd99f..f9285493315 100644 --- a/packages/metadata-protocol/src/protocol.save-union-issues.test.ts +++ b/packages/metadata-protocol/src/protocol.save-union-issues.test.ts @@ -36,8 +36,9 @@ import { z } from 'zod'; import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; import { applyConversionsToStoredItem } from '@objectstack/spec'; import { CONTROLLED_BY_PARENT_MASTER_INVALID } from '@objectstack/spec/data'; -import { getMetadataTypeSchema, PLUGIN_UI_REQUIRED_KEY_MISSING, PluginSchema } from '@objectstack/spec/kernel'; -import { ObjectStackProtocolImplementation, zodIssuesToMetadataIssues } from './protocol.js'; +import { getMetadataTypeSchema, PLUGIN_UI_REQUIRED_KEY_MISSING, PluginSchema, registerMetadataTypeSchema } from '@objectstack/spec/kernel'; +import { ObjectStackProtocolImplementation, seedRequestValidationError, zodIssuesToMetadataIssues } from './protocol.js'; +import { decorateMetadataItem } from './metadata-diagnostics.js'; import { ZOD_UNION_DEFAULT_MESSAGE } from './protocol-metadata-issues.js'; interface Row { @@ -707,17 +708,20 @@ describe('#35500 a member that is itself a bare union of kinds explains nothing' /** * #36594 — a spec refusal's registered code reaches the save door's 422. * - * A spec refinement that owns an ADR-0112 code stamps it on its zod issue as + * A spec rule that owns an ADR-0112 code stamps it on its zod issue as * `params.code`, beside the fields that say which fix applies; zod's own issue - * code for every such refinement is `'custom'`. The entry kept `path`, `message` - * and that `'custom'` and dropped `params`, so a client of the 422 could not - * tell one refusal from another except by its English sentence. The entry now - * carries the issue's `params` verbatim, and its `code` is unchanged. + * code for every such rule is `'custom'`. The 422 entry kept `path`, `message` + * and that `'custom'` and dropped `params`, so a client of the door could not + * tell one refusal from another except by its English sentence. The 422 entry + * now carries the registered refusal; `code` is unchanged. * - * `err.issues` is the 422 body's `issues` member as sent: the REST error - * answer spreads it unchanged (`packages/rest/src/error-response.ts`). + * Only the 422: the read path's `_diagnostics.errors[]` and the seed refusal's + * `seedApplied.issues[]` publish the entry as exactly `path` / `message` / + * `code` (`MetadataValidationResultSchema`, the publish response), so they stay + * that shape. `err.issues` is the 422 body's `issues` member as sent: the REST + * error answer spreads it unchanged (`packages/rest/src/error-response.ts`). */ -describe('#36594 the 422 INVALID_METADATA entry carries the zod issue\'s params', () => { +describe('#36594 the save door\'s 422 entry carries the registered refusal on `params`', () => { const detailWithReadonlyMaster = () => ({ name: 'work_order_item', label: 'Work Order Item', @@ -727,6 +731,7 @@ describe('#36594 the 422 INVALID_METADATA entry carries the zod issue\'s params' note: { name: 'note', type: 'text', label: 'Note' }, }, }); + const CBP_PARAMS = { code: CONTROLLED_BY_PARENT_MASTER_INVALID, shape: 'master-not-writable' }; it('CONTROLLED_BY_PARENT_MASTER_INVALID: an object save names the code and its shape; `code` stays \'custom\'', async () => { const { protocol, rows } = makeProtocol(); @@ -735,26 +740,57 @@ describe('#36594 the 422 INVALID_METADATA entry carries the zod issue\'s params' expect(err.code).toBe('INVALID_METADATA'); expect(err.status).toBe(422); - expect(err.issues).toEqual([{ - path: 'fields', - message: expect.any(String), - code: 'custom', - params: { code: CONTROLLED_BY_PARENT_MASTER_INVALID, shape: 'master-not-writable' }, - }]); + expect(err.issues).toEqual([{ path: 'fields', message: expect.any(String), code: 'custom', params: CBP_PARAMS }]); expect(rows.size).toBe(0); }); - it('PLUGIN_UI_REQUIRED_KEY_MISSING: the real `PluginSchema` refusal, through the door\'s mapper, names the code and the missing key', () => { + it('CONTROL — an issue zod raised without params gets no `params` member', async () => { + const { protocol } = makeProtocol(); + const body = { ...detailWithReadonlyMaster(), label: 42 }; + + const err = await rejection(save(protocol, body, 'work_order_item', 'object')); + + expect(err.status).toBe(422); + const label = err.issues.find((i: any) => i.path === 'label'); + expect(label).toEqual({ path: 'label', message: expect.any(String), code: 'invalid_type' }); + expect(Object.prototype.hasOwnProperty.call(label, 'params')).toBe(false); + }); + + it('a BigInt, a cycle, a function and NaN in a refusal\'s params are left out, and the door still answers 422', async () => { + // The door judges a type by the schema `getMetadataTypeSchema` resolves, and a + // registered override wins over the built-in one; restored below. + const builtin = getMetadataTypeSchema('object')!; + const cyclic: Record = {}; + cyclic.self = cyclic; + registerMetadataTypeSchema('object', z.any().superRefine((_v, ctx) => { + ctx.addIssue({ + code: 'custom', path: ['fields'], message: 'refused', + params: { ...CBP_PARAMS, big: BigInt(1), cyclic, fn: () => 1, nan: Number.NaN, count: 2, flag: true }, + }); + })); + try { + const { protocol } = makeProtocol(); + + const err = await rejection(save(protocol, detailWithReadonlyMaster(), 'work_order_item', 'object')); + + expect(err.code).toBe('INVALID_METADATA'); + expect(err.status).toBe(422); + expect(err.issues).toEqual([{ path: 'fields', message: 'refused', code: 'custom', params: { ...CBP_PARAMS, count: 2, flag: true } }]); + expect(() => JSON.stringify({ issues: err.issues })).not.toThrow(); + } finally { + registerMetadataTypeSchema('object', builtin); + } + expect(getMetadataTypeSchema('object')).toBe(builtin); + }); + + it('PLUGIN_UI_REQUIRED_KEY_MISSING: the real `PluginSchema` refusal, through the mapper the door calls, names the code and the missing key', () => { // No metadata type the save door accepts parses with `PluginSchema` on - // this tree (a type it does not know is refused `INVALID_REQUEST` before - // any schema runs), so this pins the one mapper every 422 INVALID_METADATA - // producer shares, fed the issues the real schema raises. + // this tree (a type name it does not know is refused `INVALID_REQUEST` + // before any schema runs), so this pins the mapper with the door's option. const parsed = PluginSchema.safeParse({ id: 'com.example.panel', name: 'Panel', version: '1.0.0', type: 'ui', slug: 'panel' }); expect(parsed.success).toBe(false); - const issues = zodIssuesToMetadataIssues(parsed.error!.issues); - - expect(issues).toEqual([{ + expect(zodIssuesToMetadataIssues(parsed.error!.issues, { refusalParams: true })).toEqual([{ path: 'staticPath', message: expect.stringMatching(new RegExp(`^${PLUGIN_UI_REQUIRED_KEY_MISSING}: `)), code: 'custom', @@ -762,23 +798,26 @@ describe('#36594 the 422 INVALID_METADATA entry carries the zod issue\'s params' }]); }); - it('CONTROL — an issue zod raised without params gets no `params` member', async () => { - const { protocol } = makeProtocol(); - const body = { ...detailWithReadonlyMaster(), sharingModel: 'controlled_by_parent', label: 42 }; + it('params whose `code` is not a registered error code are not a refusal: internal markers stay off the wire', () => { + const issue = (params: unknown) => ({ code: 'custom', path: ['a'], message: 'm', params }); - const err = await rejection(save(protocol, body, 'work_order_item', 'object')); + for (const params of [{ readAsWritten: true }, { retiredComponentType: 'x' }, { code: 'NOT_A_REGISTERED_CODE' }, { code: 7 }, ['code'], null]) { + const [entry] = zodIssuesToMetadataIssues([issue(params)], { refusalParams: true }); + expect(entry).toEqual({ path: 'a', message: 'm', code: 'custom' }); + } + }); - expect(err.status).toBe(422); - const label = err.issues.find((i: any) => i.path === 'label'); - expect(label).toEqual({ path: 'label', message: expect.any(String), code: 'invalid_type' }); - expect(Object.prototype.hasOwnProperty.call(label, 'params')).toBe(false); + it('READ PATH — `_diagnostics.errors[]` on a stored object with the same refusal carries no `params`', () => { + const decorated: any = decorateMetadataItem('object', detailWithReadonlyMaster()); + + expect(decorated._diagnostics.valid).toBe(false); + expect(decorated._diagnostics.errors).toEqual([{ path: 'fields', message: expect.any(String), code: 'custom' }]); }); - it('the mapper copies `params` and does not hand out the issue\'s own object', () => { - const params = { code: 'SOME_REGISTERED_CODE', detail: 'x' }; - const [entry] = zodIssuesToMetadataIssues([{ code: 'custom', path: ['a'], message: 'm', params }]); + it('SEED — the seed refusal behind `seedApplied.issues[]` carries no `params`', () => { + const err: any = seedRequestValidationError([{ code: 'custom', path: ['seeds', 0], message: 'm', params: CBP_PARAMS }]); - expect(entry).toEqual({ path: 'a', message: 'm', code: 'custom', params }); - expect(entry.params).not.toBe(params); + expect(err.code).toBe('INVALID_METADATA'); + expect(err.issues).toEqual([{ path: 'seeds.0', message: 'm', code: 'custom' }]); }); }); From ddbd55783dddec63357147f49895fa9cacb50d15 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 13:20:28 +0000 Subject: [PATCH 7/7] docs: ledger note and docblock name the 422 entry as the only carrier; changeset follows the save-door-only contract Claude-Session: https://claude.ai/code/session_01L9qibzQhDYnejPUPpyJvyc Co-authored-by: Claude --- .changeset/36594-metadata-issue-params.md | 8 ++++---- packages/spec/src/api/error-code-ledger.zod.ts | 12 +++++++----- .../spec/src/data/controlled-by-parent-master.ts | 6 ++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.changeset/36594-metadata-issue-params.md b/.changeset/36594-metadata-issue-params.md index 8ef77453d68..9ce6639e78c 100644 --- a/.changeset/36594-metadata-issue-params.md +++ b/.changeset/36594-metadata-issue-params.md @@ -3,14 +3,14 @@ '@objectstack/spec': patch --- -fix(metadata-protocol): a `422 INVALID_METADATA` issue entry carries its zod issue's `params`, so a client of the save door can read the registered refusal code +fix(metadata-protocol): a `422 INVALID_METADATA` issue entry carries the registered refusal on `params`, so a client of the save door can read the code Clause-②: yes (widening) Metadata-protocol: none -A spec rule that owns a registered error code stamps it on its zod issue as `params.code`, next to the fields that say which fix applies: `CONTROLLED_BY_PARENT_MASTER_INVALID` with `shape`, `PLUGIN_UI_REQUIRED_KEY_MISSING` with `key`. zod reports every such rule with its own issue code `'custom'`. The metadata save door's `422 INVALID_METADATA` built each `issues[]` entry from `path`, `message` and that `'custom'` only, so a client of the door (anything that does not parse locally the way Studio does) could not tell which rule refused the body except by its English message. +A spec rule that owns a registered error code stamps it on its zod issue as `params.code`, next to the fields that say which fix applies: `CONTROLLED_BY_PARENT_MASTER_INVALID` with `shape` (an object save), and `PLUGIN_UI_REQUIRED_KEY_MISSING` with `key` (`PluginSchema`, which no save-door type parses today). zod reports every such rule with its own issue code `'custom'`. The metadata save door's `422 INVALID_METADATA` built each `issues[]` entry from `path`, `message` and that `'custom'` only, so a client of the door (anything that does not parse locally the way Studio does) could not tell which rule refused the body except by its English message. -- **`@objectstack/metadata-protocol`.** `MetadataIssueEntry` gains an optional `params` member: a copy of the zod issue's own `params`, present only when the issue has them. `zodIssuesToMetadataIssues` fills it, so every producer that maps through it carries it: the save door's 422, the package commit and client failure paths, and the read-path diagnostics. An object save refused for a `controlled_by_parent` master now answers `{ path: 'fields', code: 'custom', message, params: { code: 'CONTROLLED_BY_PARENT_MASTER_INVALID', shape: 'master-not-writable' } }`. Read the registered code from `params.code`. +- **`@objectstack/metadata-protocol`.** The save door's 422 entry gains an optional `params` member: the registered refusal the issue carries, present only when the issue's `params.code` is a registered error code, and holding only its string, finite-number and boolean members. An object save refused for a `controlled_by_parent` master now answers `{ path: 'fields', code: 'custom', message, params: { code: 'CONTROLLED_BY_PARENT_MASTER_INVALID', shape: 'master-not-writable' } }`. Read the registered code from `params.code`. `zodIssuesToMetadataIssues` takes a new option, `refusalParams`, which only the save door sets. The read path's `_diagnostics.errors[]` and the seed refusal's `seedApplied.issues[]` keep exactly `path`, `message` and `code`, the shape their spec schemas publish. - **`@objectstack/spec`.** The `CONTROLLED_BY_PARENT_MASTER_INVALID` docblock and its ledger note now say the save door's 422 carries the code on `params.code`. -Nothing to migrate. The entry's `code` is unchanged (still zod's issue code, `'custom'` for these rules), and an issue without `params` gets no `params` member, so a client that matches `code` or compares whole entries for other issues reads exactly what it read before. +Nothing to migrate. The entry's `code` is unchanged (still zod's issue code, `'custom'` for these rules), and an entry that carries no registered refusal gets no `params` member, so a client that matches `code` or compares whole entries for other issues reads exactly what it read before. diff --git a/packages/spec/src/api/error-code-ledger.zod.ts b/packages/spec/src/api/error-code-ledger.zod.ts index 343ca879c73..3927c0a27c4 100644 --- a/packages/spec/src/api/error-code-ledger.zod.ts +++ b/packages/spec/src/api/error-code-ledger.zod.ts @@ -1953,7 +1953,8 @@ export const ERROR_CODE_LEDGER = { // tree answers with it, the thrown value is the boundary. 'CONNECTOR_UPSTREAM_CHANGED', 'CONNECTOR_UPSTREAM_UNAVAILABLE', - // `door: 'none'` on the `PLUGIN_UI_REQUIRED_KEY_MISSING` reading below: the + // `door: 'none'` on the `PLUGIN_UI_REQUIRED_KEY_MISSING` reading below, read + // as: no door answers with this code as its `error.code`. The // `controlled_by_parent` master refusal's code (`data/controlled-by-parent-master.ts`), // stamped on the zod ISSUE as `params.code` beside `params.shape` by // `ObjectSchema`'s refinement and `defineStack`'s extension-merge gate, never @@ -1961,10 +1962,11 @@ export const ERROR_CODE_LEDGER = { // Visible on the zod issue itself (any `safeParse`, the Studio settings // tab's path, which keys its localized wording on it), on `defineStack`'s // `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues, and - // on the metadata save door's `422 INVALID_METADATA`: `zodIssuesToMetadataIssues` - // copies the issue's `params` onto its entry, where this code is - // `params.code` beside `params.shape`; the entry's own `code` stays zod's - // issue code ('custom'). + // inside the metadata save door's `422 INVALID_METADATA` body, on an + // `issues[]` entry: `zodIssuesToMetadataIssues` carries the registered + // refusal there as `params.code` beside `params.shape`, while the body's + // `code` is `INVALID_METADATA` and the entry's own `code` stays zod's issue + // code ('custom'). The read path's `_diagnostics.errors[]` does not carry it. 'CONTROLLED_BY_PARENT_MASTER_INVALID', 'EXTERNAL_SCHEMA_MISMATCH', 'EXTERNAL_SCHEMA_MODE_VIOLATION', diff --git a/packages/spec/src/data/controlled-by-parent-master.ts b/packages/spec/src/data/controlled-by-parent-master.ts index 9f6843d876b..6dd155ec28f 100644 --- a/packages/spec/src/data/controlled-by-parent-master.ts +++ b/packages/spec/src/data/controlled-by-parent-master.ts @@ -120,8 +120,10 @@ export type ControlledByParentMasterShape = * `STACK_SCHEMA_INVALID` refusal, whose `issues` are the raw zod issues, and on * the metadata save door's `422 INVALID_METADATA`, whose `issues[]` entries * (`zodIssuesToMetadataIssues`, `@objectstack/metadata-protocol`) carry the - * issue's `params` beside `path`, `message` and zod's own issue `code` - * (`'custom'`): read the code there from `params.code`. + * registered refusal on `params` beside `path`, `message` and zod's own issue + * `code` (`'custom'`): read the code there from `params.code`. Where it is not: + * the read path's `_diagnostics.errors[]`, whose entries are exactly `path`, + * `message` and `code`. */ export const CONTROLLED_BY_PARENT_MASTER_INVALID = 'CONTROLLED_BY_PARENT_MASTER_INVALID' as const;