diff --git a/.changeset/36219-cel-editor-list-fields.md b/.changeset/36219-cel-editor-list-fields.md new file mode 100644 index 00000000000..8e1a2f35166 --- /dev/null +++ b/.changeset/36219-cel-editor-list-fields.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(studio): the CEL editor refuses `==` / `!=` on a list field while you type, not first on save + +Clause-②: no +Metadata-protocol: none + +The object designer's CEL editors (a field's formula and stored expression, its visible / read-only / required conditions, and the condition builder's CEL box) now hand the as-you-type check each field's type and `multiple`. Writing `record.labels == 'a'` against a multi-select, tags or checkboxes field, or a select, lookup or user field declared `multiple: true`, shows the `list-equality` refusal and its `in` rewrite under the editor and holds Save, the same verdict the object draft check and publish already gave. A scalar field is judged as before. `CelPredicateField` takes a new optional `fieldDefs` prop; a mount that omits it lints exactly as before. diff --git a/ui/packages/app-shell/src/views/metadata-admin/CelPredicateField.tsx b/ui/packages/app-shell/src/views/metadata-admin/CelPredicateField.tsx index 4481df6f4a7..ef57a245261 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/CelPredicateField.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/CelPredicateField.tsx @@ -75,6 +75,9 @@ import { type CelValueType, } from './celAuthoring.js'; import { useServerHoldAt, type ServerDiagnosticEntry } from './draftVerdict.js'; +// The draft check's own derivation, so the editor and the save judge the same +// fields as lists (`celAuthoring.ts` stays free of any import at load). +import { multiValueFieldsOf, type CelFieldDef } from './clientValidation.js'; const LINT_DEBOUNCE_MS = 250; @@ -110,6 +113,15 @@ export interface CelPredicateFieldProps { * `record.` in `record` scope, the bare name otherwise. */ fieldOptions?: ReadonlyArray<{ name: string; label: string }>; + /** + * The object's field definitions — each field's `type` and `multiple`. The + * editor reads them through `isMultiValueField` (`@objectstack/spec/data`), + * as the object draft check does, and hands the list-valued ones to the lint + * as `multiValueFields`, so `record. == 'a'` is refused + * (`list-equality`) while the author types, not first on save. Omitted: no + * field is judged a list, the hint is exactly what it was before this prop. + */ + fieldDefs?: ReadonlyArray; /** Which RLS clause this is (turns on the blocking verdicts of `@objectstack/lint`'s RLS rule). */ clause?: 'using' | 'check'; /** @@ -208,6 +220,7 @@ export function CelPredicateField({ objectName, fieldNames = EMPTY_FIELDS, fieldOptions, + fieldDefs, clause, scope, roots, @@ -257,6 +270,8 @@ export function CelPredicateField({ // Stable deps for the identifier arrays (parent may pass fresh arrays each render). const fieldsKey = fieldNames.join(','); + const multiValueFields = React.useMemo(() => (fieldDefs ? multiValueFieldsOf(fieldDefs) : undefined), [fieldDefs]); + const multiKey = multiValueFields?.join(','); const rootsKey = roots ? roots.join(',') : ''; // objectstack#27482 — the server's errors at this editor's path while the @@ -299,7 +314,7 @@ export function CelPredicateField({ React.useEffect(() => { let cancelled = false; const handle = setTimeout(() => { - const hint = { objectName, fields: fieldNames, clause, scope, slot, role }; + const hint = { objectName, fields: fieldNames, multiValueFields, clause, scope, slot, role }; lintCelPredicate(value, hint).then((res) => { if (cancelled) return; setIssues(res); @@ -319,7 +334,7 @@ export function CelPredicateField({ clearTimeout(handle); }; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [value, objectName, fieldsKey, clause, scope, slot, role]); + }, [value, objectName, fieldsKey, multiKey, clause, scope, slot, role]); /* Report "clean" upward when the field empties (no debounce needed). */ React.useEffect(() => { diff --git a/ui/packages/app-shell/src/views/metadata-admin/clientValidation.ts b/ui/packages/app-shell/src/views/metadata-admin/clientValidation.ts index 3e6385d651b..2f28b04c7b7 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/clientValidation.ts +++ b/ui/packages/app-shell/src/views/metadata-admin/clientValidation.ts @@ -816,16 +816,29 @@ function predicateSource(v: unknown): string | null { return null; } +/** One field definition {@link multiValueFieldsOf} reads: its name, `type` and `multiple`. */ +export interface CelFieldDef { + name: string; + type?: unknown; + multiple?: unknown; +} + /** - * [#36167] The draft's fields whose stored value is a list, read through + * [#36167] The fields whose stored value is a list, read through * `isMultiValueField` (`@objectstack/spec/data`) — the `multiValueFields` the * CEL lint hands the engine, so `record. == 'a'` on a multi-value - * field is refused here as the publish lint refuses it. + * field is refused here as the publish lint refuses it. The ONE derivation: + * the draft check below and `CelPredicateField`'s as-you-type lint both call + * it, so the editor and the save judge the same fields as lists. */ -function multiValueFieldsOf(view: FieldsView): string[] { - return view.entries - .filter((e) => typeof e.def.type === 'string' && isMultiValueField({ type: e.def.type, multiple: e.def.multiple === true })) - .map((e) => e.name); +export function multiValueFieldsOf(defs: ReadonlyArray): string[] { + return defs + .filter((d) => typeof d.type === 'string' && isMultiValueField({ type: d.type, multiple: d.multiple === true })) + .map((d) => d.name); +} + +function multiValueFieldsOfView(view: FieldsView): string[] { + return multiValueFieldsOf(view.entries.map((e) => ({ name: e.name, type: e.def.type, multiple: e.def.multiple }))); } /** @@ -841,7 +854,7 @@ async function validateObjectFieldRules(draft: unknown): Promise e.name); - const multiValueFields = multiValueFieldsOf(view); + const multiValueFields = multiValueFieldsOfView(view); const issues: SchemaFormIssue[] = []; for (let i = 0; i < view.entries.length; i++) { const entry = view.entries[i]; @@ -963,7 +976,7 @@ async function validateObjectValidationRules(draft: unknown): Promise e.name); - const multiValueFields = multiValueFieldsOf(view); + const multiValueFields = multiValueFieldsOfView(view); const issues: SchemaFormIssue[] = []; const visit = async (rule: unknown, path: string, depth: number): Promise => { diff --git a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ConditionBuilder.tsx b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ConditionBuilder.tsx index 3039de70b02..d33651fbe6e 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ConditionBuilder.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ConditionBuilder.tsx @@ -1241,6 +1241,9 @@ export function ConditionBuilder({ label, value, onCommit, objectName, fields: f placeholder={celExampleForRoots(offeredRoots)} objectName={objectName} fieldNames={fieldNames} + /* Each field's type and `multiple`, hidden ones too, so a list + field read with `==` / `!=` is refused as the save refuses it. */ + fieldDefs={fields} /* Forwarded verbatim, `undefined` included — see the `scope` prop's own note. An omitted scope must reach `celAuthoring` as absent so its `hint.scope ?? 'flattened'` default answers unchanged. */ diff --git a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.listEquality-36219.test.tsx b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.listEquality-36219.test.tsx new file mode 100644 index 00000000000..817fee686ec --- /dev/null +++ b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.listEquality-36219.test.tsx @@ -0,0 +1,83 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#36219] The field inspector's CEL editors hand the lint the object's field + * types and `multiple`, so `record. == 'a'` — a list compared with + * a scalar, one constant on every row — is refused (`list-equality`) while the + * author types, as the object draft check refuses it on save. A `select` + * declared `multiple: true` is the list subject; a plain `select` is the + * scalar control. Against the REAL `@objectstack/formula` engine. + */ + +import '@testing-library/jest-dom/vitest'; +import * as React from 'react'; +import { describe, it, expect, vi, afterEach } from 'vitest'; +import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react'; +// The CEL authoring helpers load these through dynamic imports; paying the first +// load at module top keeps it out of every clocked window (`check:test-source-alias`). +import '@objectstack/formula'; +import '@objectstack/lint'; + +vi.mock('../useMetadata', () => ({ + useMetadataClient: () => ({ + list: vi.fn().mockResolvedValue([]), + listDrafts: vi.fn().mockResolvedValue([]), + }), +})); + +vi.mock('../previews/useObjectFields', () => ({ + useObjectFields: () => ({ fields: [], loading: false, error: null }), +})); + +import { ObjectFieldInspector } from './ObjectFieldInspector'; + +afterEach(cleanup); + +const OPTIONS = [ + { label: 'Frontend', value: 'frontend' }, + { label: 'QA', value: 'qa' }, +]; + +function Harness({ onBlockingIssuesChange }: { onBlockingIssuesChange: (count: number) => void }) { + const [fields, setFields] = React.useState>>({ + stage: { type: 'select', label: 'Stage', options: OPTIONS, multiple: true }, + single: { type: 'select', label: 'Single', options: OPTIONS }, + note: { type: 'text', label: 'Note' }, + }); + return ( + ) => setFields(patch.fields as typeof fields)} + onClearSelection={() => {}} + onSelectionChange={() => {}} + readOnly={false} + locale={'en-US'} + onBlockingIssuesChange={onBlockingIssuesChange} + /> + ); +} + +function typeVisibleWhen(source: string) { + const report = vi.fn(); + render(); + const box = screen.getByText('Visible when').parentElement!.querySelector('textarea') as HTMLElement; + fireEvent.change(box, { target: { value: source } }); + return () => report.mock.calls.at(-1)?.[0] as number | undefined; +} + +describe('ObjectFieldInspector — list-equality while typing (#36219)', () => { + it('refuses == on a select declared multiple: true, and holds Save', async () => { + const current = typeVisibleWhen("record.stage == 'qa'"); + await waitFor(() => expect(current()).toBe(1), { timeout: 5000 }); + expect(await screen.findByText(/`record\.stage` holds a list/)).toBeInTheDocument(); + }); + + it('CONTROL — the same comparison on a scalar select is clean', async () => { + const current = typeVisibleWhen("record.single == 'qa'"); + await waitFor(() => expect(current()).toBe(0), { timeout: 5000 }); + expect(screen.queryByText(/holds a list/)).toBeNull(); + }); +}); diff --git a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx index e3be5ea4fbd..dcb5525ed41 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx @@ -1557,6 +1557,9 @@ export function ObjectFieldInspector({ // The siblings an expression may read: every field but this one (a field // computed from itself is circular), named as every other surface names it. const siblingFieldNames = view.entries.map((e) => e.name).filter((n) => n !== entry.name); + // Each field's type and `multiple`, so every CEL editor below judges a + // list-valued field as the object draft check does (`list-equality`). + const celFieldDefs = view.entries.map((e) => ({ name: e.name, type: e.def.type, multiple: e.def.multiple })); const siblingFieldOptions = view.entries .filter((e) => e.name !== entry.name) .map((e) => { @@ -1832,6 +1835,7 @@ export function ObjectFieldInspector({ objectName={typeof (draft as any).name === 'string' ? ((draft as any).name as string) : undefined} // A formula may reference every sibling, not itself (circular). fieldNames={siblingFieldNames} + fieldDefs={celFieldDefs} fieldOptions={siblingFieldOptions} scope="record" roots={FORMULA_ROOTS} @@ -1964,6 +1968,7 @@ export function ObjectFieldInspector({ // The engine computes it as it computes a formula, over the // row the write stores: `record` is the one root it binds. fieldNames={siblingFieldNames} + fieldDefs={celFieldDefs} fieldOptions={siblingFieldOptions} scope="record" roots={FORMULA_ROOTS} @@ -2030,6 +2035,7 @@ export function ObjectFieldInspector({ placeholder="record.status != 'draft'" objectName={typeof (draft as any).name === 'string' ? ((draft as any).name as string) : undefined} fieldNames={view.entries.map((e) => e.name)} + fieldDefs={celFieldDefs} scope="record" roots={FIELD_RULE_ROOTS} // The authored key, so the wrong-layer advisory reads the @@ -2081,6 +2087,7 @@ export function ObjectFieldInspector({ placeholder="record.status == 'closed'" objectName={typeof (draft as any).name === 'string' ? ((draft as any).name as string) : undefined} fieldNames={view.entries.map((e) => e.name)} + fieldDefs={celFieldDefs} scope="record" roots={FIELD_RULE_ROOTS} // The authored key, so the wrong-layer advisory reads the @@ -2101,6 +2108,7 @@ export function ObjectFieldInspector({ placeholder="record.amount > 10000" objectName={typeof (draft as any).name === 'string' ? ((draft as any).name as string) : undefined} fieldNames={view.entries.map((e) => e.name)} + fieldDefs={celFieldDefs} scope="record" roots={FIELD_RULE_ROOTS} // The authored key, so the wrong-layer advisory reads the