Skip to content
Merged
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
10 changes: 10 additions & 0 deletions .changeset/38266-spaced-override-holes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/spec': minor
---

fix(spec): a translated validation or operation message fills {{ label }} holes written with spaces, as the Console does

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

A deployment that overrides a built-in validation message (`validation.field.<key>`) or operation message (`errors.<key>`) in a translation can now write a placeholder with spaces inside the braces, `{{ label }}`, and the server fills it. Before, `interpolateValidationMessage` (used by `renderValidationMessage` and `renderOperationMessage`) filled only `{{label}}`, so the same translation showed a value in the Console and raw braces in a 400 or 409 answer, such as an import row refusal or a duplicate-record error. Whitespace (spaces or tabs) between the braces and the name is now allowed. A placeholder with no value is still left exactly as written, spaces included, and single braces (`{label}`) are still not a placeholder. The built-in messages render byte-for-byte as before, and no change is needed in your bundles: `{{label}}` works as before.
21 changes: 21 additions & 0 deletions packages/spec/src/system/operation-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ describe('operation message catalog', () => {
.toBe('不能删除:还有 1 条下级记录。');
});

it('a deployment override written with spaces inside the braces is filled, as the Console fills it', () => {
const translate = (key: string) =>
key === 'errors.duplicate_record_field' ? '{{ field }} taken' : key;
expect(renderOperationMessage({ messageKey: 'duplicate_record_field', params: { field: 'Email' } }, { translate }))
.toBe('Email taken');
});

it.each([
['no spaces', 'Hi {{name}}!', 'Hi Ada!'],
['spaces inside the braces', 'Hi {{ name }}!', 'Hi Ada!'],
['a tab and two spaces', 'Hi {{ name\t}}!', 'Hi Ada!'],
['single braces (not a placeholder)', 'Hi {name}!', 'Hi {name}!'],
['a spaced hole with no value', 'Hi {{ missing }}!', 'Hi {{ missing }}!'],
])('a deployment override is filled the way the Console does: %s', (_form, template, console) => {
// Each expected value is what the Console's i18next answers for the same
// template and params; a hole with no value stays exactly as written.
const translate = (key: string) => (key === 'errors.duplicate_record_field' ? template : key);
expect(renderOperationMessage({ messageKey: 'duplicate_record_field', params: { name: 'Ada' } }, { translate }))
.toBe(console);
});

