Skip to content
Merged
18 changes: 18 additions & 0 deletions .changeset/37909-rule-message-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/objectql': minor
'@objectstack/spec': minor
'@objectstack/lint': minor
---

feat(objectql): a validation rule's refusal fills its message's `{{ }}` holes with the judged record's values

Clause-②: yes (widening)
Metadata-protocol: semantic

A validation rule's `message`, and the translation of it a refusal shows, is now filled when a write is refused: `{{ record.FIELD }}` shows the merged record the rule judged and `{{ previous.FIELD }}` the stored row before an update, through the template formatters (`currency`, `number`, `percent`, `date`, `datetime`, `upper`, `lower`, `trim`, `truncate`, `default`, `json`). `Total {{ record.total_price | currency }} is below what was received ({{ record.received_amount | currency }})` now reads `Total $1,200.00 is below what was received ($1,500.00)`. Before, the sentence was returned as written, holes and all. A message with no `{{` is returned exactly as before.

- **What a hole may show.** A hole shows a field only when the person the refusal is worded for may read it unmasked, as the security service answers for that person. On an ordinary write that is the person who wrote. On an elevated write made for a person (a `runAs: 'system'` flow they started, whose refusal reaches them as the run's `innerRefusal`; an action body; an approval decision), the security service is asked for that person, with their own permission sets, never for the system; if their grants cannot be read, every hole renders empty. Only an elevated write that names no person (a schedule, a platform sweep) is answered as the system, and its sentence reaches the server log alone. A field the reader cannot read, a field masked for them, a `secret` / `password` or `internal` field, a field the object does not declare, a read through a field (`{{ record.account.name }}`) and a hole over any other root render empty. If no security service is registered, or the one registered cannot answer, answers nothing or fails, every hole renders empty and the server log says why, once per write. The rule's verdict never depends on the sentence, and no value is added to the error's other fields.
- **Formatting.** The formatters render in the refusal's language. `| currency` with no argument formats in the organization's default currency (Setup → Localization, `localization.currency`); `| currency:EUR` names one. If no default currency reaches the write (the organization has none set, or the write carried none), a `| currency` hole with no argument renders empty, the rest of the sentence renders, and a warning in the server log names the rule and the setting to fill; the amount is never formatted in a currency nobody chose. `{{ previous.FIELD }}` renders empty on an insert.
- **Translations first.** The translation for the refusal's language is filled first. A translation that does not render (one stored before `objectstack validate` judged it) is replaced by the authored message, filled, and a warning in the server log names the translation. An authored message that does not render is shown as written, as before, and the log names the rule.

What changes for a deployed app: a rule message that already carries a `{{ record.FIELD }}` hole shows the value from this release. A message stored before `objectstack validate` judged its holes may carry one no reader can ever see filled: a hole over another root (`{{ from }}`), a bare root (`{{ record }}`), a read through a field, or a field the object does not declare. Such a hole now renders empty where it used to be shown as written, and the server log names the rule and the hole on each write that renders it. Run `objectstack validate` and rewrite each hole it names.
2 changes: 1 addition & 1 deletion .changeset/37909-rule-message-template-holes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
Expand All @@ -21,7 +21,7 @@

Filling the holes at run time is the next change on this card; until it lands, a hole that passes the doors is returned as written.

- **Exports.** `@objectstack/spec/data` gains `VALIDATION_MESSAGE_TEMPLATE_ROOTS` (`['record', 'previous']`). The `message` describe of the six validation-rule variants and of the `_validations` translation entry now state the template.
- **Exports.** `@objectstack/spec/data` gains `VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS` (`['record', 'previous']`). The `message` describe of the six validation-rule variants and of the `_validations` translation entry now state the template.
- **The ADR-0087 ledger** gains the semantic entry `validation-rule-message-template-holes` under step 19.

FROM → TO:
Expand Down
2 changes: 2 additions & 0 deletions content/docs/permissions/system-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ bugs — it is the protection or the side effect that silently does not happen.
| 29d | The recycle bin's tenant wall does not apply: `restore`, `purge` and `empty` read and remove **every organization's** entries | objectql | Get: the bin's own doors reach any entry, as the system reaches every wall. Every caller that is not system is held to the Layer-0 verdict: one the wall denies (a signed-in caller with no active organization under a walled posture) sees no entry, so `restore` answers `null`, `purge` `false` and `empty` `0`, and the removal is scoped by the verdict the security layer recorded on the dispatch when it recorded one (#30237) | `recycleBinWallDenies` |
| 29e | A restore of an entry someone else deleted asks **no recycle-bin grant** | objectql | Get: the system restores any entry. Every caller that is not system restores only the entries whose `deleted_by` is its own `userId` unless its effective permissions grant `manageRecycleBin` on the entry's object; otherwise `403 PERMISSION_DENIED`, and the same refusal when no permission resolver is registered or it throws (#30237, ADR-0148 D5) | `assertCallerMayRestoreEntry` |
| 29f | Listing the recycle bin of an object asks **no recycle-bin grant** | objectql | Get: the system lists every entry of any object the bin takes. Every caller that is not system lists an object's entries only when its effective permissions grant `manageRecycleBin` on the object; otherwise `403 PERMISSION_DENIED`, and the same refusal when no permission resolver is registered or it throws (#30237, ADR-0148 D5) | `assertCallerManagesRecycleBin` |
| 29g | A validation rule message's `{{ }}` holes show **every field the system may read**, only when the write names no person | objectql | Get: a user-less system write (a schedule, a platform sweep) fills a refused write's message holes with the security service's system answer, every field less credential and `internal` ones; that sentence reaches the server log alone. An elevated write that names a person (`userId`: a `runAs: 'system'` flow they started, whose refusal reaches them as `innerRefusal`; an action body; the approvals mirror) is not read as the system: both field projections are asked for that person, with the permission sets the context carries or their own grants (`resolveUserAuthzGrants`), and every hole renders empty when those grants cannot be read (#37909) | `readerContext` |
| 30 | **Bulk data event `organizationId` OMITTED** — the batch is published "not asserted" | plugin-security | Get: nothing — the `data.records.*` event still publishes. Lose: the per-organization attribution: this exit is taken before the security middleware composes any tenant wall, so it records no Layer 0 verdict on the operation (`OperationContext.tenantLayer0Verdict`, #15813), and the engine's bulk producer — which reads that recorded verdict and nothing else — omits the key rather than filling it from the caller's `tenantId`; a tenant-scoped consumer then does not deliver the event inside an organization wall (#15225) | `registerAuthzMiddleware` |
| 30b | **The import doors admit a control opt-out** — `runAutomations: false` and `treatAsHistorical: true` on `POST /data/:object/import` and `/import/jobs` | rest | Get: an import whose rows are written with `skipAutomations` (no metadata-bound hook, no record-change trigger: an app's approvals and record freezes among them) or with `skipStateMachine` and `preserveAudit`. Every other caller, every signed-in user and administrator included, is refused `403 PERMISSION_DENIED` before the body is parsed, so nothing is written and no job is created. Inbound HTTP never carries the flag, so this is an in-process caller's door. Note: the flag still suppresses none of those controls by itself (see below); it only admits the opt-outs that do | `importControlOptOutRefusal` |
| 30c | **The import undo keeps the control skips** — `POST /data/import/jobs/:jobId/undo` | rest | Get: an undo whose deletes and restores are written with `skipAutomations` and `skipStateMachine`, so no metadata-bound hook, record-change trigger or `state_machine` rule refuses a reversal. Every other caller, every signed-in user and administrator included, undoes under those controls: a reversal one of them refuses is reported per row in the undo answer's `failures` and kept in the job's undo log, and the job is stamped reverted only once every row is. A historical job's `preserveAudit` belongs to the job, not the caller, and rides every undo. Inbound HTTP never carries the flag, so this is an in-process caller's door | `importUndoWriteContext` |
Expand Down Expand Up @@ -561,6 +562,7 @@ and CI refuses one that differs from what the tree produces.
| `packages/objectql/src/engine-referential-integrity.ts#masterDetailHeadersCallerReads` | 1 | 29c |
| `packages/objectql/src/engine-referential-integrity.ts#referencesCheckedBeforeRules` | 1 | 23 |
| `packages/objectql/src/engine-referential-integrity.ts#storedReferencesCallerReads` | 1 | 23 |
| `packages/objectql/src/engine-rule-message-rendering.ts#readerContext` | 1 | 29g |
| `packages/objectql/src/engine-scoped-context.ts#appAuthoredHookApi` | 1 | 1b |
| `packages/objectql/src/engine-secret-internal-fields.ts#encryptSecretFields` | 1 | 23e |
| `packages/objectql/src/engine-secret-internal-fields.ts#stripSearchCompanionFromRead` | 1 | 27 |
Expand Down
8 changes: 4 additions & 4 deletions packages/lint/src/validate-rule-message-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* `objects.OBJECT._validations.RULE.message` translations — as the ADR-0032 §3
* `{{ }}` template it is, over the judged record.
*
* The roots are the spec's, `VALIDATION_MESSAGE_TEMPLATE_ROOTS`
* The roots are the spec's, `VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS`
* (`@objectstack/spec/data`): `record`, the merged record the rule judged, and
* `previous`, the stored row before an update. Three steps, the action copy
* pass's two (`validate-action-copy-templates.ts`) and one that is the rule
Expand Down Expand Up @@ -35,7 +35,7 @@

import { validateExpression } from '@objectstack/formula';
import { closedScopeTemplateRefusal } from '@objectstack/spec/automation';
import { VALIDATION_MESSAGE_TEMPLATE_ROOTS } from '@objectstack/spec/data';
import { VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS } from '@objectstack/spec/data';
import { nearestName } from './object-graph.js';
import type { ExprIssue } from './validate-expressions.js';

Expand All @@ -55,7 +55,7 @@ const HOLE = /\{\{([^}]*)\}\}/g;
/** A hole's path — the template engine's `PATH_ONLY_RE`; a hole that fails it is the compile step's to name. */
const HOLE_PATH = /^[\w$.[\]]+$/;

const ROOTS: ReadonlySet<string> = new Set(VALIDATION_MESSAGE_TEMPLATE_ROOTS);
const ROOTS: ReadonlySet<string> = new Set(VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS);

/** A path's segments as the engine resolves them (`[i]` read as `.i`). */
function segmentsOf(path: string): string[] {
Expand Down Expand Up @@ -129,7 +129,7 @@ export function ruleMessageTemplateIssues(text: unknown, site: RuleMessageSite):
const refuse = (message: string, source: string): void => {
issues.push({ where: site.where, message, source, severity: 'error', ...(site.path ? { path: site.path } : {}) });
};
const refusal = closedScopeTemplateRefusal(text, { slot: site.slot, roots: VALIDATION_MESSAGE_TEMPLATE_ROOTS });
const refusal = closedScopeTemplateRefusal(text, { slot: site.slot, roots: VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS });
if (refusal !== undefined) {
refuse(refusal, text);
return issues;
Expand Down
4 changes: 4 additions & 0 deletions packages/objectql/src/engine-insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import type {
} from './engine-top-level.js';
import { ObjectQLStatics as ObjectQL } from './engine.js';
import type { InsertManyRowOutcome, ObjectQL as ObjectQLClass } from './engine.js';
import { attachRuleMessageRendering } from './engine-rule-message-rendering.js';

/**
* The `ObjectQL` members the bodies of {@link InsertFacet} read — every one of
Expand Down Expand Up @@ -1190,6 +1191,9 @@ export class InsertFacet {
// Locale + translation hooks for the rejection messages (#3957) —
// resolved once for the batch, identical for every row.
const msgCtx = this.validationMessageContext(object, opCtx.context);
// [#37909] What a rule message's `{{ }}` holes may show on this write,
// and the currency they format in — computed once, beside the context.
await attachRuleMessageRendering(msgCtx, this.facade, this.logger, object, schemaForValidation, opCtx.context);
// [#3407 / #5126] This is the strip site both standing notes on
// `insert()` pointed at, so both members of `WriteObservabilityOptions`
// discharge here — the same one-per-call choice `update` offers, and by
Expand Down
Loading
Loading