Skip to content

Commit 6d64e11

Browse files
committed
fix(metadata-protocol): diagnostics judge a picklist-bound field without the options the runtime resolved onto it
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5nFWpcGdLrSDAf6vS9
1 parent 5782008 commit 6d64e11

3 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎packages/metadata-protocol/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export {
164164
computeViewReferenceDiagnostics,
165165
decorateMetadataItem,
166166
decorateMetadataItems,
167+
isResolvedPicklistOptions,
168+
markResolvedPicklistOptions,
167169
} from './metadata-diagnostics.js';
168170
export type { MetadataDiagnostics } from './metadata-diagnostics.js';
169171

‎packages/metadata-protocol/src/metadata-diagnostics.ts‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,77 @@ function copyDiagnostics(verdict: MetadataDiagnostics): MetadataDiagnostics {
175175
return copy;
176176
}
177177

178+
// ---------------------------------------------------------------------------
179+
// [#26886] Options the runtime resolved from a shared picklist
180+
// ---------------------------------------------------------------------------
181+
//
182+
// A field authors `picklist: '<name>'` and no `options`; every object read
183+
// serves it with the list's options written beside `picklist` (the registry's
184+
// picklist resolution, `PicklistServedFieldSchema`). Diagnostics judge what the
185+
// author wrote, so the field schema's rule that refuses `picklist` and
186+
// `options` together must not read the copy the runtime wrote: it flagged
187+
// every picklist-bound field of every app.
188+
//
189+
// The resolution therefore MARKS the options array it derived
190+
// ({@link markResolvedPicklistOptions}), and only a marked array is left out
191+
// here. A field that authored its own `options` beside `picklist` arrives
192+
// unmarked and is still flagged, with the same message and severity. The mark
193+
// is a non-enumerable property, so no response, stored row or memo key ever
194+
// carries it, and it is keyed with `Symbol.for` so every loaded copy of this
195+
// package reads the one mark. A copy of the array (`.map`, a JSON round trip)
196+
// drops it, and the field is then judged with its `options`: the failure
197+
// direction is the old false error, never a missed one.
198+
199+
const RESOLVED_PICKLIST_OPTIONS = Symbol.for('@objectstack/metadata-protocol:resolved-picklist-options');
200+
201+
/**
202+
* Mark an `options` array as resolved from a shared picklist rather than
203+
* authored on the field. Returns the same array.
204+
*/
205+
export function markResolvedPicklistOptions<T extends object>(options: T): T {
206+
if (!Object.prototype.hasOwnProperty.call(options, RESOLVED_PICKLIST_OPTIONS)) {
207+
Object.defineProperty(options, RESOLVED_PICKLIST_OPTIONS, { value: true });
208+
}
209+
return options;
210+
}
211+
212+
/** `true` when `options` is an array {@link markResolvedPicklistOptions} marked. */
213+
export function isResolvedPicklistOptions(options: unknown): boolean {
214+
return Array.isArray(options)
215+
&& (options as unknown as Record<symbol, unknown>)[RESOLVED_PICKLIST_OPTIONS] === true;
216+
}
217+
218+
function withoutResolvedOptions(field: unknown): unknown {
219+
if (!field || typeof field !== 'object') return field;
220+
const { picklist, options } = field as { picklist?: unknown; options?: unknown };
221+
if (typeof picklist !== 'string' || !isResolvedPicklistOptions(options)) return field;
222+
const { options: _resolved, ...authored } = field as Record<string, unknown>;
223+
return authored;
224+
}
225+
226+
/**
227+
* A document with the resolved `options` left out of every picklist-bound
228+
* field, in both field spellings a served body carries (a name-keyed record,
229+
* an array). By reference when no field carries resolved options.
230+
*/
231+
function withoutResolvedPicklistOptions(document: unknown): unknown {
232+
if (!document || typeof document !== 'object') return document;
233+
const fields = (document as { fields?: unknown }).fields;
234+
if (!fields || typeof fields !== 'object') return document;
235+
if (Array.isArray(fields)) {
236+
const next = fields.map(withoutResolvedOptions);
237+
return next.some((f, i) => f !== fields[i]) ? { ...(document as object), fields: next } : document;
238+
}
239+
let next: Record<string, unknown> | undefined;
240+
for (const [name, field] of Object.entries(fields as Record<string, unknown>)) {
241+
const judged = withoutResolvedOptions(field);
242+
if (judged === field) continue;
243+
next ??= { ...(fields as Record<string, unknown>) };
244+
next[name] = judged;
245+
}
246+
return next ? { ...(document as object), fields: next } : document;
247+
}
248+
178249
/**
179250
* Compute spec diagnostics for a single metadata document.
180251
*
@@ -224,7 +295,10 @@ export function computeMetadataDiagnostics(
224295
// says. Same class as #6810 (`indexed`), different remedy — that key did
225296
// not belong on the served body at all and left at its injection site,
226297
// whereas this one is read by the UI and belongs on the response.
227-
const candidate = stripReadDecorations(item);
298+
// [#26886] …and judge each picklist-bound field as its author wrote it:
299+
// the `options` the runtime resolved onto it are not part of that shape
300+
// ({@link withoutResolvedPicklistOptions}).
301+
const candidate = withoutResolvedPicklistOptions(stripReadDecorations(item));
228302

229303
// [#25783] A document parsed before against this same schema is answered
230304
// from the memo — see "The verdict memo" above.

‎packages/objectql/src/picklist-resolution.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
* validator refuses every value of the field instead of accepting any.
3232
*/
3333

34+
// The mark that tells the load-time diagnostics these options were resolved
35+
// here, not authored: they judge what the author wrote, and the field schema
36+
// refuses `picklist` and `options` together.
37+
import { isResolvedPicklistOptions, markResolvedPicklistOptions } from '@objectstack/metadata-protocol';
38+
3439
/** One option, as `SelectOptionSchema` declares it (only `value` is read here). */
3540
export interface PicklistOptionLike {
3641
value: string | number | boolean;
@@ -133,7 +138,15 @@ function resolveField(field: unknown, resolve: (picklist: string) => readonly Pi
133138
const picklist = (field as { picklist?: unknown }).picklist;
134139
if (typeof picklist !== 'string' || picklist.length === 0) return field;
135140
const options = resolve(picklist);
136-
if (options) return { ...(field as object), options: options.map((o) => ({ ...o })) };
141+
if (options) {
142+
const resolved = options.map((o) => ({ ...o }));
143+
// Marked as derived unless the body brought `options` of its own: a field
144+
// that AUTHORS both keys is still told so. A body that is itself a served
145+
// copy (marked) stays marked.
146+
const carried = (field as { options?: unknown }).options;
147+
if (carried === undefined || isResolvedPicklistOptions(carried)) markResolvedPicklistOptions(resolved);
148+
return { ...(field as object), options: resolved };
149+
}
137150
// Unresolved: drop any options the body carried, so a stale served copy
138151
// cannot stand in for the list. The validator refuses every value of a
139152
// picklist-bound field that has none.

0 commit comments

Comments
 (0)