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

feat(lint)!: a `lookupFilters` `{ $record: "path" }` path must name a field of the record being edited, or one hop from it

Clause-②: no (narrowing) — a `lookupFilters[].value` `{ $record }` path on a lookup, master_detail or user field that names no field of the object owning that field, hops through a field that is not a relationship, names a leaf the referenced object lacks, or takes a second hop, parsed 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 (the picker that resolves it has not landed; both describes carry `[PLANNED — not enforced]`), so there is no teaching to correct; the existing follow-up for `{ $record }` docs moves with the picker change.

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

**BREAKING** only for a `{ $record }` path that already names nothing the record being edited carries. The `{ $record }` shape landed with its spelling judged (snake_case names, at most one dot) but not what it names, so `{ $record: "acount.cb_market" }` parsed and was stored. `object-field-ref-unknown` now resolves it against the object that owns the picker field: the first segment is a field of that object; with a second segment the first is a relationship field (`lookup`, `master_detail`, `user` or `tree`) and the second a field of the object it references. A hop to an object outside the stack is not judged. Measured first-party exposure: zero — no example app, platform object, skill or docs sample writes a `$record` path.

**Fix:** correct the misspelt segment. `{ $record }` reads the record being edited; a column of the candidate row is written `{ $field: "column" }`. The semantic entry `lookup-filter-record-path-unknown-refused` carries the same notice into the upgrade guide and `os migrate meta`. `os explain object-field-ref-unknown` lists the position.
9 changes: 9 additions & 0 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4482,6 +4482,15 @@ const OBJECT_FIELD_REF_UNKNOWN_EXPLANATION: RuleExplanation = {
'(the picker scopes its candidates by that key). The referenced object is judged only on ' +
'`lookup`, `master_detail` and `user` fields, the types that render the picker, and a bare ' +
'`dependsOn` name that misses on the owning object is reported once, there.',
'On those picker fields a `lookupFilters[].value.$record` path is judged too, against the record ' +
'being edited: a field of the object that owns the picker field, or at most one relationship hop ' +
'from it (a ' +
'relationship field of that object, then a field of the object it references, as in ' +
'`account.cb_market`). The picker is declared to swap that value into its query and to leave ' +
'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.',
'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
157 changes: 156 additions & 1 deletion packages/lint/src/validate-object-field-refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { explainRule } from './rule-explanations.js';
import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js';
import { SEMANTIC_ROLE_FIELD_UNKNOWN, validateSemanticRoles } from './validate-semantic-roles.js';
import { FieldSchema } from '@objectstack/spec/data';

const obj = (over: Record<string, unknown> = {}) => ({
name: 'proj_task',
Expand Down Expand Up @@ -705,6 +706,157 @@ describe('validateObjectFieldRefs — lookupFilters[].field (addresses the REFER
});
});

