diff --git a/.changeset/34562-record-structural-lint.md b/.changeset/34562-record-structural-lint.md new file mode 100644 index 00000000000..7f3f8e926a5 --- /dev/null +++ b/.changeset/34562-record-structural-lint.md @@ -0,0 +1,17 @@ +--- +'@objectstack/lint': minor +'@objectstack/spec': patch +--- + +fix(lint)!: a lookup filter's `$record` path to a structured field is refused where it is written + +Clause-②: no (narrowing) +Metadata-protocol: narrowed + +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. + +To fix a refused path, copy the value you mean onto a single-valued field and reference that field instead. + +The ADR-0087 ledger gains the semantic entry `lookup-filter-record-path-structured-refused` in `@objectstack/spec`, beside the multi-valued one. + + diff --git a/content/docs/data-modeling/relationships.mdx b/content/docs/data-modeling/relationships.mdx index 5d1fbcf61a4..c430488c3d9 100644 --- a/content/docs/data-modeling/relationships.mdx +++ b/content/docs/data-modeling/relationships.mdx @@ -63,7 +63,10 @@ price_book: Field.lookup('price_book', { - **The reference.** `{ $record }` is the whole `value`, never a member of an `in` / `notIn` list, and pairs only with `eq`, `ne`, `gt`, `lt`, `gte` or `lte`. `os validate` refuses a - path that names no field of the object the lookup is on, or of the object its hop points at. + path that names no field of the object the lookup is on, or of the object its hop points at, + and a path whose first field, or whose leaf behind the hop, holds a list or an object rather + than one value: a multi-valued field, or a structured one (`json`, `address`, `location`, + `repeater` and the like). - **Where it resolves.** In a record form, whatever its layout. Every other picker host leaves every `{ $record }` entry out: inline edit on a record page, a record page's header or related list, an action's parameter dialog, and a bulk action over selected rows. diff --git a/packages/lint/src/validate-object-field-refs.test.ts b/packages/lint/src/validate-object-field-refs.test.ts index b79377b83d1..443e750ccc5 100644 --- a/packages/lint/src/validate-object-field-refs.test.ts +++ b/packages/lint/src/validate-object-field-refs.test.ts @@ -10,7 +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'; +import { FieldSchema, STRUCTURED_JSON_TYPES } from '@objectstack/spec/data'; const obj = (over: Record = {}) => ({ name: 'proj_task', @@ -929,6 +929,63 @@ describe('validateObjectFieldRefs — lookupFilters[].value.$record (the RECORD ], multi))).toEqual([]); }); }); + + // [#34562] A STRUCTURED field — a member of `STRUCTURED_JSON_TYPES`, taken + // from `@objectstack/spec/data`, never re-listed — holds an OBJECT, so a + // path that names one (the head) or ends at one behind a hop (the leaf) has + // no scalar value either: refused by the same rule with the same hint. + describe('a path to a structured field (#34562)', () => { + const structural = [...STRUCTURED_JSON_TYPES]; + const fieldsOf = Object.fromEntries(structural.map((t) => [`s_${t}`, { type: t }])); + const stack = (filters: unknown[]) => { + const s = quote(filters, fieldsOf); + Object.assign((s.objects[1] as { fields: Record }).fields, fieldsOf); + return s; + }; + const refused = (path: string) => { + const findings = validateObjectFieldRefs(stack([entry(path)])); + expect(findings.map((f) => f.path)).toEqual([at(0)]); + expect(findings[0]).toMatchObject({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN }); + expect(findings[0]!.hint).toBe( + '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.', + ); + return findings[0]!.message; + }; + + it('covers the five the card names, and the spec set is what is judged', () => { + for (const t of ['json', 'repeater', 'location', 'address', 'vector']) expect(structural).toContain(t); + }); + + it.each(structural)('REFUSES a plain path to a `%s` field (the head)', (t) => { + expect(refused(`s_${t}`)).toBe( + `fields.price_book.lookupFilters[0].value.$record "s_${t}" names "s_${t}", a structured field on object ` + + '"sales_quote"; a $record comparand is one value under a scalar operator.', + ); + }); + + it.each(structural)('REFUSES a `%s` leaf behind a single-valued hop, naming the leaf and its object', (t) => { + expect(refused(`account.s_${t}`)).toBe( + `fields.price_book.lookupFilters[0].value.$record "account.s_${t}" ends at "s_${t}", a structured field ` + + 'on object "crm_account"; a $record comparand is one value under a scalar operator.', + ); + }); + + it('reports a hop THROUGH a structured head once, as the family\'s not-a-relationship miss', () => { + const findings = validateObjectFieldRefs(stack([entry('s_address.city')])); + expect(findings.map((f) => f.path)).toEqual([at(0)]); + expect(findings[0]!.message).toContain('not a relationship'); + }); + + it('CONTROL: scalar paths beside the structured fields stay clean — head and leaf', () => { + expect(validateObjectFieldRefs(stack([ + { field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' } }, + entry('notes'), + entry('account.cb_market'), + entry('account.name'), + ]))).toEqual([]); + }); + }); }); describe('validateObjectFieldRefs — summaryOperations.filter (addresses the CHILD object, #31224)', () => { diff --git a/packages/lint/src/validate-object-field-refs.ts b/packages/lint/src/validate-object-field-refs.ts index 4ca95947e9c..307e7d993c7 100644 --- a/packages/lint/src/validate-object-field-refs.ts +++ b/packages/lint/src/validate-object-field-refs.ts @@ -119,8 +119,13 @@ * 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 + * `FieldSchema` reads. A plain path to, or a leaf behind a hop at, a + * STRUCTURED field (#34562) — a member of `STRUCTURED_JSON_TYPES` + * (`@objectstack/spec/data`): json, address, location, … — is refused the + * same way, with the same hint: it holds a list or an object rather than + * one value. 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 @@ -302,7 +307,12 @@ import { type GraphField, type ObjectGraph, } from './object-graph.js'; -import { classifyDottedFilterHead, isMultiValueField, isVirtualSearchField } from '@objectstack/spec/data'; +import { + classifyDottedFilterHead, + isMultiValueField, + isVirtualSearchField, + STRUCTURED_JSON_TYPES, +} from '@objectstack/spec/data'; /** * A field-name list entry — object-level, or on one of the object's fields — @@ -494,7 +504,8 @@ interface FieldNameSlot { 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 + * behind a hop, its leaf may not be multi-valued (`isMultiValueField`) or + * structured (`STRUCTURED_JSON_TYPES`, #34562). 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 @@ -727,23 +738,38 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] * [#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). + * authored leaf when that is. A STRUCTURED field (#34562: a member of + * `STRUCTURED_JSON_TYPES`, `@objectstack/spec/data` — json, address, + * location, …) holds a list or an object rather than one value, so it has + * no scalar value either and is + * named at the same two positions. `what` is the finding's noun for the + * field. `undefined` = one scalar value, 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 }); + ): { how: string; field: string; object: string; what: string } | undefined => { + const nonScalar = (m: GraphField | undefined): string | undefined => { + if (typeof m?.type !== 'string') return undefined; + if (isMultiValueField({ type: m.type, multiple: m.multiple === true })) return 'a multi-valued field'; + return STRUCTURED_JSON_TYPES.has(m.type) ? 'a structured field' : undefined; + }; 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 }; + const headWhat = nonScalar(head); + if (headWhat) { + return { + how: segments.length > 1 ? 'goes through' : 'names', + field: segments[0]!, + object: objectName, + what: headWhat, + }; } 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 }; + if (verdict?.kind !== 'ok' || verdict.injected) return undefined; + const leafWhat = nonScalar(verdict.meta); + return leafWhat ? { how: 'ends at', field: verdict.field, object: verdict.object, what: leafWhat } : undefined; }; /** The declared slice of `name` on `objectName` — authored, else registry-injected. */ @@ -1063,14 +1089,15 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] } // [#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. + // multi-valued head, or a multi-valued leaf behind one hop — or + // (#34562) an OBJECT: a structured head or leaf. 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 ` + `${subject} "${read.name}" ${multi.how} "${multi.field}", ${multi.what} on object ` + `"${multi.object}"; a $record comparand is one value under a scalar operator.`, slot.singleValuedPrescription ?? slot.prescription, ); diff --git a/packages/spec/src/migrations/entries/semantic/19.lookup-filter-record-path-structured-refused.ts b/packages/spec/src/migrations/entries/semantic/19.lookup-filter-record-path-structured-refused.ts new file mode 100644 index 00000000000..d92c6ba0387 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/19.lookup-filter-record-path-structured-refused.ts @@ -0,0 +1,46 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// #34562 — a `lookupFilters[].value` `{ $record: 'path' }` path must read ONE +// value of the record being edited: a path whose head, or whose leaf behind a +// single-valued hop, is a structured field (`STRUCTURED_JSON_TYPES`) names a +// list or an object rather than one value, and is refused as +// `object-field-ref-unknown`, beside the multi-valued refusal of #34378 +// (`lookup-filter-record-path-multi-valued-refused`). +// Measured first-party exposure at this change: zero — no example app, +// platform object, plugin or skill writes a `$record` path to such a field. +// Not converted: which single value a structured path meant is the author's +// judgement. +export const entry: SemanticMigration = { + id: 'lookup-filter-record-path-structured-refused', + surface: + 'objects[].fields..lookupFilters[].value.$record of a lookup, master_detail or user field, on an object saved ' + + 'or published through the metadata doors (PUT /api/v1/meta/object/NAME, POST /api/v1/packages/PKG/publish-drafts, ' + + 'the per-item publish and the restore doors) or loaded by os validate / os build / os lint: a path whose first ' + + 'segment is a structured field of the object that owns the picker field, or whose leaf behind a single-valued hop ' + + 'is a structured field of the object that hop references — structured being a member of STRUCTURED_JSON_TYPES ' + + '(json, composite, repeater, record, location, address, vector)', + replacement: + 'A { $record } path reads ONE value of the record being edited: a single-valued, scalar field of the object that ' + + 'owns the picker field ("quote_date"), or one single-valued relationship field of it followed by a scalar field ' + + 'of the object it references ("account.cb_market"). To filter by a value a structured field holds, copy the value ' + + 'you mean onto a single-valued field and reference that. Refused at error as `object-field-ref-unknown` ' + + '(422 INVALID_METADATA at the doors).', + reason: + 'The { $record } comparand stands for one value and pairs only with the six scalar comparisons, but a ' + + 'structured field holds a list or an object rather than one value (an address, a location, a JSON document, a ' + + 'list of rows), and the path check judged existence and multi-valuedness only, so such a path parsed and ' + + 'linted clean. Read from code: the picker would compare that list or object as one value under a scalar ' + + 'operator, which no declared reading answers, so the filter does not do what it reads as. The set is ' + + 'STRUCTURED_JSON_TYPES from @objectstack/spec/data, which the cross-field comparison classes already place in ' + + 'the list-or-object family with no comparison class. A hop THROUGH a structured field was already refused as ' + + 'not a relationship. No first-party stack (example apps, platform objects, plugins, skills) writes such a ' + + 'path today. A registry-injected column and a leaf on an object outside the stack are not judged. There is no ' + + 'mechanical rewrite: which single value the author meant is a judgement about the data.', + acceptanceCriteria: + 'Load every object (`os validate` or `os build`): no `object-field-ref-unknown` finding at ' + + 'fields.NAME.lookupFilters[N].value.$record that names a structured field. PUT ' + + '/api/v1/meta/object/NAME?mode=draft&package=PKG with each such path pointing at a single-valued scalar field (or ' + + 'one single-valued hop to one) answers 200, and POST /api/v1/packages/PKG/publish-drafts publishes it.', +};