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
18 changes: 18 additions & 0 deletions .changeset/34378-lookup-filter-record-path-multi-valued.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
---

feat(lint)!: a `lookupFilters` `{ $record: "path" }` path through or to a multi-valued field is refused

Clause-②: no (narrowing) — a `lookupFilters[].value` `{ $record }` path on a lookup, master_detail or user field whose first segment is a multi-valued field of the object owning that field (a plain path to it, or a hop through it), or whose leaf behind a single-valued hop is a multi-valued field, linted clean and published; it is now refused at error as `object-field-ref-unknown` by `os lint`, `os validate`, `os build` and the runtime publish door, and reported (never refused) on the package publish receipt's object probe.
Metadata-protocol: narrowed
Skills-docs: follow-up #34201 — no doc or skill teaches `{ $record }` yet, so there is no teaching to correct here; the multi-valued clause rides the describes this change edits.

<!-- adr-0087: registered lookup-filter-record-path-multi-valued-refused -->

**BREAKING** only for a `{ $record }` path that reads a list. A `{ $record }` comparand stands for ONE value of the record being edited and pairs only with `eq`, `ne`, `gt`, `lt`, `gte` or `lte`, but the path check judged only that each segment names a field, so `{ $record: "accounts.cb_market" }` through a `multiple: true` lookup, or `{ $record: "tags" }`, linted clean. A hop through a multi-valued lookup reaches no single row, so the entry was left out without a word; a plain path to a multi-valued field put a list where one value goes. `object-field-ref-unknown` now refuses the path at the multi-valued field and names it. Multi-valued means `isMultiValueField`: a field flagged `multiple: true`, or a `multiselect`, `checkboxes` or `tags` field. A registry-injected column and a leaf on an object outside the stack are not judged. Measured first-party exposure: zero — no example app, platform object, skill or docs sample writes a `$record` path.

**FROM → TO.** FROM `{ $record: "accounts.cb_market" }` (a hop through a `multiple: true` lookup), `{ $record: "tags" }` or `{ $record: "account.markets" }` (a multi-valued leaf) TO a path that ends at, and hops through, single-valued fields only.

**Fix:** copy the value you mean onto a single-valued field of the record being edited (or of the record one single-valued hop away) and reference that. The semantic entry `lookup-filter-record-path-multi-valued-refused` carries the same notice into the upgrade guide and `os migrate meta`. `os explain object-field-ref-unknown` lists the position.
4 changes: 2 additions & 2 deletions content/docs/references/data/field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ const result = CurrencyConfigSchema.parse(data);
| :--- | :--- | :--- | :--- |
| **field** | `string` | ✅ | |
| **operator** | `Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>` | ✅ | |
| **value** | `any` | ✅ | The comparand: a literal (an array for `in` / `notIn`), a date placeholder such as `{today}` or a `{ $now: true, addDays }` comparand for a date relative to today, or a `{ $record: "path" }` reference to the record being edited — a field of it, or one lookup hop ("account.cb_market"); a `{ $record }` value pairs only with eq, ne, gt, lt, gte or lte. In a record form, whatever its layout, the picker sends the referenced value in place of the reference; every other picker host leaves the entry out. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null. |
| **value** | `any` | ✅ | The comparand: a literal (an array for `in` / `notIn`), a date placeholder such as `{today}` or a `{ $now: true, addDays }` comparand for a date relative to today, or a `{ $record: "path" }` reference to the record being edited — a field of it, or one lookup hop ("account.cb_market"); a `{ $record }` value pairs only with eq, ne, gt, lt, gte or lte. In a record form, whatever its layout, the picker sends the referenced value in place of the reference; every other picker host leaves the entry out. A `{ $record }` path through or to a multi-valued field (one flagged `multiple: true`, or a multiselect, checkboxes or tags field) has no single value: it is not resolved, and lint refuses it (`object-field-ref-unknown`). A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null. |
| **orEmpty** | `boolean` | optional | TRUE: the entry also matches a candidate whose column is null — "valid from the quote date, or with no start date". Only with eq, ne, gt, lt, gte or lte. The picker sends the comparison or a null match on the same column. |