describe('validateObjectFieldRefs — lookupFilters[].value.$record (the RECORD BEING EDITED, one hop, #34200)', () => {
// A quote's Price Book picker (the `LookupFilterRecordReference` example):
// `$record` reads the quote — the object that OWNS the picker field — or one
// relationship hop from it, never the price book the filter runs on.
const quote = (filters: unknown[], over: Record<string, unknown> = {}) => ({
objects: [
{
name: 'sales_quote',
fields: {
quote_date: { type: 'date' },
notes: { type: 'text' },
account: { type: 'lookup', reference: 'crm_account' },
partner: { type: 'lookup', reference: 'elsewhere_partner' },
parent_quote: { type: 'tree', reference: 'sales_quote' },
approver_user: { type: 'user' },
price_book: { type: 'lookup', reference: 'sales_price_book', lookupFilters: filters },
...over,
},
},
{
name: 'crm_account',
fields: {
name: { type: 'text' },
cb_market: { type: 'select' },
parent: { type: 'lookup', reference: 'crm_account' },
},
},
{
name: 'sales_price_book',
fields: { valid_from: { type: 'date' }, valid_to: { type: 'date' }, cb_market: { type: 'select' } },
},
],
});
const at = (i: number) => `objects[0].fields.price_book.lookupFilters[${i}].value.$record`;
const entry = (path: string, field = 'cb_market') => ({ field, operator: 'eq', value: { $record: path } });

it('REFUSES an unknown head, naming the segment and the edited object', () => {
const findings = validateObjectFieldRefs(quote([entry('acount.cb_market')]));
expect(findings).toHaveLength(1);
expect(findings[0]).toMatchObject({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN, path: at(0) });
expect(findings[0]!.message).toBe(
'fields.price_book.lookupFilters[0].value.$record "acount.cb_market" traverses "acount", which is not a '
+ 'field on object "sales_quote". Did you mean "account"?',
);
expect(findings[0]!.hint).toContain('`$record` reads the record being edited');
});

it('REFUSES an unknown leaf, judged against the object the hop references', () => {
const findings = validateObjectFieldRefs(quote([entry('account.cb_markt')]));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]!.message).toContain('"account.cb_markt" is not a field on object "crm_account"');
expect(findings[0]!.message).toContain('Did you mean "cb_market"?');
});

it('REFUSES a hop through a field that is not a relationship', () => {
const findings = validateObjectFieldRefs(quote([entry('notes.cb_market')]));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]!.message).toContain('traverses "notes", which is a `text` field on object "sales_quote"');
});

it('REFUSES two hops at the second one, even when every segment exists (the raw `os lint` path)', () => {
const findings = validateObjectFieldRefs(quote([entry('account.parent.cb_market')]));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]!.message).toBe(
'fields.price_book.lookupFilters[0].value.$record "account.parent.cb_market" takes a second hop at '
+ '"parent"; it reads at most one relationship hop from the record being edited.',
);
});

it('REFUSES a single name the edited record does not carry — a field of the FILTERED object is not one', () => {
// `valid_from` is a column of the price book the filter runs on; `$record`
// reads the quote. Writing the candidate's own column is `{ $field }`.
const findings = validateObjectFieldRefs(quote([entry('valid_from', 'valid_to')]));
expect(findings.map((f) => f.path)).toEqual([at(0)]);
expect(findings[0]!.message).toContain('"valid_from" is not a field on object "sales_quote"');
});

it('passes a single field of the edited record, and an injected column of it', () => {
expect(validateObjectFieldRefs(quote([
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' }, orEmpty: true },
{ field: 'valid_to', operator: 'gte', value: { $record: 'created_at' } },
]))).toEqual([]);
});

it('passes a single hop to a field of the referenced object', () => {
expect(validateObjectFieldRefs(quote([entry('account.cb_market')]))).toEqual([]);
});

// [review S1] The hop set is the family's `RELATIONSHIP_FIELD_TYPES`, not
// lookup / master_detail alone: a `user` field is a lookup to `sys_user`
// and a `tree` a self-lookup, each one stored foreign key the picker
// resolves the same way. An injected relationship column is unjudged.
it('passes a hop through a `tree` field (a self-lookup)', () => {
expect(validateObjectFieldRefs(quote([entry('parent_quote.quote_date', 'valid_from')]))).toEqual([]);
});

it('passes a hop through a `user` field (a lookup to `sys_user`, unjudged past the hop here)', () => {
expect(validateObjectFieldRefs(quote([entry('approver_user.department')]))).toEqual([]);
});

it('passes a hop through an injected column (`owner_id.NAME`): the graph cannot judge it', () => {
expect(validateObjectFieldRefs(quote([entry('owner_id.anything')]))).toEqual([]);
});

it('stays silent on a hop whose referenced object is not in this stack, as `dependsOn` does', () => {
expect(validateObjectFieldRefs(quote([entry('partner.anything')]))).toEqual([]);
});

