Skip to content

Commit 6296909

Browse files
fix(spec): a deployment's validation override prints a written value as written (#35325)
<!-- claude-projects-attribution: {"github_login":"os-bill"} --> _Requested by **Bill** · [project thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wgRuogP8Tp8XyFXLc92Bxvo6)_ repo:objectstack#5 · os-bill · session_01Utp6CZiaBg6rk7dpy1s9Me Fixes #34509 Clause-②: no Metadata-protocol: none ## 维护者速读 - **改了什么:** `renderValidationMessage` 向部署方的 `validation.field.*` 覆盖文案只要模板(不再把参数交给 i18n 服务),由渲染器自己填一次占位符;两个 `translate` 选项的注释写明这一点。 - **为什么改:** i18n 服务拿到参数会自己填一遍,渲染器再填一遍。用户写入的值里如果带 `{{label}}` 之类,第二遍会把它换成字段名,报错句子就引错了用户写的内容。 - **风险与回滚:** 只改展示文字,错误码、状态码、返回的 `value` 字段都不变;只用 `{{name}}` 占位符的覆盖文案读出来与以前一样。函数签名不变。回滚即 revert 本 PR。 - **先看:** `packages/spec/src/system/validation-message.ts` 中 `opts.translate(key, locale)` 那一行(原来带 `params`);`packages/spec/src/system/validation-message.override-once.test.ts`。 - **两分钟验证:** `pnpm --filter @objectstack/spec exec vitest run --maxWorkers=2 src/system/validation-message.override-once.test.ts`,预期 2 个用例通过。 - **需要你做的:** 无,席位落地(合入前由区线程安排 L2 契约复核)。 ## What changes - `renderValidationMessage` (`@objectstack/spec/system`) asks the deployment override as `opts.translate(key, locale)`, with no params, and interpolates the answer once. Before, it passed `params` to the translator (which, like `FileI18nAdapter.t`, fills `{{name}}` itself) and interpolated the answer again, so a written value containing a placeholder was expanded on the second pass. This is the shape `renderOperationMessage` already has. - Fold-in (accepted nit N2 of contract record 6932332429): the `translate` option docblocks on `RenderValidationMessageOptions` and `RenderOperationMessageOptions` say the lookup is asked for the template with no params. The type `ValidationMessageTranslator` and both option signatures are unchanged (`check:api-surface`: unchanged). - Changeset: `@objectstack/spec` patch. ## Tests New file `packages/spec/src/system/validation-message.override-once.test.ts`: an interpolating translator (fills `{{name}}` from params, as `FileI18nAdapter.t` does) overrides `invalid_option_value`, which echoes `{{value}}`; the written value is `{{label}} plan`. The override path must print it as written. CONTROL leg: the built-in path (no override), which was already single-pass. <details><summary>Red leg (test committed before the fix, 128113d)</summary> ``` × prints a written value containing a placeholder as written on the override path Expected: "套餐的取值「{{label}} plan」无效,请从 basic, pro 中选择" Received: "套餐的取值「套餐 plan」无效,请从 basic, pro 中选择" Tests 1 failed | 1 passed (2) os-verify-lock: VERDICT command-exit 1 ``` </details> <details><summary>Green leg (c0ed09b)</summary> ``` validation-message.override-once.test.ts + validation-message.test.ts + operation-message.test.ts Test Files 3 passed (3) Tests 99 passed (99) os-verify-lock: VERDICT command-exit 0 ``` </details> ## Acceptance notes - A1 (callers that pass a `translate`): only `objectql` `record-validator` (through `engine-validation-messages.ts`) and `core` `import-coerce` (through `import-runner`) hand `renderValidationMessage` a translator; `rest` `form-routes`, `service-settings` and `service-messaging` `actor-reference` pass none. Every translator found is a wrapper over `II18nService.t` (`FileI18nAdapter.t`, the core memory fallback), which looks up by key and locale and uses params only to interpolate. None needs params at lookup time. - A2: the published `.d.ts` of `RenderValidationMessageOptions.translate` is still `translate?: ValidationMessageTranslator`; only its docblock changed. Clause-② no. - The test is a new file, as the claim's file surface names. The twin pin for `renderOperationMessage` sits in `operation-message.test.ts`; a reviewer who prefers the pin as a top-level `describe` in `validation-message.test.ts` can ask for the move. - The `RenderOperationMessageOptions` docblock is in `operation-message.ts`, not `validation-message.ts` as the claim's file surface reads; the claim names that docblock, so the one comment edit there is in scope. ## Verification All readings are at head `c0ed09b721`. - `pnpm --filter @objectstack/spec build`: `VERDICT command-exit 0`. The dependency closure of `@objectstack/spec` is empty. - `pnpm --filter @objectstack/spec test`: Test Files 856 passed (856), Tests 27366 passed, 1 todo; `VERDICT command-exit 0`. - `pnpm --filter @objectstack/spec typecheck`: `VERDICT command-exit 0`. - Gate families derived by `node scripts/pm/dispatch-gates.mjs --commands` (83): 80 exit 0, 3 exit 3 (PREREQUISITE NOT MET, they read workspace `dist/` not built here): `check:doc-formula-expressions` (`@objectstack/formula`), `check:dual-build-cjs-loads`, `check:lean-entry-closure` (`@objectstack/objectql`). NOT MEASURED locally; CI's `Lint & Repo Gates` runs them. `--ran` reconciliation: 83 derived, 80 run, 3 NOT-MEASURED, 0 UNRUN. - `check:api-surface`: public API surface and factory signatures unchanged. `check:docs`: 249 generated files in sync. `check:nul-bytes`: OK. - `pnpm check:dev-prereqs` (local only): exit 1, "The workspace is not built" (35 of 108 packages, all outside this diff). This is a reading of this machine, not of the diff. - `node scripts/pm/overlap-check.mjs --branch=claude/issue-34509-validation-override-once`: `VERDICT clean exit 0`. - `test:repo` and the full dogfood battery are not run locally: the title carries no `!` and no `(narrowing)` arm; CI's `Test Core` runs `test:repo`. Consumer packages' tests (`objectql`, `core`) are left to CI: the public surface is byte-unchanged in signature. --- _Generated by [Claude Code](https://claude.ai/code/session_01Utp6CZiaBg6rk7dpy1s9Me)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e4bfa33 commit 6296909