### Nested Shape: `Field.lookupAutofill[number]`
Expand Down Expand Up @@ -437,7 +437,7 @@ Allowed Values: `phone`, `id_card`, `bank_account`, `email`, `name`

## LookupFilterRecordReference

A lookupFilters comparand read from the record being edited: `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). The picker replaces it with that value before querying; unlike `{ $field }`, which names a column of the candidate row.
A lookupFilters comparand read from the record being edited: `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). The picker replaces it with that value before querying; unlike `{ $field }`, which names a column of the candidate row. A path through or to a multi-valued field (one flagged `multiple: true`, or a multiselect, checkboxes or tags field) has no single value: it is not resolved, and lint refuses it (`object-field-ref-unknown`).

### Properties

Expand Down
5 changes: 4 additions & 1 deletion packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4499,7 +4499,10 @@ const OBJECT_FIELD_REF_UNKNOWN_EXPLANATION: RuleExplanation = {
'an entry out while the value is empty, so a path the record does not have would drop the filter ' +
'without a word. A miss names the segment: an unknown head, ' +
'a hop through a field that is not a relationship, a leaf the referenced object lacks, or a ' +
'second hop. A `$record` nested inside a literal value is not a reference and is not judged.',
'second hop. A path that names real fields is refused too when it reads a list: a path through or ' +
'to a multi-valued field (one flagged `multiple: true`, or a `multiselect`, `checkboxes` or `tags` ' +
'field), or a multi-valued leaf behind one hop, because the comparand is one ' +
'value under a scalar operator. A `$record` nested inside a literal value is not a reference and is not judged.',
'A summary field\'s `summaryOperations.filter` is judged against the CHILD object ' +
'`summaryOperations.object` names: the engine ANDs it with the parent-FK match and runs it as the ' +
'query on that child, so every key (inside `$and` / `$or` / `$not` groups) is a field of the child. ' +
Expand Down
75 changes: 75 additions & 0 deletions packages/lint/src/validate-object-field-refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ describe('validateObjectFieldRefs — lookupFilters[].value.$record (the RECORD
name: { type: 'text' },
cb_market: { type: 'select' },
parent: { type: 'lookup', reference: 'crm_account' },
markets: { type: 'multiselect' },
},
},
{
Expand Down Expand Up @@ -855,6 +856,79 @@ describe('validateObjectFieldRefs — lookupFilters[].value.$record (the RECORD
}));
expect(findings.map((f) => f.path)).toEqual(['objects[0].fields.approver.lookupFilters[0].value.$record']);
});

// [#34378] A path that names real fields can still read a LIST. The
// comparand is one value under a scalar operator, so a multi-valued head
// (a plain path to it, or a hop through it) or a multi-valued leaf behind a
// single-valued hop is refused — by `isMultiValueField`, `FieldSchema`'s own
// predicate: an inherently multi type, or `multiple: true` on a
// multi-capable one.
describe('a path through or to a multi-valued field (#34378)', () => {
const multi = {
tags: { type: 'tags' },
regions: { type: 'select', multiple: true },
stage: { type: 'select', multiple: false },
accounts: { type: 'lookup', reference: 'crm_account', multiple: true },
partners: { type: 'lookup', reference: 'elsewhere_partner', multiple: true },
watchers: { type: 'user', multiple: true },
};
const refused = (path: string) => {
const findings = validateObjectFieldRefs(quote([entry(path)], multi));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]).toMatchObject({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN });
expect(findings[0]!.hint).toContain('Point `$record` at a single-valued field');
return findings[0]!.message;
};

