Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/36794-reference-out-of-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/spec': minor
'@objectstack/core': patch
'@objectstack/runtime': patch
---

feat(spec,core): an import lookup cell that names a record under another parent answers its own reason, `reference_out_of_scope`, with the scoping columns as data

Clause-②: yes (widening)
Metadata-protocol: added (`ValidateDataIssueSchema.scope`, a new optional key on the import cell error; the new `FieldErrorCode` value is a wire code, no author-writable key)
Skills-docs: follow-up owed — skills/objectstack-data/rules/relationships.md

`FieldErrorCode` (ADR-0114) gains `reference_out_of_scope`. The importer answers it for a SCOPED lookup cell (a `dependsOn` column, or the master the same row sets) whose value matches no record within the row's scope while it does match a record outside it: the row names a record that lives under another parent. Until now that cell answered `reference_not_found`, the same reason as a value no record has, and only the message told the two apart (`Contract: no record matches "CTR-0006" (Account = "Acme")`); a client that keys its text on the reason, such as the Import Wizard's dry run, could only say "no matching record". The message is unchanged in English and has its own catalog key, `import_reference_out_of_scope`, in all four platform locales (a deployment overrides it under `validation.field.import_reference_out_of_scope`).

The cell error, the element of an import row's `errors` (`ValidateDataIssueSchema`), now carries `scope` on that reason only: the row columns the lookup was resolved within, each `{ field, value }` with the cell as the importer typed it, so a client names the scoping column and its cell without parsing `message`.

Unchanged: a value that matches no record at all, scoped or not, still answers `reference_not_found` with the plain sentence and no `scope`. The out-of-scope probe is still the importing caller's own read, so a record that caller cannot read is never named and its cell answers `reference_not_found`.