it('an override key that misses (echoed back) falls through to the built-in', () => {
const translate = (key: string) => key;
expect(renderOperationMessage({ messageKey: 'delete_restricted', params: PARAMS }, { locale: 'zh-CN', translate }))
Expand Down
10 changes: 6 additions & 4 deletions packages/spec/src/system/operation-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@
*
* ## Interpolation
*
* `{{name}}` placeholders, matching `II18nService.t()`'s convention. An unknown
* placeholder is left verbatim so a broken override is visible rather than
* silently blank — {@link interpolateValidationMessage} is shared with the
* sibling catalog so the two cannot drift on this.
* `{{name}}` placeholders, matching `II18nService.t()`'s convention; whitespace
* inside the braces is allowed (`{{ name }}`), as the Console reads the same
* translation string. An unknown placeholder is left exactly as written so a
* broken override is visible rather than silently blank —
* {@link interpolateValidationMessage} is shared with the sibling catalog so
* the two cannot drift on this.
*/

import { resolveBundleLocale } from './i18n-resolver';
Expand Down
17 changes: 13 additions & 4 deletions packages/spec/src/system/validation-message.override-once.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
* field label by a second pass.
*
* The translator is shaped as `FileI18nAdapter.t` answers: handed `params`, it
* fills `{{name}}` itself. Asked for the template with params, its answer would
* already carry the user's `{{label}}`, and the renderer's own pass would then
* expand it. Asked without params, it returns the template and the renderer
* fills `{{name}}` itself (inner whitespace allowed). Asked for the template
* with params, its answer would already carry the user's `{{label}}`, and the
* renderer's own pass would then expand it. Asked without params, it returns the template and the renderer
* fills it exactly once.
*/
describe('renderValidationMessage — a deployment override is interpolated once', () => {
Expand All @@ -25,7 +25,7 @@ describe('renderValidationMessage — a deployment override is interpolated once
const interpolatingTranslate = (k: string, _locale: string, params?: Record<string, unknown>) => {
if (k !== key) return k;
return params && Object.keys(params).length > 0
? overrideTemplate.replace(/\{\{(\w+)\}\}/g, (m, name: string) => (params[name] != null ? String(params[name]) : m))
? overrideTemplate.replace(/\{\{\s*(\w+)\s*\}\}/g, (m, name: string) => (params[name] != null ? String(params[name]) : m))
: overrideTemplate;
};
const written = '{{label}} plan';
Expand All @@ -41,6 +41,15 @@ describe('renderValidationMessage — a deployment override is interpolated once
.toBe(`套餐的取值「${written}」无效,请从 basic, pro 中选择`);
});

it('prints a written value containing a spaced placeholder as written on the override path', () => {
// The renderer now reads `{{ label }}` as a placeholder in a TEMPLATE; a
// value that carries one must still be printed as written, not expanded.
const spacedWritten = '{{ label }} plan';
const spacedInput = { ...input, params: { value: spacedWritten, allowed: 'basic, pro' } };
expect(renderValidationMessage(spacedInput, { locale: 'zh-CN', translate: interpolatingTranslate }))
.toBe(`套餐的取值「${spacedWritten}」无效,请从 basic, pro 中选择`);
});

it('CONTROL — the built-in path, which was already single-pass, prints it as written too', () => {
expect(renderValidationMessage(input, { locale: 'zh-CN' })).toBe(
BUILTIN_VALIDATION_MESSAGES['zh-CN'].invalid_option_value
Expand Down
30 changes: 30 additions & 0 deletions packages/spec/src/system/validation-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ describe('interpolateValidationMessage', () => {
expect(renderValidationMessage({ messageKey: 'no_such_key', label: 'Qty' }))
.toBe('Qty (no_such_key)');
});

// One translation string feeds this renderer, the server `t()` and the
// Console's i18next, so a hole the Console fills must be filled here too, or
// a 400 shows the braces. Each expected value is what i18next (the Console's
// default `{{ }}` interpolation) answers for the same template and params.
const spellings: Array<[string, string, string]> = [
['no spaces', 'Hi {{name}}!', 'Hi Ada!'],
['spaces inside the braces', 'Hi {{ name }}!', 'Hi Ada!'],
['a tab and two spaces', 'Hi {{ name\t}}!', 'Hi Ada!'],
['single braces (not a placeholder)', 'Hi {name}!', 'Hi {name}!'],
['a spaced hole with no value', 'Hi {{ missing }}!', 'Hi {{ missing }}!'],
];

it.each(spellings)('fills the way the Console does: %s', (_form, template, console) => {
expect(interpolateValidationMessage(template, { name: 'Ada' })).toBe(console);
});

it.each(spellings)('a deployment override is filled the way the Console does: %s', (_form, template, console) => {
const key = validationMessageTranslationKey('required');
const translate = (k: string) => (k === key ? template : k);
expect(renderValidationMessage({ messageKey: 'required', label: 'Qty', params: { name: 'Ada' } }, { translate }))
.toBe(console);
});

it('a spaced override names the field label, as the import runner renders it', () => {
const key = validationMessageTranslationKey('required');
const translate = (k: string) => (k === key ? '{{ label }} is needed' : k);
expect(renderValidationMessage({ messageKey: 'required', label: 'Qty' }, { translate }))
.toBe('Qty is needed');
});
});

/**
Expand Down
18 changes: 12 additions & 6 deletions packages/spec/src/system/validation-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@
* in the caller's locale) and `{{field}}` (its API name). A few are filled from
* the refusing door's message-only parameters, which name a fact of the
* platform rather than a constraint of the field and are never shipped on the
* error (`{{firstYear}}` / `{{lastYear}}` above). An unknown placeholder is
* left verbatim so a broken override is visible rather than silently blank.
* error (`{{firstYear}}` / `{{lastYear}}` above). Whitespace inside the braces
* is allowed (`{{ label }}`), as the Console's i18next and the server `t()`
* read the same translation string, so a deployment override renders the same
* on both sides. An unknown placeholder is left exactly as written (spaces
* included) so a broken override is visible rather than silently blank.
*/

import { resolveBundleLocale } from './i18n-resolver';
Expand Down Expand Up @@ -380,16 +383,19 @@ export const BUILTIN_VALIDATION_MESSAGES: Record<string, Record<string, string>>
export const VALIDATION_MESSAGE_FALLBACK_LOCALE = 'en';

/**
* Fill `{{name}}` placeholders from `params`. An unresolved placeholder is left
* verbatim (`{{min}}`) rather than blanked, so a malformed override or a missing
* param is visible in the message instead of producing "must be ≥ .".
* Fill `{{name}}` placeholders from `params`. Whitespace inside the braces is
* allowed (`{{ name }}`), matching the Console's i18next and the server `t()`;
* single braces (`{name}`) are not a placeholder. An unresolved placeholder is
* left exactly as written (`{{min}}`, `{{ min }}`) rather than blanked, so a
* malformed override or a missing param is visible in the message instead of
* producing "must be ≥ .".
*/
export function interpolateValidationMessage(
template: string,
params: Record<string, unknown> | undefined,
): string {
if (!params) return template;
return template.replace(/\{\{(\w+)\}\}/g, (whole, name: string) => {
return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (whole, name: string) => {
const value = params[name];
return value === undefined || value === null ? whole : String(value);
});
Expand Down
Loading