Repository navigation
Commit 49c09ed
feat(objectql): a validation rule's refusal fills its message's {{ }} holes with the judged record's values (#38736)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9SNzk6FfxPPrxLwCB5BftuN)_
Part of #37909 — PR 2 of the ruling's sequencing (`domain:server`:
render in `authoredRuleMessage`, currency and readable-field handover,
the empty-on-unreadable pins). PR 1 (#38687, spec + lint, merged as
9cbb41b) is the doors; PR 3 (skills and docs) stays on the card.
Clause-②: yes (widening)
Metadata-protocol: semantic
Skills-docs: follow-up #37909 (PR 3 of the ruling's sequencing: skills
and docs)
## 维护者速读
- 用户能看到什么变化:校验规则拦下保存时,提示语里的 `{{ record.字段 }}` 会填上被拦记录的真实值(比如「总价
¥1,200.00 低于已收
¥1,500.00」),金额按组织默认币种、按用户语言格式化;看不到或被遮罩的字段一律留空。系统身份替某人执行的写(如该人触发的系统流程)按该人的权限判定,没有安全服务时全部留空并记日志。
- 为什么做:维护者已裁(P3);这是第二步,让第一步定下的写法真正生效,HotCRM 的改单拦截才能说出差多少钱。
- 出错会怎样:最坏是某个洞该显示的值显示成空;规则拦不拦不受影响,值也不会写进错误的其它字段;撤回这次合并即回到「原样显示」。
- 要你做什么:组织没设默认币种时,提示语里不带币种的金额占位怎么处理,正等你在决策卡上定;本 PR 暂按席位推荐的
A(该占位留空、日志提醒设置默认币种)实现,你选别的方案只改一个函数,改完再请你审批。
## What changes
Ruling P3 (card comment 7239726323), parameters 5–8, and the forward
flags of PR 1's contract review (7269393846).
- **`@objectstack/objectql`** — `authoredRuleMessage`
(`validation/rule-validator.ts`) now fills the sentence it picks:
- translation first (parameter 7): the translated template is picked and
filled; if it does not render, the authored template is filled instead
and the write's logger names the translation
(`objects.O._validations.R.message`, the locale); an authored message
that does not render (one stored before the doors) is shown as written
and the logger names the rule;
- the fill is `renderRuleMessage` (new
`validation/rule-message-render.ts`) through the formula
`templateEngine` — no second engine — over the merged record (`record`)
and the stored row (`previous`, `null` on an insert), in the refusal's
negotiated locale, with the tenant default currency and the reference
timezone. Every rule kind that emits an authored message fills it:
`script` / `cross_field`, `format`, `json_schema`, `state_machine`, and
a `conditional` branch. A `script` rule's holes read the record as
judged before related rows are hydrated into reference fields, so `{{
record.account }}` is the stored id, never the related row;
- what a hole may show (parameter 6) is
`ValidationMessageContext.rendering`, computed once per write by the new
`engine-rule-message-rendering.ts` (`attachRuleMessageRendering`),
called beside `validationMessageContext` on the three paths that build
it: `engine-insert.ts`, `engine-update.ts` (the by-id and bulk legs
share it) and `engine-validate.ts` (the dry run). It is computed only
when the object's rule messages or their translations carry a `{{`, so
no other write asks the security service anything new.
- **The showable fields** — `getReadableFields` ∩ `getQueryableFields`
of the registered security service (the contract: "the difference
between the two is exactly the fields this caller sees masked"); with no
`getQueryableFields`, or `undefined` from it, every `maskingRule` field
counts as masked (the contract's prescribed fallback). Every field the
engine masks or omits on a read (`secret`, generic `password`,
`internal`) is never showable. A hole is empty twice over: blanked
before the render, and absent from the scope the engine evaluates.
- **Whose read (contract review M1, fix (a))** — both projections are
asked under the write's own context, except on an elevated write
(`isSystem`) that names a person (`userId`): a `runAs: 'system'` flow,
an action body (`{ ...caller, isSystem: true }`), the approvals mirror.
There they are asked for that person: the same context with `isSystem:
false`, carrying the positions and permission sets it carries, or, where
it carries none (the flow's and the approvals mirror's context), the
person's own grants resolved through `resolveUserAuthzGrants`
(`@objectstack/core`), as a `runAs: 'user'` hook fired by the same write
seeds them. Grants that cannot be read: every hole empty. A user-less
system write (a schedule, a platform sweep) keeps the system's answer;
its sentence reaches the server log alone.
- **Fail closed, and say why (S1)** — no security service registered
(the kernel throws on an unregistered name; a bare engine's slot is
empty), a service that cannot be asked (no function in the slot, no
`getReadableFields`), one that answers `undefined` or throws, and
unreadable grants of the person an elevated write was made for: nothing
is showable, every hole renders empty, and the write's logger gets one
line naming the cause (`RuleMessageRendering.unanswered`), once per
write.
- **What the log is told (S2)** — a hole no reader could ever see filled
(another root, a bare root, a read through a field, a field the object
does not declare: a message stored before the doors) is named with its
rule and the hole. A hole empty only for this reader (unreadable,
masked) is never logged. Each distinct line is written once per write,
so a bulk refusal does not repeat it (N1).
- **The currency** (parameter 5) — the write's
`ExecutionContext.currency` (the organization's `localization.currency`,
which the request context already resolves), when it is a three-capital
ISO 4217 code. **Pending the maintainer's ruling (parameter 8 fork, put
to the maintainer as a decision card by the seat):** with no tenant
default, a `currency` hole with no argument passes the doors but cannot
render. Provisionally (option A, the seat's recommendation) that hole
renders empty and the write's logger names the rule, the hole and the
setting, saying the organization has none set or the write carried none
(N1) (Setup → Localization, `localization.currency`); the rest of the
sentence renders. It is isolated in one function, `tenantCurrencyHole`
(`validation/rule-message-render.ts`), and pinned by one test; a
different ruling changes those two and the changeset's sentence, nothing
else. ⛔ The amount is never formatted in a currency nobody chose, nor
shown unformatted.
- **`@objectstack/spec` + `@objectstack/lint`** — the roots export is
renamed before its first release, `VALIDATION_MESSAGE_TEMPLATE_ROOTS` →
`VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS` (contract review N2:
`@objectstack/spec/system` already has a `VALIDATION_MESSAGE_*`
field-constraint family that binds no roots), with its lint importer,
the translation describe that names it, and the regenerated
`api-surface/data.json`, `export-origins/data.json` and
`docs/references/system/translation.md`. PR 1's changeset is edited to
name the final export (both ship in one release).
### Decisions this PR takes
- **N5, `previous.FIELD` on an insert-only rule:** the doors keep
accepting it and it renders empty, as on every insert (parameter 1
allows it; a rule with `events: ['insert', 'update']` reads it on one
leg only). Pinned.
- **A hole over a root other than `record` / `previous`, a bare root, a
read through a field, an undeclared field:** the doors refuse each (PR
1); a rule stored before them renders each empty, never the built-in
sentence's own parameters.
### Deliberate correction of a pending release note
`.changeset/37909-rule-message-template-holes.md` (PR 1's, pending, not
yet released) is edited in one sentence: its **Exports** bullet named
`VALIDATION_MESSAGE_TEMPLATE_ROOTS`, which this PR renames, so it now
reads "`@objectstack/spec/data` gains
`VALIDATION_RULE_MESSAGE_TEMPLATE_ROOTS` (`['record', 'previous']`)".
Restoring it would publish a release note naming an export the release
does not carry. `check-empty-changeset` stays red on that file by
design; the dispatching seat confirms the correction by its own record.
### Outside the claimed file surface
-
`packages/services/service-automation/src/flow-rule-message-reader.integration.test.ts`
(new, test only): contract review M1 asks for the flow path pinned end
to end (`runAs: 'system'` → `innerRefusal`), which only a test composing
the automation engine reaches. No service-automation source changes.
- `content/docs/permissions/system-context.mdx` (one register row, 29g,
and the regenerated register): the new `readerContext` reads `isSystem`
to de-elevate, and `pnpm check:system-context-census` requires every
such read to be owned by a row naming its symbol.
### Tests forced to change
-
`packages/objectql/src/validation/rule-validator.state-machine-option-labels.test.ts`
— "leaves an author-written message exactly as written — no holes are
filled in it" pinned the contract this card changes (ruling P3). Its
intent stays: the built-in sentence's `{{from}}` / `{{to}}` never fill
an authored message. Those are holes over another root, so they now
render empty; the code and `constraint` assertions are unchanged.
## Tests
- `packages/objectql/src/validation/rule-message-render.test.ts` (19,
through `evaluateValidationRules`): formatters and locale, the tenant
currency, `previous` on update and on insert, plain text and a single
brace as written; the field floor (outside `showable`, never logged; no
rendering at all; read-through / bare root / other root / undeclared,
each logged with its rule; the stored id of a hydrated reference; no
value in the machine fields); the scope floor on its own, a hole the
blanking builds (N5); an unanswered field security, its cause logged;
every rule kind (`conditional` branch, `format`, `state_machine`);
translation first, its fallback with the warning, a pre-door authored
message as written; and the provisional no-currency arm (that hole
empty, the rest rendered, the rule and the setting named).
- `packages/objectql/src/engine-rule-message-rendering.test.ts` (15, a
real engine with the security service as a double in its source slot):
readable and unmasked shows, masked / `password` / `internal` empty;
unreadable empty; no `getQueryableFields` → `maskingRule` empty; a
lifted mask shows; `undefined`, a throw from either projection, a
throwing source, a service without `getReadableFields` → every hole
empty, the write still refused, one log line naming each cause; no
service registered → every hole empty and the line; a reader-only blank
never logged; stored structural holes logged once per write across a
bulk insert. Elevated writes (M1): `{ ...caller, isSystem: true }` asked
for the caller with their sets, never as the system; `{ isSystem: true,
userId }` with no sets on an engine with no permission store → grants
unreadable, every hole empty, the cause logged; CONTROL a user-less
system write keeps the system answer; CONTROL the same person's own
write reads the same. `| currency` in the context's currency and locale;
`previous` on a real update; the dry run fills the same sentence.
-
`packages/services/service-automation/src/flow-rule-message-reader.integration.test.ts`
(4, ObjectKernel + ObjectQLPlugin + driver-sql on better-sqlite3 +
identity objects + the real SecurityPlugin + the real AutomationEngine):
a member profile withholds `margin`; a `runAs: 'system'` flow started by
that member, refused by the rule, carries `Amount -1 is below the margin
on Acme.` in `innerRefusal.message` (run against the previous head's
build it carried `… the margin 30 …`); `runAs: 'user'` and the member's
own request read the same; CONTROL a user-less system write shows `30`.
- Ablations: dropping the masked-field judgement reds 2 engine tests;
building the scope from the whole row instead of the showable values
reds the N5 pin (`SSN 123-45-6789 end`).
## Acceptance notes
- The render ships in the same release as PR 1 (#38687, merged): PR 1
alone teaches holes that render as written. This branch was restarted
from `main` after PR 1 merged and its four commits rebased onto it.
- The rendered sentence is written to the server log by the engine's
existing `Insert operation failed` warning, as the authored message was;
it carries only fields the person the refusal is worded for may read.
- Out of scope, from contract review 7273883345: N2 (an `Intl`
`RangeError` for a locale tag or time zone `Intl` does not know shows
the template as written; whether such a tag reaches a write's context is
not established), N3 (`objectstack validate`'s seed-row refusal,
`packages/lint/src/seed-row-refusals.ts`, now prints blank holes; a
follow-up hands it a `rendering` over the seed row's declared fields, or
prints the template), N7 (the projections are asked on every write to an
object whose messages carry a hole, refused or not; asking at the first
refusal needs an async render). Each goes to PR 3 or a follow-up card,
as the seat files it.
## Verification
- `pnpm --filter @objectstack/objectql exec vitest run --maxWorkers=2
src/validation/rule-message-render.test.ts
src/engine-rule-message-rendering.test.ts`: 34 passed; `pnpm --filter
@objectstack/service-automation exec vitest run
src/flow-rule-message-reader.integration.test.ts`: 4 passed.
- Two-minute check: in `examples/app-showcase`, give a validation rule
the message `Total {{ record.total_price | currency }} is too low.`, set
Setup → Localization → Default currency, and save a record the rule
refuses: the refusal names the amount.
<details><summary>Local gates at head 331b47d</summary>
- `node scripts/pm/dispatch-gates.mjs --commands` (derived after the
last commit): 125 commands, each run with stdin closed through the
verify lock; 124 `exit 0`, and `node scripts/check-empty-changeset.mjs
--base origin/main :: exit 1`, the deliberate correction above. `--ran`
reconciliation: 125 derived, 125 run, 0 NOT-MEASURED, 0 UNRUN.
- The run at 8f25796 (104 derived) had one other red, `pnpm
check:system-context-census` (the `readerContext` read had no owning
row); 331b47d adds row 29g and the census passes in the 125.
- `pnpm --filter @objectstack/objectql exec vitest run --maxWorkers=2`
on the tree committed as 6a49759 + 8f25796: 643 files, 14602
tests, all passed (lock VERDICT command-exit 0).
- Dogfood battery (`pnpm --filter @objectstack/dogfood test`) at
331b47d: lock VERDICT command-exit 0; Test Files 495 passed, 3
skipped; Tests 4187 passed, 68 skipped; no failure.
- `pnpm build --concurrency=2` (whole workspace) on the rebased head
9b716a3: lock VERDICT command-exit 0; `@objectstack/objectql` rebuilt
at the fix (exit 0) before the service-automation test ran against it.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmQ6vV3YvhJWKF1gSDs
<!-- signed: repo:objectstack@os-steve · os-steve ·
session_01ADHkmQ6vV3YvhJWKF1gSDs -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 111ad63 commit 49c09ed
20 files changed
Lines changed: 1342 additions & 41 deletions
File tree
- .changeset
- content/docs/permissions
- packages
- lint/src
- objectql/src
- validation
- services/service-automation/src
- spec
- api-surface
- docs/references/system
- export-origins
- src
- data
- system
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
| |||
561 | 562 | | |
562 | 563 | | |
563 | 564 | | |
| 565 | + | |
564 | 566 | | |
565 | 567 | | |
566 | 568 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| |||
1190 | 1191 | | |
1191 | 1192 | | |
1192 | 1193 | | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
1193 | 1197 | | |
1194 | 1198 | | |
1195 | 1199 | | |
| |||
0 commit comments