What to do: a client that switches on `reference_not_found` to tell a user "no such record" keeps receiving it for a value no record has. A client that wants every unresolved lookup cell matches `reference_not_found` and `reference_out_of_scope` both.
2 changes: 1 addition & 1 deletion content/docs/api/error-catalog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ snake_case, so the code and the schema property are the same word.
| Presence and shape | `required`, `invalid_type`, `invalid_shape`, `unknown_field` |
| Per-type parse | `invalid_boolean`, `invalid_number`, `invalid_date`, `invalid_time`, `invalid_email`, `invalid_url`, `invalid_phone`, `invalid_json`, `invalid_format` |
| Bounded ranges | `min_length`, `max_length`, `min_value`, `max_value`, `max_scale`, `max_precision`, `min_items`, `max_items` |
| Closed sets and references | `invalid_option`, `value_domain` (the written value is not a member of the field's declared `valueDomain` standard), `invalid_value`, `reference_not_found`, `reference_ambiguous` |
| Closed sets and references | `invalid_option`, `value_domain` (the written value is not a member of the field's declared `valueDomain` standard), `invalid_value`, `reference_not_found`, `reference_ambiguous`, `reference_out_of_scope` (an import lookup cell whose value matches a record only under another parent than the row's own; the cell error names that parent's columns in `scope`) |
| Declarative rules | `rule_violation`, `json_schema_violation`, `invalid_initial_state`, `invalid_transition` |

Branch on `code` to decide *how* to mark an input; show `message` to the user.
Expand Down
9 changes: 7 additions & 2 deletions content/docs/data-modeling/import-mappings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,13 @@ The `master_detail` rule has no counterpart in the picker, which narrows by `dep
and `lookupFilters`, never by an inferred master: for a lookup without `dependsOn`, the
import is stricter than the form.

Inside that scope a tie is `reference_ambiguous` and a name that only another master
carries is `reference_not_found`; it never resolves to another master's record. A row
Inside that scope a tie is `reference_ambiguous`, and a name or id that only another
master carries is `reference_out_of_scope`: it never resolves to another master's record.
The row's sentence names the scoping columns and the cells the row gave them
(`Contract: no record matches "CTR-0006" (Account = "Acme")`), and the cell error carries
the same columns as data, `scope: [{ field, value }]`, so a client can say so in its own
words. A name that no record carries anywhere stays `reference_not_found`. Only a record
the importing caller can read is ever counted as "under another master". A row
that leaves the master blank is resolved across the whole target as before. When two
columns of the row point at the master object (a bill-to and a ship-to account), neither
is taken as the master; declare `dependsOn` on the lookup to say which one is. A master
Expand Down
4 changes: 3 additions & 1 deletion content/docs/references/api/errors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const result = EnhancedApiErrorSchema.parse(data);
| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **field** | `string` | ✅ | Field path (supports dot notation) |
| **code** | `Enum<'required' \| 'invalid_type' \| 'invalid_shape' \| 'unknown_field' \| 'invalid_boolean' \| 'invalid_number' \| 'invalid_date' \| 'invalid_time' \| 'invalid_email' \| … +22 more>` | ✅ | Which constraint the value violated (field-level catalog, ADR-0114) |
| **code** | `Enum<'required' \| 'invalid_type' \| 'invalid_shape' \| 'unknown_field' \| 'invalid_boolean' \| 'invalid_number' \| 'invalid_date' \| 'invalid_time' \| 'invalid_email' \| … +23 more>` | ✅ | Which constraint the value violated (field-level catalog, ADR-0114) |
| **message** | `string` | ✅ | Human-readable error message, rendered in the caller’s locale |
| **label** | `string` | optional | Field display label in the caller’s locale |
| **hint** | `string` | optional | The full prescription behind a one-line message, when there is more to say |
Expand Down Expand Up @@ -222,6 +222,7 @@ const result = EnhancedApiErrorSchema.parse(data);
* `invalid_value`
* `reference_not_found`
* `reference_ambiguous`
* `reference_out_of_scope`
* `reference_user_banned`
* `rule_violation`
* `json_schema_violation`
Expand Down Expand Up @@ -261,6 +262,7 @@ const result = EnhancedApiErrorSchema.parse(data);
* `invalid_value`
* `reference_not_found`
* `reference_ambiguous`
* `reference_out_of_scope`
* `reference_user_banned`
* `rule_violation`
* `json_schema_violation`
Expand Down
2 changes: 2 additions & 0 deletions content/docs/references/api/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ Type: `{ sourceField: string; targetField: string; targetLabel?: string; transfo
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |

### Nested Shape: `ImportRowResult.warnings[number]`

Expand All @@ -405,6 +406,7 @@ Type: `{ sourceField: string; targetField: string; targetLabel?: string; transfo
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |

### Nested Shape: `ImportRowResult.droppedFields[number]`

Expand Down
11 changes: 11 additions & 0 deletions content/docs/references/api/protocol.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ A write-path strip event: caller-supplied fields legally dropped from the payloa
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |


---
Expand Down Expand Up @@ -632,6 +633,7 @@ A write-path strip event: caller-supplied fields legally dropped from the payloa
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |


---
Expand Down Expand Up @@ -3384,6 +3386,7 @@ A write-path strip event: caller-supplied fields legally dropped from the payloa
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |


---
Expand Down Expand Up @@ -3522,6 +3525,14 @@ A write-path strip event: caller-supplied fields legally dropped from the payloa
| **message** | `string` | ✅ | Human-readable message — a validation rule's author-written text where one exists. |
| **rule** | `string` | optional | The `name` of the ADVISORY validation rule this finding reports. Present only on an advisory rule hit. |
| **severity** | `Enum<'warning' \| 'info'>` | optional | The advisory rule's declared severity, `warning` or `info`. Present only on an advisory rule hit. |
| **scope** | `{ field: string; value: string }[]` | optional | The row columns a scoped lookup cell was resolved within, each with the cell the row gave it, in the order the refusal names them. Present only on an import cell error whose `code` is `reference_out_of_scope`: the value matches a record, but none within these columns, so a client can name the scoping column and its cell in its own text instead of parsing `message`. |

### Nested Shape: `ValidateDataIssue.scope[number]`

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **field** | `string` | ✅ | API name of the row column that scoped the lookup. |
| **value** | `string` | ✅ | The cell the row gave that column, as the importer typed it. |


---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ hand-patch a number here** — fix the code or the verdict and regenerate.

| Dir | Sites |
|---|---|
| `api/` | 537 |
| `api/` | 538 |
49 changes: 39 additions & 10 deletions packages/core/src/utils/import-coerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export interface RefMatch {
id?: string;
ambiguous?: boolean;
matchedField?: string;
/**
* [#36794] The SCOPED read was refused: its scope names a target field the
* caller cannot read. The miss then says nothing about where the value lives,
* so the refusal stays `reference_not_found` and the value is never reported
* as a record under another parent.
*/
scopeDenied?: boolean;
}

/**
Expand Down Expand Up @@ -145,12 +152,13 @@ function resolveScoped(
}

/**
* [#36159] A row column a lookup cell was scoped by: the column's label and its
* cell as the importer typed it. Carried beside the scope (whose keys are the
* TARGET's fields and whose values are coerced ids) so a refusal can name what
* the importer wrote.
* [#36159] A row column a lookup cell was scoped by: the column's API name, its
* label and its cell as the importer typed it. Carried beside the scope (whose
* keys are the TARGET's fields and whose values are coerced ids) so a refusal
* can name what the importer wrote.
*/
interface ScopedBy {
field: string;
label: string;
value: string;
}
Expand All @@ -159,10 +167,17 @@ interface ScopedBy {
* [#36159] The refusal for a reference cell that matched nothing. A scoped
* miss is asked once more WITHOUT the scope, through the same resolver: when
* the value matches a record there (one or several), the row's own scope is
* why nothing matched, and the sentence names the scoping columns and the
* why nothing matched. [#36794] That refusal has its own reason,
* `reference_out_of_scope`: its sentence names the scoping columns and the
* values the row gave them, e.g. `Contract: no record matches "CTR-0006"
* (Account = "Acme")`. A value that matches nothing at all keeps the plain
* sentence. The reason stays `reference_not_found`.
* (Account = "Acme")`, and the error carries the same columns as `scope`, so a
* client keyed on the code (the Import Wizard) names them in its own text
* without parsing the sentence. A value that matches nothing at all keeps
* `reference_not_found` and the plain sentence.
*
* A scoped read the field-predicate guard refused (`RefMatch.scopeDenied`: the
* scope names a target field the caller cannot read) is never probed: its miss
* says nothing about where the value lives, so it stays `reference_not_found`.
*
* The probe is the caller's own read: the resolver runs every lookup as the
* importing principal, so it can find only a record that principal can already
Expand All @@ -177,13 +192,20 @@ async function referenceNotFound(
ctx: CoerceContext,
scope: Readonly<Record<string, unknown>> | undefined,
scopedBy: readonly ScopedBy[] | undefined,
scopedMatch: RefMatch,
): Promise<{ error: FieldCoerceError }> {
const refusal = coerceError(meta, field, 'reference_not_found', 'import_reference_not_found', value, ctx);
if (!scope || !scopedBy || scopedBy.length === 0 || !ctx.resolveRef || !meta.reference) return refusal;
// A scope the caller may not filter by was never asked: nothing is known about
// where the value lives, so it is not "under another parent".
if (scopedMatch.scopeDenied) return refusal;
const unscoped = normalizeRefMatch(await ctx.resolveRef(meta.reference, value, meta));
if (unscoped.id === undefined && !unscoped.ambiguous) return refusal;
const qualifier = scopedBy.map((s) => `${s.label} = "${s.value}"`).join(', ');
return { error: { ...refusal.error, message: `${refusal.error.message} (${qualifier})` } };
const outOfScope = coerceError(
meta, field, 'reference_out_of_scope', 'import_reference_out_of_scope', value, ctx, { scope: qualifier },
);
return { error: { ...outOfScope.error, scope: scopedBy.map((s) => ({ field: s.field, value: s.value })) } };
}

/** Normalise a resolver result (legacy string or structured) to a RefMatch. */
Expand Down Expand Up @@ -244,6 +266,12 @@ export interface FieldCoerceError {
*/
code: FieldErrorCode;
message: string;
/**
* [#36794] The row columns a `reference_out_of_scope` cell was resolved
* within, each with the cell the row gave it (`ValidateDataIssueSchema.scope`).
* Present on that code only.
*/
scope?: Array<{ field: string; value: string }>;
}

/**
Expand Down Expand Up @@ -906,7 +934,7 @@ async function coerceCell(
if (m.ambiguous) {
return coerceError(meta, field, 'reference_ambiguous', 'import_reference_ambiguous', token, ctx);
}
if (m.id === undefined) return referenceNotFound(meta, field, token, ctx, scope, scopedBy);
if (m.id === undefined) return referenceNotFound(meta, field, token, ctx, scope, scopedBy, m);
out.push(m.id);
}
return { value: out };
Expand All @@ -920,7 +948,7 @@ async function coerceCell(
if (match.ambiguous) {
return coerceError(meta, field, 'reference_ambiguous', 'import_reference_ambiguous', display, ctx);
}
if (match.id === undefined) return referenceNotFound(meta, field, display, ctx, scope, scopedBy);
if (match.id === undefined) return referenceNotFound(meta, field, display, ctx, scope, scopedBy, match);
return { value: match.id };
}

Expand Down Expand Up @@ -1086,6 +1114,7 @@ export async function coerceRow(
(scope ??= {})[s.param] = v;
// [#36159] What the importer wrote in the scoping column, by its label.
scopedBy.push({
field: s.field,
label: metaMap.get(s.field)?.label?.trim() || s.field,
value: String(sourceRow[s.field]).trim(),
});
Expand Down
Loading
Loading