Skip to content

Commit c2a3e61

Browse files
fix(lint)!: refuse a lookupFilters $record path to a structured field (#38096)
Fixes #34562 Clause-②: no (narrowing) Metadata-protocol: narrowed Skills-docs: none — no page under `skills/` teaches a `$record` path to a structured field (the two `$record` samples in `skills/objectstack-data/rules/field-types.md` read a date and a lookup hop to a select) ## 维护者速读 - 用户能看到什么变化:元数据作者在查找字段的过滤条件里,把"取当前记录的值"指向地址、JSON、重复组这类结构型字段时,校验会直接报错并给出改法;用户界面无变化。 - 为什么做:延续上一张"多值字段不能作单值比较"的修复,结构型字段同样没有单个可比较的值,以前校验放行、过滤看似生效实则不生效。 - 出错会怎样:最坏情况是作者原本能保存的这类写法现在被拒,需要改指向单值字段;撤回本改动即可恢复放行。 - 要你做什么:无。 ## What changes `object-field-ref-unknown`, position `lookupFilters[N].value.$record`: a path whose HEAD (a plain path) or LEAF (behind one single-valued relationship hop) is a structured field is now refused, beside the multi-valued refusal and with the same hint (`singleValuedPrescription`, unchanged). The set is `STRUCTURED_JSON_TYPES` imported from `@objectstack/spec/data`, never re-listed. The verdict names it `a structured field`; the multi-valued verdict text is byte-identical. A hop THROUGH a structured head (`s_address.city`) was already refused as the family's "not a relationship" miss and still is, once (pinned). ADR-0087: a new semantic ledger entry `lookup-filter-record-path-structured-refused` (`packages/spec/src/migrations/entries/semantic/19.lookup-filter-record-path-structured-refused.ts`, filed under protocol major 19 (`PROTOCOL_VERSION` 19.0.0, the 18.x line is released, so an `18.` file would be skipped on `--from 18`); `registry.ts` is generated and untracked), and the changeset carries `adr-0087: registered lookup-filter-record-path-structured-refused` plus a `@objectstack/spec` patch line. Docs: `content/docs/data-modeling/relationships.mdx` listed only the unknown-path refusal; its sentence now also names a path whose first field, or leaf behind the hop, is multi-valued or structured. Files: `packages/lint/src/validate-object-field-refs.ts`, `packages/lint/src/validate-object-field-refs.test.ts`, `packages/spec/src/migrations/entries/semantic/19.lookup-filter-record-path-structured-refused.ts`, `content/docs/data-modeling/relationships.mdx`, `.changeset/34562-record-structural-lint.md`. Read first (by risk): 1. `validate-object-field-refs.ts`, `multiValuedSegment` — the predicate now returns the noun (`what`); multi-valued is tested first so its findings keep their wording. 2. `validate-object-field-refs.ts`, the `singleValued` refusal block — the message interpolates `what`. 3. The new `describe('a path to a structured field (#34562)')` block in the test file. ## Tests - New block beside the multi-valued block: `it.each` over every member of `STRUCTURED_JSON_TYPES` at the head and at the leaf (14 cases), each asserting the rule id, the full message and the full hint text; a coverage case that the five the card names are members; a CONTROL case (scalar head and leaf beside the structured fields) that stays clean; the hop-through case. - Red without the fix: restoring the source file to the merge base turns exactly the 14 head/leaf cases red (`Tests 14 failed | 134 passed (148)`), control, coverage and hop-through stay green; restored. ## Acceptance notes - S1 → follow-up #38141 (the `os lint --explain` text and the `$record` describes). - The other `18.` ledger entries that landed after the protocol moved to 19 are out of this diff → #38142. - `composite` and `record` (in the spec set, not in the card's five) are kept: `composite` values parse as `z.record(z.string(), z.unknown())` (`field-value.zod.ts`), `record` is a name-keyed map of sub-objects (`field.zod.ts`), and `filter-cross-field-comparison-class.ts` already classes the whole `STRUCTURED_JSON_TYPES` set as `list-or-object`, no comparison class. Neither has a scalar value. - The hint is the multi-valued one verbatim ("a multi-valued field holds a list"), per the ruling; it reads slightly off for a structured field. Not reworded here. - The verdict says `a structured field`, not `a structured TYPE field`: the type-named form put two fixture verdicts over the file's 200-character one-line ceiling. - Measured first-party exposure, `git grep -n '\$record' origin/main -- examples packages/platform-objects packages/plugins skills` at 815e882: 18 hits, zero `lookupFilters` `$record` paths to a structured field (the two lookup-filter hits are the skills samples above; the rest are flow `$record` variables in plugin-approvals). - Docs read against the new refusal: `content/docs/data-modeling/fields.mdx` (its `$record` passage states no refusal list and teaches only `quote_date`) still holds; `content/docs/data-modeling/relationships.mdx` named only the unknown-path refusal and is corrected in this PR. - CI at 9c2f41e: the red `TypeScript Type Check` and `Test UI` rollups were their lanes `cancelled` (run 46096909426 and 46096909327 concluded `cancelled` when the next push superseded them), not failures; no re-run requested. ## Verification Two-minute check: `pnpm --filter @objectstack/lint exec vitest run src/validate-object-field-refs.test.ts -t "structured field"` — expect 17 passing tests. <details><summary>Readings at head 9a9f0e6</summary> - `pnpm --filter @objectstack/lint typecheck && pnpm --filter @objectstack/lint test`: VERDICT command-exit 0, `Test Files 244 passed (244)`, `Tests 9873 passed (9873)`. - `node scripts/check-adr-0087-registration.mjs --base origin/main`: exit 0, `[bang+clause-②-narrowing+metadata-protocol-narrowed] registered lookup-filter-record-path-structured-refused (new here)`. - Registry regenerated by `pnpm --filter @objectstack/spec build` (exit 0); `check:spec-changes`, `check:upgrade-guide`, `vitest run src/migrations` (207 passed) and the cli `migrate-meta-engine-guidance.test.ts` (3 passed) green. Probe on the built dist: `composeMigrationChain(18)` now includes `lookup-filter-record-path-structured-refused` (it did not under the `18.` name). - Full dogfood battery, `pnpm --filter @objectstack/dogfood test` after building its closure: VERDICT command-exit 0, `Test Files 490 passed | 3 skipped (493)`, `Tests 4144 passed | 68 skipped (4212)`. - Measured exposure: `git grep -n '\$record' origin/main -- examples packages/platform-objects packages/plugins skills` at 815e882, 18 hits, zero `lookupFilters` `$record` paths to a structured field. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcSAaBEfQpU2yJexSJ8)_ <!-- signed: repo:objectstack#8 · sunhaolin · session_017xRBcSAaBEfQpU2yJexSJ8 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent aacbb81 commit c2a3e61

5 files changed

Lines changed: 168 additions & 18 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/lint': minor
3+
'@objectstack/spec': patch
4+
---
5+
6+
fix(lint)!: a lookup filter's `$record` path to a structured field is refused where it is written
7+
8+
Clause-②: no (narrowing)
9+
Metadata-protocol: narrowed
10+
11+
A lookup field's `lookupFilters[].value.$record` reads one value from the record being edited and compares it under a scalar operator. A path whose first segment, or whose leaf behind one relationship hop, is a structured field — a member of `STRUCTURED_JSON_TYPES` in `@objectstack/spec/data`: `json`, `composite`, `repeater`, `record`, `location`, `address`, `vector` — holds a list or an object rather than one value. Read from code, the picker compares that list or object as one value under a scalar operator, so the filter does not do what it reads as, and `os lint` said nothing. Rule `object-field-ref-unknown` now refuses such a path, at the same two positions and with the same hint as a path to a multi-valued field: point `$record` at a single-valued field. A hop through a structured field was already refused as "not a relationship" and still is, once.
12+
13+
To fix a refused path, copy the value you mean onto a single-valued field and reference that field instead.
14+
15+
The ADR-0087 ledger gains the semantic entry `lookup-filter-record-path-structured-refused` in `@objectstack/spec`, beside the multi-valued one.
16+
17+
<!-- adr-0087: registered lookup-filter-record-path-structured-refused -->

‎content/docs/data-modeling/relationships.mdx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ price_book: Field.lookup('price_book', {
6363

6464
- **The reference.** `{ $record }` is the whole `value`, never a member of an `in` / `notIn`
6565
list, and pairs only with `eq`, `ne`, `gt`, `lt`, `gte` or `lte`. `os validate` refuses a
66-
path that names no field of the object the lookup is on, or of the object its hop points at.
66+
path that names no field of the object the lookup is on, or of the object its hop points at,
67+
and a path whose first field, or whose leaf behind the hop, holds a list or an object rather
68+
than one value: a multi-valued field, or a structured one (`json`, `address`, `location`,
69+
`repeater` and the like).
6770
- **Where it resolves.** In a record form, whatever its layout. Every other picker host leaves
6871
every `{ $record }` entry out: inline edit on a record page, a record page's header or
6972
related list, an action's parameter dialog, and a bulk action over selected rows.

‎packages/lint/src/validate-object-field-refs.test.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { explainRule } from './rule-explanations.js';
1111
import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js';
1212
import { SEMANTIC_ROLE_FIELD_UNKNOWN, validateSemanticRoles } from './validate-semantic-roles.js';
13-
import { FieldSchema } from '@objectstack/spec/data';
13+
import { FieldSchema, STRUCTURED_JSON_TYPES } from '@objectstack/spec/data';
1414

1515
const obj = (over: Record<string, unknown> = {}) => ({
1616
name: 'proj_task',
@@ -929,6 +929,63 @@ describe('validateObjectFieldRefs — lookupFilters[].value.$record (the RECORD
929929
], multi))).toEqual([]);
930930
});
931931
});
932+
933+
// [#34562] A STRUCTURED field — a member of `STRUCTURED_JSON_TYPES`, taken
934+
// from `@objectstack/spec/data`, never re-listed — holds an OBJECT, so a
935+
// path that names one (the head) or ends at one behind a hop (the leaf) has
936+
// no scalar value either: refused by the same rule with the same hint.
937+
describe('a path to a structured field (#34562)', () => {
938+
const structural = [...STRUCTURED_JSON_TYPES];
939+
const fieldsOf = Object.fromEntries(structural.map((t) => [`s_${t}`, { type: t }]));
940+
const stack = (filters: unknown[]) => {
941+
const s = quote(filters, fieldsOf);
942+
Object.assign((s.objects[1] as { fields: Record<string, unknown> }).fields, fieldsOf);
943+
return s;
944+
};
945+
const refused = (path: string) => {
946+
const findings = validateObjectFieldRefs(stack([entry(path)]));
947+
expect(findings.map((f) => f.path)).toEqual([at(0)]);
948+
expect(findings[0]).toMatchObject({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN });
949+
expect(findings[0]!.hint).toBe(
950+
'Point `$record` at a single-valued field: a multi-valued field holds a list, and no scalar comparison '
951+
+ 'takes one. Copy the value you mean onto a single-valued field and reference that.',
952+
);
953+
return findings[0]!.message;
954+
};
955+
956+
it('covers the five the card names, and the spec set is what is judged', () => {
957+
for (const t of ['json', 'repeater', 'location', 'address', 'vector']) expect(structural).toContain(t);
958+
});
959+
960+
it.each(structural)('REFUSES a plain path to a `%s` field (the head)', (t) => {
961+
expect(refused(`s_${t}`)).toBe(
962+
`fields.price_book.lookupFilters[0].value.$record "s_${t}" names "s_${t}", a structured field on object `
963+
+ '"sales_quote"; a $record comparand is one value under a scalar operator.',
964+
);
965+
});
966+
967+
it.each(structural)('REFUSES a `%s` leaf behind a single-valued hop, naming the leaf and its object', (t) => {
968+
expect(refused(`account.s_${t}`)).toBe(
969+
`fields.price_book.lookupFilters[0].value.$record "account.s_${t}" ends at "s_${t}", a structured field `
970+
+ 'on object "crm_account"; a $record comparand is one value under a scalar operator.',
971+
);
972+
});
973+
974+
it('reports a hop THROUGH a structured head once, as the family\'s not-a-relationship miss', () => {
975+
const findings = validateObjectFieldRefs(stack([entry('s_address.city')]));
976+
expect(findings.map((f) => f.path)).toEqual([at(0)]);
977+
expect(findings[0]!.message).toContain('not a relationship');
978+
});
979+
980+
it('CONTROL: scalar paths beside the structured fields stay clean — head and leaf', () => {
981+
expect(validateObjectFieldRefs(stack([
982+
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' } },
983+
entry('notes'),
984+
entry('account.cb_market'),
985+
entry('account.name'),
986+
]))).toEqual([]);
987+
});
988+
});
932989
});
933990