it('reports the `$record` miss and the `.field` miss of one entry as two findings: two objects, two fixes', () => {
const findings = validateObjectFieldRefs(quote([
{ field: 'cb_markt', operator: 'eq', value: { $record: 'cb_markt' } },
]));
expect(findings.map((f) => f.path)).toEqual([
'objects[0].fields.price_book.lookupFilters[0].field',
at(0),
]);
});

it('reads the whole value only: a list member is the parse\'s refusal, not this rule\'s', () => {
// `refuseMalformedLookupFilterEntry` refuses `{ $record }` as a list member
// ("never a member of a list"), so nothing judged here could add to it.
expect(validateObjectFieldRefs(quote([
{ field: 'cb_market', operator: 'in', value: [{ $record: 'acount.cb_market' }, 'domestic'] },
]))).toEqual([]);
expect(FieldSchema.safeParse({
type: 'lookup',
reference: 'sales_price_book',
lookupFilters: [{ field: 'cb_market', operator: 'in', value: [{ $record: 'account.cb_market' }, 'domestic'] }],
}).success).toBe(false);
});

it('leaves a `$record` nested deeper inside a literal unnamed — an opaque literal to the parse too', () => {
expect(validateObjectFieldRefs(quote([
{ field: 'cb_market', operator: 'eq', value: { market: { $record: 'acount.cb_market' } } },
]))).toEqual([]);
});

it('stays silent on a type with no picker: nothing reads the key there', () => {
expect(validateObjectFieldRefs(quote([], {
memo: { type: 'text', lookupFilters: [entry('acount.cb_market')] },
}))).toEqual([]);
});

it('judges a `user` field\'s picker too, against the record being edited', () => {
const findings = validateObjectFieldRefs(quote([], {
approver: { type: 'user', lookupFilters: [{ field: 'name', operator: 'eq', value: { $record: 'acount.name' } }] },
}));
expect(findings.map((f) => f.path)).toEqual(['objects[0].fields.approver.lookupFilters[0].value.$record']);
});
});

describe('validateObjectFieldRefs — summaryOperations.filter (addresses the CHILD object, #31224)', () => {
// `crm_account` is the parent; `crm_invoice` (with an FK to it) the child.
const rollup = (filter: unknown, over: Record<string, unknown> = {}) => ({
Expand Down Expand Up @@ -1030,6 +1182,8 @@ describe('[#22161] one-line verdicts — object-field-ref-unknown', () => {
'`compactLayout`',
'`relatedListColumns` against the object that owns the field',
'`lookupColumns` and `lookupFilters[].field` against the REFERENCED object',
'`lookupFilters[].value.$record`',
'at most one relationship hop',
'`lookup`, `master_detail` and `user`',
'reported once, there',
'read verbatim',
Expand All @@ -1056,6 +1210,7 @@ describe('[#22161] one-line verdicts — object-field-ref-unknown', () => {
'.relatedListColumns[',
'.lookupColumns[',
'.lookupFilters[',
'.value.$record',
'.dependsOn[',
'.summaryOperations.filter.',
'.nameField',
Expand All @@ -1079,7 +1234,7 @@ describe('[#22161] one-line fix — object-field-ref-unknown', () => {
const converted = fired.filter((f) => f.rule === OBJECT_FIELD_REF_UNKNOWN);
// Coverage control: every position fired (the verdict block above names
// them), so the bound reads each prescription.
for (const position of ['.highlightFields[', '.publicSharing.redactFields[', '.indexes[', '.relatedListColumns[', '.lookupColumns[', '.lookupFilters[', '.dependsOn[', '.summaryOperations.filter.', '.nameField', '.displayNameField', '.activityMilestones[']) {
for (const position of ['.highlightFields[', '.publicSharing.redactFields[', '.indexes[', '.relatedListColumns[', '.lookupColumns[', '.lookupFilters[', '.value.$record', '.dependsOn[', '.summaryOperations.filter.', '.nameField', '.displayNameField', '.activityMilestones[']) {
expect(converted.some((f) => f.path.includes(position)), position).toBe(true);
}
for (const f of converted) {
Expand Down
Loading
Loading