diff --git a/.changeset/37936-depends-on-filter-key-tree.md b/.changeset/37936-depends-on-filter-key-tree.md new file mode 100644 index 00000000000..6e9c9ace553 --- /dev/null +++ b/.changeset/37936-depends-on-filter-key-tree.md @@ -0,0 +1,17 @@ +--- +'@objectstack/lint': minor +'@objectstack/spec': patch +--- + +fix(lint)!: a tree field's `dependsOn` filter key is judged like a lookup's + +Clause-②: no (narrowing) +Metadata-protocol: narrowed + +An import scopes a reference cell by the field's `dependsOn` entries on `lookup`, `master_detail`, `user` and `tree` fields, the spec's reference types (`REFERENCE_VALUE_TYPES`, `@objectstack/spec/data`). Rule `object-field-ref-unknown` judged the filter half of a `dependsOn` entry (`param`, or the bare name) only on `lookup`, `master_detail` and `user`, so on a `tree` field a filter key that names no field, or names a field computed on read such as a `formula`, published clean and every import of the column failed its lookup. The rule now judges that position on all four types, reading the set from `@objectstack/spec/data` rather than restating it, so a tree field's filter key is refused at `error` on `os validate` and at the runtime publish gate, against the object that declares the tree. A tree field that names no `reference` is judged against its own object too, the self-reference the registry stamps on it, so the runtime publish gate refuses it on the raw body it receives. `lookupColumns` and `lookupFilters` are still judged only on the three picker types. + +To fix a refused entry, point `param` at a stored field of the object that declares the tree, written as `{ field: 'this_record_field', param: 'stored_field' }`, fix the misspelt name, or drop the entry. + +The `dependsOn` description in `@objectstack/spec` now names every reference type it scopes, and the ADR-0087 ledger gains the semantic entry `lookup-depends-on-param-computed-field-refused-on-tree`. + + diff --git a/content/docs/data-modeling/import-mappings.mdx b/content/docs/data-modeling/import-mappings.mdx index e2dff2c5e00..3841d7a155d 100644 --- a/content/docs/data-modeling/import-mappings.mdx +++ b/content/docs/data-modeling/import-mappings.mdx @@ -376,10 +376,10 @@ the scope and names the cause (`Task: "Design review" could not be looked up wit "Gemini": the target field this lookup's dependsOn filters by cannot be filtered on, …`), never `reference_out_of_scope`. Point `param` at a stored field of the target. `os validate` and the runtime publish gate refuse such an entry at authoring (`object-field-ref-unknown`, -naming the target field). That check judges a `lookup`, `master_detail` or `user` field, against -the objects it can see, so an import still meets this sentence in three cases: on metadata stored -before the check; on a `tree` field or a legacy `reference` field, which the import scopes by -`dependsOn` too but the check does not judge; and after an `os validate` of a stack that does not +naming the target field). That check judges every field the import scopes, a `lookup`, +`master_detail`, `user` or `tree` field (a `tree` against its own object, with or without a +`reference`), against the objects it can see, so an import still meets this sentence in two +cases: on metadata stored before the check; and after an `os validate` of a stack that does not hold both objects, such as a target object from another package, or a target field turned into a formula in a package validated without the object whose lookup filters by it. The runtime publish gate judges against every object the environment serves, so a publish that turns such a target diff --git a/content/docs/references/data/field.mdx b/content/docs/references/data/field.mdx index c94c8d6d665..349cf06f66b 100644 --- a/content/docs/references/data/field.mdx +++ b/content/docs/references/data/field.mdx @@ -95,7 +95,7 @@ const result = CurrencyConfigSchema.parse(data); | **lookupColumns** | `(string \| { field: string; label?: string; width?: string; type?: string })[]` | optional | Explicit columns for the record-picker table; auto-derived from the referenced object when omitted. | | **lookupPageSize** | `integer` | optional | Rows per page in the record-picker dialog (default 10). | | **lookupFilters** | `{ field: string; operator: Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>; value: any; orEmpty?: boolean }[]` | optional | Base filters restricting which records are selectable (e.g. only active). The structured, picker-honoured lookup filter. Each entry compares a column of the CANDIDATE record (`field`, on the referenced object) with `value`. A value may read the record being edited as `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). In a record form, whatever its layout, the picker sends that value in its place and re-queries whenever a referenced field changes; 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 picker with no single record being edited (a bulk action over selected rows). The referenced fields are implied: do not list them in `dependsOn`, which also scopes the candidate query by equality (`dependsOn: ['quote_date']` adds `quote_date = …` to the query over the referenced object). While the value is empty or cannot be read (the hop's row or field does not come back, no object is named for the hop, or the host has no single-row reader), the entry is left out. A `{ $record }` is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and eq on a date, datetime or time column, is refused earlier, in generic words. The picker narrows the choice only: a rule the save must enforce belongs on the server too. | -| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | +| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | | **allowCreate** | `boolean` | optional | Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field. | | **lookupAutofill** | `{ target: string; source: string; overwrite?: boolean }[]` | optional | Fields a pick in this lookup fills on the record being edited: each `{ target, source }` pair copies the picked record's `source` field (a field of the `reference` object) into this object's `target` field — e.g. `[{ target: 'unit_price', source: 'list_price' }]` on a quote line's product lookup. Read wherever this field is picked: the record form, and a line-item grid column over it. OVERWRITE RULE — the default, `overwrite` absent or false: a pick never replaces a value somebody wrote. The target is filled only while it is empty or still holds the value an earlier pick filled in; a value the user typed, or one the record already stored, stays. Only a pair that declares `overwrite: true` replaces the target on every pick, whatever it holds. CURRENCY RULE: an amount of money (a `currency` field, or a `formula` returning `currency`) is copied only into a target in the same currency, both in the home currency or both naming the same ISO 4217 code (a `fixed` code, or the code the record or its `master_detail` parent holds). Otherwise the target is left as it is and the form says why, naming both currencies and the picked amount; nothing is converted. A `number` carries no currency and copies as before. Client-side only: it fills the form, and nothing on the write path reads it — the filled value is saved as an ordinary write, and an import or API write that sets the lookup fills nothing. Only on a single-value `lookup` / `master_detail`. Each target is a field of this object other than this one, written by its author — not `readonly`, not computed (`formula`, `summary`, `autonumber`, or an `expression`) — and named once. Each source is a field of the referenced object that the record carries (not `internal`) and whose value the target can hold; `os validate` / `os build` judge the source. In a line-item grid the column key `autofill` still governs only the same-named copy, under the same currency rule; a declared pair takes its target out of that copy. | | **expression** | `string \| { dialect: Enum<'cel' \| 'cron' \| 'template'>; source: string; ast?: any; meta?: object }` | optional | CEL expression the field's value is computed from, e.g. F`record.amount * 0.1`. On a `formula` field: computed on read, no column. On any other (stored) field: computed by the engine from the row the write stores and stored, so a client-sent value never stands and a `summary` can roll the column up — on every insert and every update by id (the stored row merged with the patch), and on a predicate update only when the patch moves an input (one value for every matched row, else the update is refused). | diff --git a/content/docs/references/data/object.mdx b/content/docs/references/data/object.mdx index 13c27fe5b97..d3121fd2aa1 100644 --- a/content/docs/references/data/object.mdx +++ b/content/docs/references/data/object.mdx @@ -284,7 +284,7 @@ const result = ApiMethod.parse(data); | **lookupColumns** | `(string \| { field: string; label?: string; width?: string; type?: string })[]` | optional | Explicit columns for the record-picker table; auto-derived from the referenced object when omitted. | | **lookupPageSize** | `integer` | optional | Rows per page in the record-picker dialog (default 10). | | **lookupFilters** | `{ field: string; operator: Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>; value: any; orEmpty?: boolean }[]` | optional | Base filters restricting which records are selectable (e.g. only active). The structured, picker-honoured lookup filter. Each entry compares a column of the CANDIDATE record (`field`, on the referenced object) with `value`. A value may read the record being edited as `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). In a record form, whatever its layout, the picker sends that value in its place and re-queries whenever a referenced field changes; 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 picker with no single record being edited (a bulk action over selected rows). The referenced fields are implied: do not list them in `dependsOn`, which also scopes the candidate query by equality (`dependsOn: ['quote_date']` adds `quote_date = …` to the query over the referenced object). While the value is empty or cannot be read (the hop's row or field does not come back, no object is named for the hop, or the host has no single-row reader), the entry is left out. A `{ $record }` is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and eq on a date, datetime or time column, is refused earlier, in generic words. The picker narrows the choice only: a rule the save must enforce belongs on the server too. | -| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | +| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | | **allowCreate** | `boolean` | optional | Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field. | | **lookupAutofill** | `{ target: string; source: string; overwrite?: boolean }[]` | optional | Fields a pick in this lookup fills on the record being edited: each `{ target, source }` pair copies the picked record's `source` field (a field of the `reference` object) into this object's `target` field — e.g. `[{ target: 'unit_price', source: 'list_price' }]` on a quote line's product lookup. Read wherever this field is picked: the record form, and a line-item grid column over it. OVERWRITE RULE — the default, `overwrite` absent or false: a pick never replaces a value somebody wrote. The target is filled only while it is empty or still holds the value an earlier pick filled in; a value the user typed, or one the record already stored, stays. Only a pair that declares `overwrite: true` replaces the target on every pick, whatever it holds. CURRENCY RULE: an amount of money (a `currency` field, or a `formula` returning `currency`) is copied only into a target in the same currency, both in the home currency or both naming the same ISO 4217 code (a `fixed` code, or the code the record or its `master_detail` parent holds). Otherwise the target is left as it is and the form says why, naming both currencies and the picked amount; nothing is converted. A `number` carries no currency and copies as before. Client-side only: it fills the form, and nothing on the write path reads it — the filled value is saved as an ordinary write, and an import or API write that sets the lookup fills nothing. Only on a single-value `lookup` / `master_detail`. Each target is a field of this object other than this one, written by its author — not `readonly`, not computed (`formula`, `summary`, `autonumber`, or an `expression`) — and named once. Each source is a field of the referenced object that the record carries (not `internal`) and whose value the target can hold; `os validate` / `os build` judge the source. In a line-item grid the column key `autofill` still governs only the same-named copy, under the same currency rule; a declared pair takes its target out of that copy. | | **expression** | `string \| { dialect: Enum<'cel' \| 'cron' \| 'template'>; source: string; ast?: any; meta?: object }` | optional | CEL expression the field's value is computed from, e.g. F`record.amount * 0.1`. On a `formula` field: computed on read, no column. On any other (stored) field: computed by the engine from the row the write stores and stored, so a client-sent value never stands and a `summary` can roll the column up — on every insert and every update by id (the stored row merged with the patch), and on a predicate update only when the patch moves an input (one value for every matched row, else the update is refused). | @@ -685,7 +685,7 @@ const result = ApiMethod.parse(data); | **lookupColumns** | `(string \| { field: string; label?: string; width?: string; type?: string })[]` | optional | Explicit columns for the record-picker table; auto-derived from the referenced object when omitted. | | **lookupPageSize** | `integer` | optional | Rows per page in the record-picker dialog (default 10). | | **lookupFilters** | `{ field: string; operator: Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>; value: any; orEmpty?: boolean }[]` | optional | Base filters restricting which records are selectable (e.g. only active). The structured, picker-honoured lookup filter. Each entry compares a column of the CANDIDATE record (`field`, on the referenced object) with `value`. A value may read the record being edited as `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). In a record form, whatever its layout, the picker sends that value in its place and re-queries whenever a referenced field changes; 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 picker with no single record being edited (a bulk action over selected rows). The referenced fields are implied: do not list them in `dependsOn`, which also scopes the candidate query by equality (`dependsOn: ['quote_date']` adds `quote_date = …` to the query over the referenced object). While the value is empty or cannot be read (the hop's row or field does not come back, no object is named for the hop, or the host has no single-row reader), the entry is left out. A `{ $record }` is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and eq on a date, datetime or time column, is refused earlier, in generic words. The picker narrows the choice only: a rule the save must enforce belongs on the server too. | -| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | +| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | | **allowCreate** | `boolean` | optional | Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field. | | **lookupAutofill** | `{ target: string; source: string; overwrite?: boolean }[]` | optional | Fields a pick in this lookup fills on the record being edited: each `{ target, source }` pair copies the picked record's `source` field (a field of the `reference` object) into this object's `target` field — e.g. `[{ target: 'unit_price', source: 'list_price' }]` on a quote line's product lookup. Read wherever this field is picked: the record form, and a line-item grid column over it. OVERWRITE RULE — the default, `overwrite` absent or false: a pick never replaces a value somebody wrote. The target is filled only while it is empty or still holds the value an earlier pick filled in; a value the user typed, or one the record already stored, stays. Only a pair that declares `overwrite: true` replaces the target on every pick, whatever it holds. CURRENCY RULE: an amount of money (a `currency` field, or a `formula` returning `currency`) is copied only into a target in the same currency, both in the home currency or both naming the same ISO 4217 code (a `fixed` code, or the code the record or its `master_detail` parent holds). Otherwise the target is left as it is and the form says why, naming both currencies and the picked amount; nothing is converted. A `number` carries no currency and copies as before. Client-side only: it fills the form, and nothing on the write path reads it — the filled value is saved as an ordinary write, and an import or API write that sets the lookup fills nothing. Only on a single-value `lookup` / `master_detail`. Each target is a field of this object other than this one, written by its author — not `readonly`, not computed (`formula`, `summary`, `autonumber`, or an `expression`) — and named once. Each source is a field of the referenced object that the record carries (not `internal`) and whose value the target can hold; `os validate` / `os build` judge the source. In a line-item grid the column key `autofill` still governs only the same-named copy, under the same currency rule; a declared pair takes its target out of that copy. | | **expression** | `string \| { dialect: Enum<'cel' \| 'cron' \| 'template'>; source: string; ast?: any; meta?: object }` | optional | CEL expression the field's value is computed from, e.g. F`record.amount * 0.1`. On a `formula` field: computed on read, no column. On any other (stored) field: computed by the engine from the row the write stores and stored, so a client-sent value never stands and a `summary` can roll the column up — on every insert and every update by id (the stored row merged with the patch), and on a predicate update only when the patch moves an input (one value for every matched row, else the update is refused). | diff --git a/packages/lint/src/rule-explanations.ts b/packages/lint/src/rule-explanations.ts index 2584f6eff71..4898863f37f 100644 --- a/packages/lint/src/rule-explanations.ts +++ b/packages/lint/src/rule-explanations.ts @@ -4896,7 +4896,9 @@ const OBJECT_FIELD_REF_UNKNOWN_EXPLANATION: RuleExplanation = { 'not have never gets one, so it stays gated for good) and, through `param` or the bare name, ' + 'against the referenced object ' + '(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 ' + + '`lookup`, `master_detail` and `user` fields, the types that render the picker, except for the ' + + '`dependsOn` filter key, which is also judged on a `tree` field, since an import scopes a tree ' + + 'cell by it too; a bare ' + '`dependsOn` name that misses on the owning object is reported once, there. The `dependsOn` ' + 'filter key is refused too when it names a field of the referenced object computed on read, ' + 'such as a `formula`: the engine cannot filter on it, so every picker query the entry scopes is ' + diff --git a/packages/lint/src/validate-object-field-refs.test.ts b/packages/lint/src/validate-object-field-refs.test.ts index 443e750ccc5..113259753d4 100644 --- a/packages/lint/src/validate-object-field-refs.test.ts +++ b/packages/lint/src/validate-object-field-refs.test.ts @@ -1248,6 +1248,94 @@ describe('validateObjectFieldRefs — dependsOn (the gate on the OWNER, the filt expect(refusal!.path).toBe('objects.crm_invoice.fields.contact.dependsOn[0].param'); }); + // [#37936] The importer scopes an import cell by `dependsOn` on every spec + // reference type, not only the picker types: the filter half is judged on + // that set, which adds `tree` (a self-reference). `lookupColumns` and + // `lookupFilters` keep the picker set — the importer reads neither. + const treeOf = (over: Record) => ({ + objects: [invoice({ + region_calc: { type: 'formula', expression: 'UPPER(region)' }, + parent: { type: 'tree', reference: 'crm_invoice', ...over }, + })], + }); + + it('REFUSES a tree field\'s filter key naming a formula of its own object', () => { + const findings = validateObjectFieldRefs(treeOf({ dependsOn: [{ field: 'region', param: 'region_calc' }] })); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ + severity: 'error', + rule: OBJECT_FIELD_REF_UNKNOWN, + path: 'objects[0].fields.parent.dependsOn[0].param', + }); + expect(findings[0]!.message).toContain('"region_calc" is a formula field of "crm_invoice", computed on read'); + + // The bare name is the filter key too. + expect(validateObjectFieldRefs(treeOf({ dependsOn: ['region_calc'] })).map((f) => f.path)) + .toEqual(['objects[0].fields.parent.dependsOn[0]']); + }); + + it('REFUSES a tree field\'s misspelt filter key, and passes a stored one', () => { + const findings = validateObjectFieldRefs(treeOf({ dependsOn: [{ field: 'region', param: 'regon' }] })); + expect(findings.map((f) => f.path)).toEqual(['objects[0].fields.parent.dependsOn[0].param']); + expect(findings[0]!.message).toContain('object "crm_invoice"'); + + expect(validateObjectFieldRefs(treeOf({ dependsOn: ['region', { field: 'total', param: 'total' }] }))).toEqual([]); + }); + + it('keeps lookupColumns and lookupFilters unjudged on a tree field (the picker set is unchanged)', () => { + expect(validateObjectFieldRefs(treeOf({ + lookupColumns: ['no_such_column'], + lookupFilters: [{ field: 'no_such_field', operator: 'eq', value: 'x' }], + }))).toEqual([]); + }); + + it('refuses a tree field\'s formula filter key at the runtime publish gate too', () => { + const stack = treeOf({ dependsOn: [{ field: 'region', param: 'region_calc' }] }); + const result = runRuntimeAuthoringRules({ type: 'object', item: stack.objects[0], context: { objects: [] } }); + const refusal = result.errors.find((f) => f.rule === OBJECT_FIELD_REF_UNKNOWN); + expect(refusal, JSON.stringify(result.errors)).toBeDefined(); + expect(refusal!.path).toBe('objects.crm_invoice.fields.parent.dependsOn[0].param'); + }); + + // A tree that names no `reference` is a self-reference: the registry stamps + // its own object's name on it (`materializeTreeSelfReferences`), so the + // import scopes it against that object. The runtime save door passes the + // raw body, so the slot supplies the same target itself. + const bareTreeOf = (dependsOn: unknown[]) => ({ + objects: [invoice({ + region_calc: { type: 'formula', expression: 'UPPER(region)' }, + parent: { type: 'tree', dependsOn }, + })], + }); + + it('judges a tree field that names no `reference` against its own object (the os validate path)', () => { + const findings = validateObjectFieldRefs(bareTreeOf([{ field: 'region', param: 'region_calc' }])); + expect(findings.map((f) => f.path)).toEqual(['objects[0].fields.parent.dependsOn[0].param']); + expect(findings[0]!.message).toContain('"region_calc" is a formula field of "crm_invoice", computed on read'); + + expect(validateObjectFieldRefs(bareTreeOf([{ field: 'region', param: 'regon' }])).map((f) => f.path)) + .toEqual(['objects[0].fields.parent.dependsOn[0].param']); + expect(validateObjectFieldRefs(bareTreeOf(['region']))).toEqual([]); + }); + + it('refuses a reference-less tree field\'s formula filter key at the runtime publish gate (raw body)', () => { + const stack = bareTreeOf([{ field: 'region', param: 'region_calc' }]); + const result = runRuntimeAuthoringRules({ type: 'object', item: stack.objects[0], context: { objects: [] } }); + const refusal = result.errors.find((f) => f.rule === OBJECT_FIELD_REF_UNKNOWN); + expect(refusal, JSON.stringify(result.errors)).toBeDefined(); + expect(refusal!.path).toBe('objects.crm_invoice.fields.parent.dependsOn[0].param'); + }); + + it('a legacy `reference` field names no target, so its filter key stays unjudged (and nothing scopes it)', () => { + // `referenceTargetOf` answers only the spec's reference types, so neither + // this rule nor the importer's field map has a target for it, and + // `FieldSchema` refuses the spelling at the door. + expect(FieldSchema.safeParse({ type: 'reference', reference: 'crm_account' }).success).toBe(false); + expect(validateObjectFieldRefs(twoObjects({ + contact: { type: 'reference', reference: 'crm_account', dependsOn: [{ field: 'region', param: 'regon' }] }, + }))).toEqual([]); + }); + it('a registry-injected column on the owner is a live gate (skip 3)', () => { expect(validateObjectFieldRefs({ objects: [invoice({ note: { type: 'text', dependsOn: ['owner_id'] }, diff --git a/packages/lint/src/validate-object-field-refs.ts b/packages/lint/src/validate-object-field-refs.ts index 307e7d993c7..0007539aa86 100644 --- a/packages/lint/src/validate-object-field-refs.ts +++ b/packages/lint/src/validate-object-field-refs.ts @@ -137,7 +137,10 @@ * `isVirtualSearchField`, #37717): the picker's candidate query and the * importer's scoped lookup filter by it, and the engine's filter seam * refuses every such filter, so the picker gets no answer and every import - * of the column fails its lookup. + * of the column fails its lookup. The filter half is judged on every type + * the importer scopes by `dependsOn` (`REFERENCE_VALUE_TYPES`, #37936): + * the three picker types below, plus `tree` — a tree that names no + * `reference` judged against its own object, as the registry stamps it. * - **`summaryOperations.filter`** — every field key of a roll-up's child-row * predicate (#31224), judged against the CHILD object * `summaryOperations.object` names, never the object that owns the summary @@ -216,7 +219,12 @@ * graph's `GraphField.reference`), judged only on the three types that render * that picker — `lookup` and `master_detail` (`FieldEditWidget.tsx`) and * `user`, whose `UserField` delegates to it with `sys_user` fixed. On any - * other type the REFERENCE positions have no reader and stay unjudged. + * other type the REFERENCE positions have no reader and stay unjudged, with + * one exception: the filter half of `dependsOn` is also read by the importer, + * which scopes an import cell by it on `tree` (a self-reference) too, so that + * one position is judged on the importer's set (#37936), and a tree that names + * no `reference` is judged against its own object, the target the registry + * stamps on it. * * `lookupColumns`, `dependsOn` and `indexes[].fields` are read VERBATIM by * their readers — a picker column key, a record key, a physical column — so @@ -311,6 +319,7 @@ import { classifyDottedFilterHead, isMultiValueField, isVirtualSearchField, + REFERENCE_VALUE_TYPES, STRUCTURED_JSON_TYPES, } from '@objectstack/spec/data'; @@ -451,6 +460,22 @@ type NameAddress = 'owner' | 'reference'; */ const PICKER_FIELD_TYPES: ReadonlySet = new Set(['lookup', 'master_detail', 'user']); +/** + * [#37936] The field types whose `dependsOn` filter half has a reader: every + * type an import scopes a cell by `dependsOn` on (`referenceScopeSources`, + * `packages/core/src/utils/import-coerce.ts`). That is {@link PICKER_FIELD_TYPES} + * plus `tree`, which the console also renders with the lookup widget that + * reads `dependsOn` — the spec's reference types, `REFERENCE_VALUE_TYPES` + * (`@objectstack/spec/data`), imported, never restated. The importer's own + * gate, `IMPORT_REFERENCE_TYPES`, also admits the legacy `reference` spelling, + * but that member is inert: `buildFieldMetaMap` takes the target from + * `referenceTargetOf`, which answers none for it, so no `reference` cell is + * ever scoped. Only this slot reads the set: `lookupColumns` and + * `lookupFilters` keep {@link PICKER_FIELD_TYPES}, since the importer reads + * neither. + */ +const DEPENDS_ON_SCOPED_TYPES: ReadonlySet = REFERENCE_VALUE_TYPES; + /** One name read out of one list entry, and where it sits inside the entry. */ interface SlotRead { name: string; @@ -482,10 +507,26 @@ interface FieldNameSlot { /** * [#34200] `true` when the slot's only reader is the record picker, so it is * judged only on {@link PICKER_FIELD_TYPES} even though its names address - * the OWNER. (A REFERENCE slot needs the picker's target anyway, so it is - * judged on those types alone without saying so.) + * the OWNER. (A REFERENCE slot needs the field's target anyway, so it is + * judged on {@link referenceTypes} alone without saying so.) */ pickerOnly?: boolean; + /** + * [#37936] At a REFERENCE slot, the field types whose readers read it, and so + * the types on which the field's target is the object it is judged against. + * Unset = {@link PICKER_FIELD_TYPES}. + */ + referenceTypes?: ReadonlySet; + /** + * [#37936] `true` when a `tree` field that names no `reference` is judged + * against the object that declares it, as the registry reads it: + * `materializeTreeSelfReferences` (`@objectstack/spec/data`) stamps that + * self-reference when the object is registered, so the importer scopes such + * a field against its own object, while a body the parse has not seen (the + * runtime save door passes the raw body) gives the graph no target. Set on + * this one slot only. + */ + treeSelfTarget?: boolean; /** * [#34200] The most relationship hops a path may take before its leaf, at a * path-resolved (`verbatim: false`) position whose reader walks a bounded @@ -623,6 +664,10 @@ const FIELD_NAME_SLOTS: readonly FieldNameSlot[] = [ address: 'reference', verbatim: true, read: (entry) => readString(entry) ?? readMember(entry, 'param') ?? readMember(entry, 'field'), + // [#37936] The importer scopes by this key on every type in its set, not + // only the picker types, so the slot is judged on that set. + referenceTypes: DEPENDS_ON_SCOPED_TYPES, + treeSelfTarget: true, prescription: 'When the two sides are spelled differently, write the entry as ' + '`{ field: \'this_record_field\', param: \'referenced_object_field\' }`; otherwise fix ' @@ -1025,7 +1070,14 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] const fieldName = nameOf(field.name); if (!fieldName) continue; const meta = owner.fields.get(fieldName); - const target = meta?.type && PICKER_FIELD_TYPES.has(meta.type) ? meta.reference : undefined; + /** The field's target, on the types whose readers read the slot's REFERENCE names. */ + const targetOn = (slot: FieldNameSlot): string | undefined => { + if (!meta?.type || !(slot.referenceTypes ?? PICKER_FIELD_TYPES).has(meta.type)) return undefined; + if (meta.reference) return meta.reference; + // A tree is a self-reference: the registry writes its own object's name + // onto a tree that names none (`materializeTreeSelfReferences`). + return slot.treeSelfTarget && meta.type === 'tree' ? objName : undefined; + }; // A name already reported at an entry is not reported a second time // against the other object (the `dependsOn` pair): one fix answers both. @@ -1035,7 +1087,7 @@ export function validateObjectFieldRefs(stack: AnyRec): ObjectFieldRefFinding[] const list = field[slot.key]; if (!Array.isArray(list)) continue; if (slot.pickerOnly && !(meta?.type && PICKER_FIELD_TYPES.has(meta.type))) continue; - const against = slot.address === 'owner' ? objName : target; + const against = slot.address === 'owner' ? objName : targetOn(slot); if (!against) continue; list.forEach((entry, i) => { diff --git a/packages/spec/docs/references/system/migration.md b/packages/spec/docs/references/system/migration.md index 56298b61be3..1a2259cec20 100644 --- a/packages/spec/docs/references/system/migration.md +++ b/packages/spec/docs/references/system/migration.md @@ -97,7 +97,7 @@ Add a new field to an existing object | **lookupColumns** | `(string \| { field: string; label?: string; width?: string; type?: string })[]` | optional | Explicit columns for the record-picker table; auto-derived from the referenced object when omitted. | | **lookupPageSize** | `integer` | optional | Rows per page in the record-picker dialog (default 10). | | **lookupFilters** | `{ field: string; operator: Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>; value: any; orEmpty?: boolean }[]` | optional | Base filters restricting which records are selectable (e.g. only active). The structured, picker-honoured lookup filter. Each entry compares a column of the CANDIDATE record (`field`, on the referenced object) with `value`. A value may read the record being edited as `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). In a record form, whatever its layout, the picker sends that value in its place and re-queries whenever a referenced field changes; 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 picker with no single record being edited (a bulk action over selected rows). The referenced fields are implied: do not list them in `dependsOn`, which also scopes the candidate query by equality (`dependsOn: ['quote_date']` adds `quote_date = …` to the query over the referenced object). While the value is empty or cannot be read (the hop's row or field does not come back, no object is named for the hop, or the host has no single-row reader), the entry is left out. A `{ $record }` is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and eq on a date, datetime or time column, is refused earlier, in generic words. The picker narrows the choice only: a rule the save must enforce belongs on the server too. | -| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | +| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | | **allowCreate** | `boolean` | optional | Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field. | | **lookupAutofill** | `{ target: string; source: string; overwrite?: boolean }[]` | optional | Fields a pick in this lookup fills on the record being edited: each `{ target, source }` pair copies the picked record's `source` field (a field of the `reference` object) into this object's `target` field — e.g. `[{ target: 'unit_price', source: 'list_price' }]` on a quote line's product lookup. Read wherever this field is picked: the record form, and a line-item grid column over it. OVERWRITE RULE — the default, `overwrite` absent or false: a pick never replaces a value somebody wrote. The target is filled only while it is empty or still holds the value an earlier pick filled in; a value the user typed, or one the record already stored, stays. Only a pair that declares `overwrite: true` replaces the target on every pick, whatever it holds. CURRENCY RULE: an amount of money (a `currency` field, or a `formula` returning `currency`) is copied only into a target in the same currency, both in the home currency or both naming the same ISO 4217 code (a `fixed` code, or the code the record or its `master_detail` parent holds). Otherwise the target is left as it is and the form says why, naming both currencies and the picked amount; nothing is converted. A `number` carries no currency and copies as before. Client-side only: it fills the form, and nothing on the write path reads it — the filled value is saved as an ordinary write, and an import or API write that sets the lookup fills nothing. Only on a single-value `lookup` / `master_detail`. Each target is a field of this object other than this one, written by its author — not `readonly`, not computed (`formula`, `summary`, `autonumber`, or an `expression`) — and named once. Each source is a field of the referenced object that the record carries (not `internal`) and whose value the target can hold; `os validate` / `os build` judge the source. In a line-item grid the column key `autofill` still governs only the same-named copy, under the same currency rule; a declared pair takes its target out of that copy. | | **expression** | `string \| { dialect: Enum<'cel' \| 'cron' \| 'template'>; source: string; ast?: any; meta?: object }` | optional | CEL expression the field's value is computed from, e.g. F`record.amount * 0.1`. On a `formula` field: computed on read, no column. On any other (stored) field: computed by the engine from the row the write stores and stored, so a client-sent value never stands and a `summary` can roll the column up — on every insert and every update by id (the stored row merged with the patch), and on a predicate update only when the patch moves an input (one value for every matched row, else the update is refused). | diff --git a/packages/spec/docs/references/system/migration.part-2.md b/packages/spec/docs/references/system/migration.part-2.md index 04f2782ebe9..7f0d63e9959 100644 --- a/packages/spec/docs/references/system/migration.part-2.md +++ b/packages/spec/docs/references/system/migration.part-2.md @@ -76,7 +76,7 @@ Add a new field to an existing object | **lookupColumns** | `(string \| { field: string; label?: string; width?: string; type?: string })[]` | optional | Explicit columns for the record-picker table; auto-derived from the referenced object when omitted. | | **lookupPageSize** | `integer` | optional | Rows per page in the record-picker dialog (default 10). | | **lookupFilters** | `{ field: string; operator: Enum<'eq' \| 'ne' \| 'gt' \| 'lt' \| 'gte' \| 'lte' \| 'contains' \| 'in' \| 'notIn'>; value: any; orEmpty?: boolean }[]` | optional | Base filters restricting which records are selectable (e.g. only active). The structured, picker-honoured lookup filter. Each entry compares a column of the CANDIDATE record (`field`, on the referenced object) with `value`. A value may read the record being edited as `{ $record: "field" }` or `{ $record: "lookup_field.field" }` (at most one lookup hop). In a record form, whatever its layout, the picker sends that value in its place and re-queries whenever a referenced field changes; 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 picker with no single record being edited (a bulk action over selected rows). The referenced fields are implied: do not list them in `dependsOn`, which also scopes the candidate query by equality (`dependsOn: ['quote_date']` adds `quote_date = …` to the query over the referenced object). While the value is empty or cannot be read (the hop's row or field does not come back, no object is named for the hop, or the host has no single-row reader), the entry is left out. A `{ $record }` is the whole value, never a member of an `in` / `notIn` list. `orEmpty: true` also matches a candidate whose column is null. A `{ $record }` that reaches a server unresolved is refused (INVALID_FILTER), never read as null: by name, naming the field and the path, when it arrives as the eq shorthand on a column other than a date, datetime or time column; any other operator, and eq on a date, datetime or time column, is refused earlier, in generic words. The picker narrows the choice only: a rule the save must enforce belongs on the server too. | -| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | +| **dependsOn** | `(string \| { field: string; param?: string })[]` | optional | Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; `{field,param}` when the remote filter key differs — the `{field,param}` form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent. | | **allowCreate** | `boolean` | optional | Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field. | | **lookupAutofill** | `{ target: string; source: string; overwrite?: boolean }[]` | optional | Fields a pick in this lookup fills on the record being edited: each `{ target, source }` pair copies the picked record's `source` field (a field of the `reference` object) into this object's `target` field — e.g. `[{ target: 'unit_price', source: 'list_price' }]` on a quote line's product lookup. Read wherever this field is picked: the record form, and a line-item grid column over it. OVERWRITE RULE — the default, `overwrite` absent or false: a pick never replaces a value somebody wrote. The target is filled only while it is empty or still holds the value an earlier pick filled in; a value the user typed, or one the record already stored, stays. Only a pair that declares `overwrite: true` replaces the target on every pick, whatever it holds. CURRENCY RULE: an amount of money (a `currency` field, or a `formula` returning `currency`) is copied only into a target in the same currency, both in the home currency or both naming the same ISO 4217 code (a `fixed` code, or the code the record or its `master_detail` parent holds). Otherwise the target is left as it is and the form says why, naming both currencies and the picked amount; nothing is converted. A `number` carries no currency and copies as before. Client-side only: it fills the form, and nothing on the write path reads it — the filled value is saved as an ordinary write, and an import or API write that sets the lookup fills nothing. Only on a single-value `lookup` / `master_detail`. Each target is a field of this object other than this one, written by its author — not `readonly`, not computed (`formula`, `summary`, `autonumber`, or an `expression`) — and named once. Each source is a field of the referenced object that the record carries (not `internal`) and whose value the target can hold; `os validate` / `os build` judge the source. In a line-item grid the column key `autofill` still governs only the same-named copy, under the same currency rule; a declared pair takes its target out of that copy. | | **expression** | `string \| { dialect: Enum<'cel' \| 'cron' \| 'template'>; source: string; ast?: any; meta?: object }` | optional | CEL expression the field's value is computed from, e.g. F`record.amount * 0.1`. On a `formula` field: computed on read, no column. On any other (stored) field: computed by the engine from the row the write stores and stored, so a client-sent value never stands and a `summary` can roll the column up — on every insert and every update by id (the stored row merged with the patch), and on a predicate update only when the patch moves an input (one value for every matched row, else the update is refused). | diff --git a/packages/spec/src/data/field.zod.ts b/packages/spec/src/data/field.zod.ts index f9a7d525065..5008ac09d6a 100644 --- a/packages/spec/src/data/field.zod.ts +++ b/packages/spec/src/data/field.zod.ts @@ -2668,7 +2668,7 @@ export const FieldSchema = lazySchema(() => { }, { field: z.string(), param: z.string().optional(), - })])).optional().describe("Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For `lookup`/`master_detail` it scopes the candidate query (string = same local/remote key; {field,param} when the remote filter key differs — the {field,param} form is lookup-only). For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent."), + })])).optional().describe("Declares that this field's available values depend on the value of other field(s) on the same record — the form gates the field until they are set and re-evaluates as they change. For a reference field (`lookup`, `master_detail`, `user`, `tree`) it scopes the candidate query and an import's lookup of the column by equality on the referenced object (string = same local/remote key; {field,param} when the remote filter key differs — the {field,param} form is reference-only), so that key must be a stored field of the referenced object, never one computed on read. For `select`/`multiselect`/`radio` the actual per-option rule lives in each option's `visibleWhen`; list the referenced fields here (string form) so the option list gates and refreshes with the parent."), allowCreate: z.boolean().optional().describe('Allow inline quick-create from the record picker: when no match exists the user can create a record from the typed text (optimistic dataSource.create with the display field). Best for simple objects whose only required field is the display field.'), /** diff --git a/packages/spec/src/migrations/entries/semantic/19.lookup-depends-on-param-computed-field-refused-on-tree.ts b/packages/spec/src/migrations/entries/semantic/19.lookup-depends-on-param-computed-field-refused-on-tree.ts new file mode 100644 index 00000000000..c8d842ef360 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/19.lookup-depends-on-param-computed-field-refused-on-tree.ts @@ -0,0 +1,35 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// #37936 — the `dependsOn` filter-key check of +// `lookup-depends-on-param-computed-field-refused` now judges every type the +// importer scopes an import cell by `dependsOn` on (the spec's reference types, +// `REFERENCE_VALUE_TYPES`), which adds `tree`: its filter key, on the object +// that declares it (a tree is a self-reference, with or without `reference`), +// is refused when it names a field computed on read or no field at all. Measured first-party exposure at this change: zero — no tree +// field in objectstack, hotcrm or cloud writes `dependsOn`. Not converted: +// which stored field holds the value the author meant is the author's call. +export const entry: SemanticMigration = { + id: 'lookup-depends-on-param-computed-field-refused-on-tree', + surface: + 'object.fields..dependsOn[] on a tree field, with or without `reference` — the filter key the entry scopes the object that declares ' + + 'the tree by (`param` in the { field, param } form, else the bare name or `field`), when it names no field of ' + + 'that object, or a field of it computed on read (a formula)', + replacement: + 'a stored field of the object that declares the tree, holding the same value, written as { field: ' + + '\'this_record_field\', param: \'stored_field\' }; fix a misspelt name; or drop the entry when the picker and ' + + 'the import need not be scoped.', + reason: + 'An import scopes a tree cell by this key, as it does a lookup cell, and the console renders a tree field with ' + + 'the lookup picker, which filters its candidates by it; the engine refuses every filter on a field computed on ' + + 'read. The check judged the key only on lookup, master_detail and user fields, so on a tree field such an entry ' + + 'published clean and every import of the column failed its lookup. object-field-ref-unknown now reports it as ' + + 'an error, on objectstack validate and at the runtime publish gate; a tree field that names no `reference` is ' + + 'judged against the object that declares it, the self-reference the registry stamps on it. No D2 conversion ' + + 'exists: only the author knows which stored field carries the value.', + acceptanceCriteria: + 'Run objectstack validate: no object-field-ref-unknown finding at fields.NAME.dependsOn[N] of a tree field. For ' + + 'each entry it named, the filter key is a stored field of the object that declares the tree, or the entry is ' + + 'gone, and importing the column resolves its cells within the scope.', +};