Skip to content

Commit 15c27dc

Browse files
committed
fix(core): leave a [field, op, value] tuple's comparand to the doors that judged it
The token pass walked the value position of a legacy tuple as a filter node, so ['attachment', '=', { meta: { $record: 'x' } }] was refused where the same deep-equality literal in the object spelling comes back by reference. An array whose first member is a string other than and / or is a comparison: its third member is a comparand, not a node. CONTROL pins for the tuple spelling; the refusal's $record echo is bounded to 60 characters; the describe and the changeset name the time column the temporal door also covers. Claude-Session: https://claude.ai/code/session_01FEkQ5kgF3mo9f7LXuv5hac Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1d58fb8 commit 15c27dc

4 files changed

Lines changed: 58 additions & 11 deletions

File tree

‎.changeset/34199-record-reference-refused-by-name.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A `lookupFilters` entry may read the record being edited as `{ $record: "field"
1212

1313
`A { "$record": "account.cb_market" } comparand on field "cb_market" at filter.cb_market reached the server unresolved, and is refused rather than compared.`
1414

15-
- Before: the eq shorthand `{ cb_market: { $record: "account.cb_market" } }` reached the driver, which refused it in its own words: driver-memory as an unsupported operator `"$record"`, driver-sql with the operator and field withheld. Both were already `INVALID_FILTER` / 400 (driver-mongodb refuses an unknown operator the same way), so on the built-in drivers no filter that answered rows now refuses.
16-
- Now: that shape is refused by name before any driver is asked, on every engine verb and in the analytics service, whether or not the filter also holds a string placeholder. On the engine's object path the other five operators (`{ valid_from: { $lte: { $record: "…" } } }`) and the eq shorthand on a date or datetime column are still refused one door earlier, in that door's own words, as before.
17-
- Unchanged: a `{ $record }` deeper inside a comparand, such as `{ attachment: { meta: { $record: "x" } } }`, is a deep-equality literal (or a nested-relation condition) and passes through as before; driver-memory still compares it whole.
15+
- Before: the eq shorthand `{ cb_market: { $record: "account.cb_market" } }` reached the driver, which refused it in its own words: driver-memory as an unsupported operator `"$record"`, driver-sql with the operator and field withheld. Both were already `INVALID_FILTER` / 400 (driver-mongodb refuses an unknown operator the same way), so on the built-in drivers no filter that answered rows now refuses: the pass judges only a field's comparand, never a value inside one (see Unchanged).
16+
- Now: that shape is refused by name before any driver is asked, on every engine verb and in the analytics service, whether or not the filter also holds a string placeholder. On the engine's object path the other five operators (`{ valid_from: { $lte: { $record: "…" } } }`) and the eq shorthand on a date, datetime or time column are still refused one door earlier, in that door's own words, as before. The engine lowers a `[field, op, value]` array `where` before the pass, so there the array spelling is judged as its object form; the analytics service hands the pass its `where` unlowered, and the pass leaves a tuple to the doors that judged it before.
17+
- Unchanged: a `{ $record }` deeper inside a comparand is a deep-equality literal (or a nested-relation condition) and passes through as before, in either spelling: the object form `{ attachment: { meta: { $record: "x" } } }`, which driver-memory still compares whole, and the tuple `['attachment', '=', { meta: { $record: "x" } }]`, whose third member is a comparand, never a filter node. Both come back from the pass by reference (pinned as CONTROLs on `resolveFilterTokens`).
1818

19-
The `lookupFilters` describes now say what holds on every path: an unresolved `{ $record }` is refused with `INVALID_FILTER`, never read as null, and by name on the eq shorthand of a column that is not a date or datetime. The `[PLANNED — not enforced]` marker stays on the picker half, which does not resolve `{ $record }` or read `orEmpty` yet.
19+
The `lookupFilters` describes now say what holds on every path: an unresolved `{ $record }` is refused with `INVALID_FILTER`, never read as null, and by name on the eq shorthand of a column other than a date, datetime or time column. The `[PLANNED — not enforced]` marker stays on the picker half, which does not resolve `{ $record }` or read `orEmpty` yet.

‎packages/core/src/utils/filter-tokens.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ describe('[#34199] resolveFilterTokens — an unresolved { $record } comparand r
292292
expect(err.message).toContain(`on field "${field}" at ${at} reached the server unresolved`);
293293
});
294294

295+
it('walks an or-array from its second member: an object node there is still judged', () => {
296+
const err = refusal(['or', ['stage', '=', 'won'], { cb_market: { $record: 'account.cb_market' } }]);
297+
expect(err.code).toBe('INVALID_FILTER');
298+
expect(err.status).toBe(400);
299+
expect(err.message).toContain('on field "cb_market" at filter[2].cb_market reached');
300+
});
301+
302+
it('bounds the echoed $record value, which came off the wire', () => {
303+
const err = refusal({ cb_market: { $record: 'x'.repeat(500) } });
304+
expect(err.code).toBe('INVALID_FILTER');
305+
expect(err.status).toBe(400);
306+
expect(err.message).not.toContain('x'.repeat(60));
307+
expect(err.message).toContain('on field "cb_market" at filter.cb_market reached');
308+
});
309+
295310
it('refuses a malformed reference by its key too', () => {
296311
const malformed = refusal({ cb_market: { $record: 'account.cb_market', addDays: 1 } });
297312
expect(malformed.code).toBe('INVALID_FILTER');
@@ -309,6 +324,12 @@ describe('[#34199] resolveFilterTokens — an unresolved { $record } comparand r
309324
['as an $in list member', { region: { $in: ['NA', 'EU', { $record: 'account.region' }] } }],
310325
['at the root, where no field names it', { $record: 'account.cb_market' }],
311326
['as the third member of a [field, op, value] tuple', ['cb_market', '=', { $record: 'account.cb_market' }]],
327+
// The tuple's value is a comparand: the array spelling of the deep-equality
328+
// CONTROL above is the same literal, and the analytics service hands the
329+
// unlowered array to this pass.
330+
['inside a deep-equality comparand in the array spelling', ['attachment', '=', { meta: { $record: 'x' } }]],
331+
['inside a tuple under an and-array', ['and', ['stage', '=', 'won'], ['attachment', '=', { meta: { $record: 'x' } }]]],
332+
['inside a tuple of a legacy list of tuples', [['stage', '=', 'won'], ['attachment', 'in', [{ meta: { $record: 'x' } }]]]],
312333
])('control — %s: not a field\'s comparand, so it comes back by reference', (_label, filter) => {
313334
expect(resolveFilterTokens(filter, full)).toBe(filter);
314335
});

‎packages/core/src/utils/filter-tokens.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,16 +637,27 @@ interface RecordReferenceComparand {
637637
* - **A `$` key in a node position that is not `$and` / `$or` / `$not`**
638638
* (`{ $record: 'x' }` at the root): it is not a field's comparand, and the
639639
* drivers already refuse an undeclared node key.
640-
* - **An array where a node belongs** is walked as a list of nodes, so the
641-
* legacy `[field, op, value]` tuple is never read as one: its third member is
642-
* a node to this walk, and a `{ $record }` node names no field.
640+
* - **A legacy `[field, op, value]` tuple** (an array whose first member is a
641+
* string other than `and` / `or`, the split `parseFilterAST` lowers by):
642+
* it is a comparison, not a list of nodes, and its third member is a
643+
* comparand — `['attachment', '=', { meta: { $record: 'x' } }]` is the same
644+
* deep-equality literal as the object spelling above, and comes back by
645+
* reference like it. An `['and' | 'or', …]` array is walked from its second
646+
* member, and any other array as a list of nodes.
643647
*
644648
* Allocation-free and shallow (it never enters a comparand), so it costs a
645649
* read whose filter holds no reference one walk of its node keys.
646650
*/
647651
function findRecordReferenceComparand(node: unknown, at = 'filter'): RecordReferenceComparand | undefined {
648652
if (Array.isArray(node)) {
649-
for (let i = 0; i < node.length; i++) {
653+
let start = 0;
654+
if (typeof node[0] === 'string') {
655+
const head = node[0].toLowerCase();
656+
// A `[field, op, value]` tuple: a comparison whose value is a comparand — not judged.
657+
if (head !== 'and' && head !== 'or') return undefined;
658+
start = 1;
659+
}
660+
for (let i = start; i < node.length; i++) {
650661
const hit = findRecordReferenceComparand(node[i], `${at}[${i}]`);
651662
if (hit) return hit;
652663
}
@@ -697,7 +708,7 @@ class UnresolvedRecordReferenceError extends Error {
697708

698709
constructor({ reference, field, at }: RecordReferenceComparand) {
699710
super(
700-
`A { "$record": ${JSON.stringify(reference.$record)} } comparand on field "${field}" at ${at} ` +
711+
`A { "$record": ${previewReferencePath(reference.$record)} } comparand on field "${field}" at ${at} ` +
701712
'reached the server unresolved, and is refused rather than compared. { $record } reads the record ' +
702713
'being edited (a field of it, or one lookup hop such as "account.cb_market"), and only the record ' +
703714
'picker of the form editing that record can resolve it: the picker sends the referenced value in ' +
@@ -710,6 +721,21 @@ class UnresolvedRecordReferenceError extends Error {
710721
}
711722
}
712723

724+
/**
725+
* [#34199] The `$record` value as the refusal echoes it: JSON, bounded to 60
726+
* characters like the sibling comparand doors' previews — a malformed
727+
* reference came off the wire and can carry any JSON value.
728+
*/
729+
function previewReferencePath(path: unknown): string {
730+
let text: string;
731+
try {
732+
text = JSON.stringify(path) ?? String(path);
733+
} catch {
734+
text = String(path);
735+
}
736+
return text.length > 60 ? `${text.slice(0, 59)}…` : text;
737+
}
738+
713739
/** [#25808] The anchor with its instant pinned — see {@link isUnresolvedNowAnchor}. */
714740
function resolveNowAnchor(node: { $now: true; addDays?: unknown }, now: Date): Record<string, unknown> {
715741
const offset = node.addDays;

‎packages/spec/src/data/field.zod.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,8 +2271,8 @@ export const FieldSchema = lazySchema(() => {
22712271
+ 'empty, and on a picker with no single record being edited (a bulk-action bar, a filter panel), the entry is left '
22722272
+ 'out. A { $record } is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A { $record } that reaches a '
22732273
+ 'server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when '
2274-
+ 'it arrives as the eq shorthand on a column that is not a date or datetime; any other operator, and eq on a date '
2275-
+ 'or datetime column, is refused earlier, in generic words. The '
2274+
+ 'it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and '
2275+
+ 'eq on a date, datetime or time column, is refused earlier, in generic words. The '
22762276
+ 'picker narrows the choice only: a rule the save must enforce belongs on the server too. [PLANNED — not '
22772277
+ 'enforced] The picker does not resolve { $record } or read orEmpty yet.',
22782278
),

0 commit comments

Comments
 (0)