934991
describe('validateObjectFieldRefs — summaryOperations.filter (addresses the CHILD object, #31224)', () => {

‎packages/lint/src/validate-object-field-refs.ts‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@
119119
* one behind a single-valued hop — is refused too: the comparand is ONE
120120
* value under a scalar operator, and a list has no such value. The
121121
* predicate is `isMultiValueField` (`@objectstack/spec/data`), the one
122-
* `FieldSchema` reads. A registry-injected column, and a leaf on an object
123-
* outside this stack, stay unjudged. A `{ $record }` nested
122+
* `FieldSchema` reads. A plain path to, or a leaf behind a hop at, a
123+
* STRUCTURED field (#34562) — a member of `STRUCTURED_JSON_TYPES`
124+
* (`@objectstack/spec/data`): json, address, location, … — is refused the
125+
* same way, with the same hint: it holds a list or an object rather than
126+
* one value. A
127+
* registry-injected column, and a leaf on an object outside this stack,
128+
* stay unjudged. A `{ $record }` nested
124129
* deeper inside a literal (`value: { any: { $record: 'x' } }`) is an
125130
* opaque literal to the parse, and this rule does not name it either: no
126131
* reader resolves that shape, nothing first-party writes it, and the
@@ -302,7 +307,12 @@ import {
302307
type GraphField,
303308
type ObjectGraph,
304309
} from './object-graph.js';
305-
import { classifyDottedFilterHead, isMultiValueField, isVirtualSearchField } from '@objectstack/spec/data';
310+
import {
311+
classifyDottedFilterHead,
312+
isMultiValueField,
313+
isVirtualSearchField,
314+
STRUCTURED_JSON_TYPES,
315+
} from '@objectstack/spec/data';
306316

307317
/**
308318
* A field-name list entry — object-level, or on one of the object's fields —
@@ -494,7 +504,8 @@ interface FieldNameSlot {
494504
ownFinding?: boolean;
495505
/**
496506
* [#34378] `true` when the path must read ONE value: its first segment and,
497-
* behind a hop, its leaf may not be multi-valued (`isMultiValueField`). A
507+
* behind a hop, its leaf may not be multi-valued (`isMultiValueField`) or
508+
* structured (`STRUCTURED_JSON_TYPES`, #34562). A
498509
* path whose head is multi-valued is refused whether it ends there or hops
499510
* through it, at the head; a multi-valued leaf behind a single-valued hop is
500511
* refused at the leaf. Judged only on a path the family's resolution did
@@ -727,23 +738,38 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[]
727738
* [#34378] Where a path that resolved stops reading ONE value: its AUTHORED
728739
* first segment on `objectName` when that is multi-valued (a plain path to
729740
* it, or a hop through it), else — behind a single-valued hop — its
730-
* authored leaf when that is. `undefined` = single-valued, or not
731-
* judgeable here (a registry-injected column, an object outside the stack).
741+
* authored leaf when that is. A STRUCTURED field (#34562: a member of
742+
* `STRUCTURED_JSON_TYPES`, `@objectstack/spec/data` — json, address,
743+
* location, …) holds a list or an object rather than one value, so it has
744+
* no scalar value either and is
745+
* named at the same two positions. `what` is the finding's noun for the
746+
* field. `undefined` = one scalar value, or not judgeable here (a
747+
* registry-injected column, an object outside the stack).
732748
*/
733749
const multiValuedSegment = (
734750
objectName: string,
735751
segments: readonly string[],
736-
): { how: string; field: string; object: string } | undefined => {
737-
const isMulti = (m: GraphField | undefined): boolean =>
738-
typeof m?.type === 'string' && isMultiValueField({ type: m.type, multiple: m.multiple === true });
752+
): { how: string; field: string; object: string; what: string } | undefined => {
753+
const nonScalar = (m: GraphField | undefined): string | undefined => {
754+
if (typeof m?.type !== 'string') return undefined;
755+
if (isMultiValueField({ type: m.type, multiple: m.multiple === true })) return 'a multi-valued field';
756+
return STRUCTURED_JSON_TYPES.has(m.type) ? 'a structured field' : undefined;
757+
};
739758
const head = graph.get(objectName)?.fields.get(segments[0]!);
740-
if (isMulti(head)) {
741-
return { how: segments.length > 1 ? 'goes through' : 'names', field: segments[0]!, object: objectName };
759+
const headWhat = nonScalar(head);
760+
if (headWhat) {
761+
return {
762+
how: segments.length > 1 ? 'goes through' : 'names',
763+
field: segments[0]!,
764+
object: objectName,
765+
what: headWhat,
766+
};
742767
}
743768
if (segments.length < 2) return undefined;
744769
const verdict = resolveFieldPath(graph, objectName, segments.join('.'));
745-
if (verdict?.kind !== 'ok' || verdict.injected || !isMulti(verdict.meta)) return undefined;
746-
return { how: 'ends at', field: verdict.field, object: verdict.object };
770+
if (verdict?.kind !== 'ok' || verdict.injected) return undefined;
771+
const leafWhat = nonScalar(verdict.meta);
772+
return leafWhat ? { how: 'ends at', field: verdict.field, object: verdict.object, what: leafWhat } : undefined;
747773
};
748774

749775
/** The declared slice of `name` on `objectName` — authored, else registry-injected. */
@@ -1063,14 +1089,15 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[]
10631089
}
10641090

10651091
// [#34378] A path that names real fields can still read a LIST: a
1066-
// multi-valued head, or a multi-valued leaf behind one hop. The
1067-
// comparand is one value under a scalar operator, so it is refused.
1092+
// multi-valued head, or a multi-valued leaf behind one hop — or
1093+
// (#34562) an OBJECT: a structured head or leaf. The comparand is
1094+
// one value under a scalar operator, so it is refused.
10681095
if (!hit && slot.singleValued) {
10691096
const multi = multiValuedSegment(against, segments);
10701097
if (multi) {
10711098
refuseKey(
10721099
{ where, path },
1073-
`${subject} "${read.name}" ${multi.how} "${multi.field}", a multi-valued field on object `
1100+
`${subject} "${read.name}" ${multi.how} "${multi.field}", ${multi.what} on object `
10741101
+ `"${multi.object}"; a $record comparand is one value under a scalar operator.`,
10751102
slot.singleValuedPrescription ?? slot.prescription,
10761103
);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import type { SemanticMigration } from '../../types.js';
4+
5+
// #34562 — a `lookupFilters[].value` `{ $record: 'path' }` path must read ONE
6+
// value of the record being edited: a path whose head, or whose leaf behind a
7+
// single-valued hop, is a structured field (`STRUCTURED_JSON_TYPES`) names a
8+
// list or an object rather than one value, and is refused as
9+
// `object-field-ref-unknown`, beside the multi-valued refusal of #34378
10+
// (`lookup-filter-record-path-multi-valued-refused`).
11+
// Measured first-party exposure at this change: zero — no example app,
12+
// platform object, plugin or skill writes a `$record` path to such a field.
13+
// Not converted: which single value a structured path meant is the author's
14+
// judgement.
15+
export const entry: SemanticMigration = {
16+
id: 'lookup-filter-record-path-structured-refused',
17+
surface:
18+
'objects[].fields.<name>.lookupFilters[].value.$record of a lookup, master_detail or user field, on an object saved '
19+
+ 'or published through the metadata doors (PUT /api/v1/meta/object/NAME, POST /api/v1/packages/PKG/publish-drafts, '
20+
+ 'the per-item publish and the restore doors) or loaded by os validate / os build / os lint: a path whose first '
21+
+ 'segment is a structured field of the object that owns the picker field, or whose leaf behind a single-valued hop '
22+
+ 'is a structured field of the object that hop references — structured being a member of STRUCTURED_JSON_TYPES '
23+
+ '(json, composite, repeater, record, location, address, vector)',
24+
replacement:
25+
'A { $record } path reads ONE value of the record being edited: a single-valued, scalar field of the object that '
26+
+ 'owns the picker field ("quote_date"), or one single-valued relationship field of it followed by a scalar field '
27+
+ 'of the object it references ("account.cb_market"). To filter by a value a structured field holds, copy the value '
28+
+ 'you mean onto a single-valued field and reference that. Refused at error as `object-field-ref-unknown` '
29+
+ '(422 INVALID_METADATA at the doors).',
30+
reason:
31+
'The { $record } comparand stands for one value and pairs only with the six scalar comparisons, but a '
32+
+ 'structured field holds a list or an object rather than one value (an address, a location, a JSON document, a '
33+
+ 'list of rows), and the path check judged existence and multi-valuedness only, so such a path parsed and '
34+
+ 'linted clean. Read from code: the picker would compare that list or object as one value under a scalar '
35+
+ 'operator, which no declared reading answers, so the filter does not do what it reads as. The set is '
36+
+ 'STRUCTURED_JSON_TYPES from @objectstack/spec/data, which the cross-field comparison classes already place in '
37+
+ 'the list-or-object family with no comparison class. A hop THROUGH a structured field was already refused as '
38+
+ 'not a relationship. No first-party stack (example apps, platform objects, plugins, skills) writes such a '
39+
+ 'path today. A registry-injected column and a leaf on an object outside the stack are not judged. There is no '
40+
+ 'mechanical rewrite: which single value the author meant is a judgement about the data.',
41+
acceptanceCriteria:
42+
'Load every object (`os validate` or `os build`): no `object-field-ref-unknown` finding at '
43+
+ 'fields.NAME.lookupFilters[N].value.$record that names a structured field. PUT '
44+
+ '/api/v1/meta/object/NAME?mode=draft&package=PKG with each such path pointing at a single-valued scalar field (or '
45+
+ 'one single-valued hop to one) answers 200, and POST /api/v1/packages/PKG/publish-drafts publishes it.',
46+
};

0 commit comments

Comments
 (0)