it('REFUSES a plain path to an inherently multi-valued field (`tags`)', () => {
expect(refused('tags')).toBe(
'fields.price_book.lookupFilters[0].value.$record "tags" names "tags", a multi-valued field on object '
+ '"sales_quote"; a $record comparand is one value under a scalar operator.',
);
});

it('REFUSES a plain path to a `multiple: true` select', () => {
expect(refused('regions')).toContain('names "regions", a multi-valued field on object "sales_quote"');
});

it('REFUSES a hop through a `multiple: true` lookup, at the hop', () => {
expect(refused('accounts.cb_market')).toBe(
'fields.price_book.lookupFilters[0].value.$record "accounts.cb_market" goes through "accounts", a '
+ 'multi-valued field on object "sales_quote"; a $record comparand is one value under a scalar operator.',
);
});

it('REFUSES a hop through a `multiple: true` user field, its target unjudged past the hop', () => {
expect(refused('watchers.department')).toContain('goes through "watchers", a multi-valued field');
});

it('REFUSES a hop through a multi-valued lookup to an object outside the stack: the head is this object\'s', () => {
expect(refused('partners.anything')).toContain('goes through "partners", a multi-valued field');
});

it('REFUSES a multi-valued leaf behind a single-valued hop, naming the leaf and its object', () => {
expect(refused('account.markets')).toBe(
'fields.price_book.lookupFilters[0].value.$record "account.markets" ends at "markets", a multi-valued '
+ 'field on object "crm_account"; a $record comparand is one value under a scalar operator.',
);
});

it('reports a misspelt leaf behind a multi-valued hop once, as the miss', () => {
const findings = validateObjectFieldRefs(quote([entry('accounts.cb_markt')], multi));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]!.message).toContain('"accounts.cb_markt" is not a field on object "crm_account"');
});

it('passes single-valued paths: a plain field, a single lookup, a hop, a `multiple: false` select', () => {
expect(validateObjectFieldRefs(quote([
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' } },
entry('account', 'cb_market'),
entry('account.cb_market'),
entry('stage'),
entry('account.created_at', 'valid_to'),
], multi))).toEqual([]);
});
});
});