4 files changed

Lines changed: 82 additions & 5 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/spec': patch
3+
---
4+
5+
fix(spec): a deployment's validation override prints a written value exactly as written
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`renderValidationMessage` (`@objectstack/spec/system`) now asks a deployment's `validation.field.<messageKey>` override for its template, without the parameters, and fills the placeholders once. Before, the `i18n` service filled them and the renderer filled the result again, so a written value that itself contained a placeholder, such as a select value `{{label}} plan` refused as `invalid_option_value`, was expanded on the second pass and the refusal showed the field label where the user's own value should be. An override's text now quotes the value exactly as written, as the built-in sentence already did. Overrides that use only `{{name}}` placeholders read the same as before.
11+
12+
The `translate` option of `RenderValidationMessageOptions` and `RenderOperationMessageOptions` now says it is asked for the template with no params. Its type is unchanged. The error code, status and the shipped `value` field of the refusal are unchanged.

‎packages/spec/src/system/operation-message.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,11 @@ export interface RenderOperationMessageInput {
538538
export interface RenderOperationMessageOptions {
539539
/** BCP-47 locale; defaults to `en`. */
540540
locale?: string;
541-
/** Deployment override hook — an `II18nService.t`-compatible lookup. */
541+
/**
542+
* Deployment override hook — an `II18nService.t`-compatible lookup. It is
543+
* asked for the override TEMPLATE with no params (`translate(key, locale)`);
544+
* the renderer fills the placeholders once.
545+
*/
542546
translate?: ValidationMessageTranslator;
543547
}
544548

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { describe, it, expect } from 'vitest';
4+
import {
5+
BUILTIN_VALIDATION_MESSAGES,
6+
renderValidationMessage,
7+
validationMessageTranslationKey,
8+
} from './validation-message';
9+
10+
/**
11+
* A deployment's `validation.field.<messageKey>` override is interpolated ONCE.
12+
* `{{value}}` is what the user wrote, so a value that itself contains a
13+
* placeholder (`{{label}}`) must be printed as written — not expanded into the
14+
* field label by a second pass.
15+
*
16+
* The translator is shaped as `FileI18nAdapter.t` answers: handed `params`, it
17+
* fills `{{name}}` itself. Asked for the template with params, its answer would
18+
* already carry the user's `{{label}}`, and the renderer's own pass would then
19+
* expand it. Asked without params, it returns the template and the renderer
20+
* fills it exactly once.
21+
*/
22+
describe('renderValidationMessage — a deployment override is interpolated once', () => {
23+
const key = validationMessageTranslationKey('invalid_option_value');
24+
const overrideTemplate = '{{label}}的取值「{{value}}」无效,请从 {{allowed}} 中选择';
25+
const interpolatingTranslate = (k: string, _locale: string, params?: Record<string, unknown>) => {
26+
if (k !== key) return k;
27+
return params && Object.keys(params).length > 0
28+
? overrideTemplate.replace(/\{\{(\w+)\}\}/g, (m, name: string) => (params[name] != null ? String(params[name]) : m))
29+
: overrideTemplate;
30+
};
31+
const written = '{{label}} plan';
32+
const input = {
33+
messageKey: 'invalid_option_value',
34+
label: '套餐',
35+
field: 'plan',
36+
params: { value: written, allowed: 'basic, pro' },
37+
};
38+
39+
it('prints a written value containing a placeholder as written on the override path', () => {
40+
expect(renderValidationMessage(input, { locale: 'zh-CN', translate: interpolatingTranslate }))
41+
.toBe(`套餐的取值「${written}」无效,请从 basic, pro 中选择`);
42+
});
43+
44+
it('CONTROL — the built-in path, which was already single-pass, prints it as written too', () => {
45+
expect(renderValidationMessage(input, { locale: 'zh-CN' })).toBe(
46+
BUILTIN_VALIDATION_MESSAGES['zh-CN'].invalid_option_value
47+
.replace('{{label}}', '套餐')
48+
.replace('{{value}}', () => written)
49+
.replace('{{allowed}}', 'basic, pro'),
50+
);
51+
});
52+
});

‎packages/spec/src/system/validation-message.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ export interface RenderValidationMessageInput {
349349
export interface RenderValidationMessageOptions {
350350
/** BCP-47 locale; defaults to `en`. */
351351
locale?: string;
352-
/** Deployment override hook — an `II18nService.t`-compatible lookup. */
352+
/**
353+
* Deployment override hook — an `II18nService.t`-compatible lookup. It is
354+
* asked for the override TEMPLATE with no params (`translate(key, locale)`);
355+
* the renderer fills the placeholders once.
356+
*/
353357
translate?: ValidationMessageTranslator;
354358
}
355359

@@ -383,14 +387,19 @@ export function renderValidationMessage(
383387
const key = validationMessageTranslationKey(input.messageKey);
384388
let override: string | undefined;
385389
try {
386-
override = opts.translate(key, locale, params);
390+
// The override TEMPLATE is asked for, without `params`, and interpolated
391+
// once below. An `II18nService.t` handed `params` interpolates them
392+
// itself, and a second pass here would then expand a placeholder that
393+
// arrived inside a VALUE (a written `{{label}}` echoed as `{{value}}`)
394+
// instead of printing it as written — the shape `renderOperationMessage`
395+
// already has.
396+
override = opts.translate(key, locale);
387397
} catch {
388398
// A misbehaving i18n service must never turn a 400 into a 500.
389399
override = undefined;
390400
}
391401
// The contract is "returns the key itself if not found" — anything else is
392-
// a real translation. Interpolate defensively: an implementation that does
393-
// not substitute (or ignores `params`) still yields a filled message.
402+
// a real translation, filled here exactly once.
394403
if (typeof override === 'string' && override.length > 0 && override !== key) {
395404
return interpolateValidationMessage(override, params);
396405
}

0 commit comments

Comments
 (0)