|
113 | 113 | * types alone, the picker being its only reader. A path of two hops is |
114 | 114 | * refused at the second hop on the raw `os lint` path the parse has not |
115 | 115 | * seen; a hop's leaf on an object outside this stack stays unjudged, as |
116 | | - * every cross-package name in this family does. A `{ $record }` nested |
| 116 | + * every cross-package name in this family does. A path through or to a |
| 117 | + * MULTI-VALUED field (#34378) — a hop through a `multiple: true` lookup |
| 118 | + * (`accounts.cb_market`), a plain path to one (`tags`), or a leaf that is |
| 119 | + * one behind a single-valued hop — is refused too: the comparand is ONE |
| 120 | + * value under a scalar operator, and a list has no such value. The |
| 121 | + * 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 |
117 | 124 | * deeper inside a literal (`value: { any: { $record: 'x' } }`) is an |
118 | 125 | * opaque literal to the parse, and this rule does not name it either: no |
119 | 126 | * reader resolves that shape, nothing first-party writes it, and the |
@@ -286,9 +293,10 @@ import { |
286 | 293 | recordsOf, |
287 | 294 | resolveFieldPath, |
288 | 295 | type FieldPathVerdict, |
| 296 | + type GraphField, |
289 | 297 | type ObjectGraph, |
290 | 298 | } from './object-graph.js'; |
291 | | -import { classifyDottedFilterHead, isVirtualSearchField } from '@objectstack/spec/data'; |
| 299 | +import { classifyDottedFilterHead, isMultiValueField, isVirtualSearchField } from '@objectstack/spec/data'; |
292 | 300 |
|
293 | 301 | /** |
294 | 302 | * A field-name list entry — object-level, or on one of the object's fields — |
@@ -478,6 +486,18 @@ interface FieldNameSlot { |
478 | 486 | * different objects. |
479 | 487 | */ |
480 | 488 | ownFinding?: boolean; |
| 489 | + /** |
| 490 | + * [#34378] `true` when the path must read ONE value: its first segment and, |
| 491 | + * behind a hop, its leaf may not be multi-valued (`isMultiValueField`). A |
| 492 | + * path whose head is multi-valued is refused whether it ends there or hops |
| 493 | + * through it, at the head; a multi-valued leaf behind a single-valued hop is |
| 494 | + * refused at the leaf. Judged only on a path the family's resolution did |
| 495 | + * not already refuse, and only on AUTHORED fields: a registry-injected |
| 496 | + * column, or a leaf on an object outside the stack, stays unjudged. |
| 497 | + */ |
| 498 | + singleValued?: boolean; |
| 499 | + /** [#34378] The hint when {@link singleValued} refuses a path. */ |
| 500 | + singleValuedPrescription?: string; |
481 | 501 | } |
482 | 502 |
|
483 | 503 | function nameOf(v: unknown): string | undefined { |
@@ -551,10 +571,14 @@ const FIELD_NAME_SLOTS: readonly FieldNameSlot[] = [ |
551 | 571 | pickerOnly: true, |
552 | 572 | maxHops: 1, |
553 | 573 | ownFinding: true, |
| 574 | + singleValued: true, |
554 | 575 | read: readRecordReference, |
555 | 576 | prescription: |
556 | 577 | 'Fix the path — `$record` reads the record being edited: a field of this object, or one ' |
557 | 578 | + 'relationship field of it and then a field of the object it references (`account.cb_market`).', |
| 579 | + singleValuedPrescription: |
| 580 | + 'Point `$record` at a single-valued field: a multi-valued field holds a list, and no scalar comparison ' |
| 581 | + + 'takes one. Copy the value you mean onto a single-valued field and reference that.', |
558 | 582 | }, |
559 | 583 | { |
560 | 584 | key: 'dependsOn', |
@@ -675,6 +699,29 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] |
675 | 699 | findings.push({ severity: 'error', rule: OBJECT_FIELD_REF_UNKNOWN, where: at.where, path: at.path, message, hint }); |
676 | 700 | }; |
677 | 701 |
|
| 702 | + /** |
| 703 | + * [#34378] Where a path that resolved stops reading ONE value: its AUTHORED |
| 704 | + * first segment on `objectName` when that is multi-valued (a plain path to |
| 705 | + * it, or a hop through it), else — behind a single-valued hop — its |
| 706 | + * authored leaf when that is. `undefined` = single-valued, or not |
| 707 | + * judgeable here (a registry-injected column, an object outside the stack). |
| 708 | + */ |
| 709 | + const multiValuedSegment = ( |
| 710 | + objectName: string, |
| 711 | + segments: readonly string[], |
| 712 | + ): { how: string; field: string; object: string } | undefined => { |
| 713 | + const isMulti = (m: GraphField | undefined): boolean => |
| 714 | + typeof m?.type === 'string' && isMultiValueField({ type: m.type, multiple: m.multiple === true }); |
| 715 | + const head = graph.get(objectName)?.fields.get(segments[0]!); |
| 716 | + if (isMulti(head)) { |
| 717 | + return { how: segments.length > 1 ? 'goes through' : 'names', field: segments[0]!, object: objectName }; |
| 718 | + } |
| 719 | + if (segments.length < 2) return undefined; |
| 720 | + const verdict = resolveFieldPath(graph, objectName, segments.join('.')); |
| 721 | + if (verdict?.kind !== 'ok' || verdict.injected || !isMulti(verdict.meta)) return undefined; |
| 722 | + return { how: 'ends at', field: verdict.field, object: verdict.object }; |
| 723 | + }; |
| 724 | + |
678 | 725 | /** The declared slice of `name` on `objectName` — authored, else registry-injected. */ |
679 | 726 | const fieldOf = (objectName: string, name: string) => { |
680 | 727 | const o = graph.get(objectName); |
@@ -975,6 +1022,21 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] |
975 | 1022 | prescription: slot.prescription, |
976 | 1023 | }); |
977 | 1024 | if (hit && !slot.ownFinding) reported.add(`${at}=${read.name}`); |
| 1025 | + |
| 1026 | + // [#34378] A path that names real fields can still read a LIST: a |
| 1027 | + // multi-valued head, or a multi-valued leaf behind one hop. The |
| 1028 | + // comparand is one value under a scalar operator, so it is refused. |
| 1029 | + if (!hit && slot.singleValued) { |
| 1030 | + const multi = multiValuedSegment(against, segments); |
| 1031 | + if (multi) { |
| 1032 | + refuseKey( |
| 1033 | + { where, path }, |
| 1034 | + `${subject} "${read.name}" ${multi.how} "${multi.field}", a multi-valued field on object ` |
| 1035 | + + `"${multi.object}"; a $record comparand is one value under a scalar operator.`, |
| 1036 | + slot.singleValuedPrescription ?? slot.prescription, |
| 1037 | + ); |
| 1038 | + } |
| 1039 | + } |
978 | 1040 | }); |
979 | 1041 | } |
980 | 1042 |
|
|
0 commit comments