describe('validateObjectFieldRefs — summaryOperations.filter (addresses the CHILD object, #31224)', () => {
Expand Down Expand Up @@ -1184,6 +1258,7 @@ describe('[#22161] one-line verdicts — object-field-ref-unknown', () => {
'`lookupColumns` and `lookupFilters[].field` against the REFERENCED object',
'`lookupFilters[].value.$record`',
'at most one relationship hop',
'a path through or to a multi-valued field',
'`lookup`, `master_detail` and `user`',
'reported once, there',
'read verbatim',
Expand Down
66 changes: 64 additions & 2 deletions packages/lint/src/validate-object-field-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@
* types alone, the picker being its only reader. A path of two hops is
* refused at the second hop on the raw `os lint` path the parse has not
* seen; a hop's leaf on an object outside this stack stays unjudged, as
* every cross-package name in this family does. A `{ $record }` nested
* every cross-package name in this family does. A path through or to a
* MULTI-VALUED field (#34378) — a hop through a `multiple: true` lookup
* (`accounts.cb_market`), a plain path to one (`tags`), or a leaf that is
* one behind a single-valued hop — is refused too: the comparand is ONE
* value under a scalar operator, and a list has no such value. The
* predicate is `isMultiValueField` (`@objectstack/spec/data`), the one
* `FieldSchema` reads. A registry-injected column, and a leaf on an object
* outside this stack, stay unjudged. A `{ $record }` nested
* deeper inside a literal (`value: { any: { $record: 'x' } }`) is an
* opaque literal to the parse, and this rule does not name it either: no
* reader resolves that shape, nothing first-party writes it, and the
Expand Down Expand Up @@ -286,9 +293,10 @@ import {
recordsOf,
resolveFieldPath,
type FieldPathVerdict,
type GraphField,
type ObjectGraph,
} from './object-graph.js';
import { classifyDottedFilterHead, isVirtualSearchField } from '@objectstack/spec/data';
import { classifyDottedFilterHead, isMultiValueField, isVirtualSearchField } from '@objectstack/spec/data';

/**
* A field-name list entry — object-level, or on one of the object's fields —
Expand Down Expand Up @@ -478,6 +486,18 @@ interface FieldNameSlot {
* different objects.
*/
ownFinding?: boolean;
/**
* [#34378] `true` when the path must read ONE value: its first segment and,
* behind a hop, its leaf may not be multi-valued (`isMultiValueField`). A
* path whose head is multi-valued is refused whether it ends there or hops
* through it, at the head; a multi-valued leaf behind a single-valued hop is
* refused at the leaf. Judged only on a path the family's resolution did
* not already refuse, and only on AUTHORED fields: a registry-injected
* column, or a leaf on an object outside the stack, stays unjudged.
*/
singleValued?: boolean;
/** [#34378] The hint when {@link singleValued} refuses a path. */
singleValuedPrescription?: string;
}

function nameOf(v: unknown): string | undefined {
Expand Down Expand Up @@ -551,10 +571,14 @@ const FIELD_NAME_SLOTS: readonly FieldNameSlot[] = [
pickerOnly: true,
maxHops: 1,
ownFinding: true,
singleValued: true,
read: readRecordReference,
prescription:
'Fix the path — `$record` reads the record being edited: a field of this object, or one '
+ 'relationship field of it and then a field of the object it references (`account.cb_market`).',
singleValuedPrescription:
'Point `$record` at a single-valued field: a multi-valued field holds a list, and no scalar comparison '
+ 'takes one. Copy the value you mean onto a single-valued field and reference that.',
},
{
key: 'dependsOn',
Expand Down Expand Up @@ -675,6 +699,29 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[]
findings.push({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN, where: at.where, path: at.path, message, hint });
};

/**
* [#34378] Where a path that resolved stops reading ONE value: its AUTHORED
* first segment on `objectName` when that is multi-valued (a plain path to
* it, or a hop through it), else — behind a single-valued hop — its
* authored leaf when that is. `undefined` = single-valued, or not
* judgeable here (a registry-injected column, an object outside the stack).
*/
const multiValuedSegment = (
objectName: string,
segments: readonly string[],
): { how: string; field: string; object: string } | undefined => {
const isMulti = (m: GraphField | undefined): boolean =>
typeof m?.type === 'string' && isMultiValueField({ type: m.type, multiple: m.multiple === true });
const head = graph.get(objectName)?.fields.get(segments[0]!);
if (isMulti(head)) {
return { how: segments.length > 1 ? 'goes through' : 'names', field: segments[0]!, object: objectName };
}
if (segments.length < 2) return undefined;
const verdict = resolveFieldPath(graph, objectName, segments.join('.'));
if (verdict?.kind !== 'ok' || verdict.injected || !isMulti(verdict.meta)) return undefined;
return { how: 'ends at', field: verdict.field, object: verdict.object };
};

/** The declared slice of `name` on `objectName` — authored, else registry-injected. */
const fieldOf = (objectName: string, name: string) => {
const o = graph.get(objectName);
Expand Down Expand Up @@ -975,6 +1022,21 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[]
prescription: slot.prescription,
});
if (hit && !slot.ownFinding) reported.add(`${at}=${read.name}`);

// [#34378] A path that names real fields can still read a LIST: a
// multi-valued head, or a multi-valued leaf behind one hop. The
// comparand is one value under a scalar operator, so it is refused.
if (!hit && slot.singleValued) {
const multi = multiValuedSegment(against, segments);
if (multi) {
refuseKey(
{ where, path },
`${subject} "${read.name}" ${multi.how} "${multi.field}", a multi-valued field on object `
+ `"${multi.object}"; a $record comparand is one value under a scalar operator.`,
slot.singleValuedPrescription ?? slot.prescription,
);
}
}
});
}

Expand Down
Loading
Loading