Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/36219-cel-editor-list-fields.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -110,6 +113,15 @@ export interface CelPredicateFieldProps {
* `record.<name>` 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.<list field> == '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<CelFieldDef>;
/** Which RLS clause this is (turns on the blocking verdicts of `@objectstack/lint`'s RLS rule). */
clause?: 'using' | 'check';
/**
Expand Down Expand Up @@ -208,6 +220,7 @@ export function CelPredicateField({
objectName,
fieldNames = EMPTY_FIELDS,
fieldOptions,
fieldDefs,
clause,
scope,
roots,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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(() => {
Expand Down
29 changes: 21 additions & 8 deletions ui/packages/app-shell/src/views/metadata-admin/clientValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.<field> == '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<CelFieldDef>): 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 })));
}

/**
Expand All @@ -841,7 +854,7 @@ async function validateObjectFieldRules(draft: unknown): Promise<SchemaFormIssue
if (view.entries.length === 0) return [];
const objectName = typeof d?.name === 'string' ? d.name : undefined;
const fieldNames = view.entries.map((e) => 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];
Expand Down Expand Up @@ -963,7 +976,7 @@ async function validateObjectValidationRules(draft: unknown): Promise<SchemaForm
const objectName = typeof d?.name === 'string' ? d.name : undefined;
const view = readFields(d?.fields);
const fieldNames = view.entries.map((e) => e.name);
const multiValueFields = multiValueFieldsOf(view);
const multiValueFields = multiValueFieldsOfView(view);
const issues: SchemaFormIssue[] = [];

const visit = async (rule: unknown, path: string, depth: number): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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.<list field> == '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<Record<string, Record<string, unknown>>>({
stage: { type: 'select', label: 'Stage', options: OPTIONS, multiple: true },
single: { type: 'select', label: 'Single', options: OPTIONS },
note: { type: 'text', label: 'Note' },
});
return (
<ObjectFieldInspector
type="object"
name="ticket"
draft={{ name: 'ticket', fields }}
selection={{ kind: 'field', id: 'note' }}
onPatch={(patch: Record<string, unknown>) => setFields(patch.fields as typeof fields)}
onClearSelection={() => {}}
onSelectionChange={() => {}}
readOnly={false}
locale={'en-US'}
onBlockingIssuesChange={onBlockingIssuesChange}
/>
);
}

function typeVisibleWhen(source: string) {
const report = vi.fn();
render(<Harness onBlockingIssuesChange={report} />);
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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading