Skip to content

Commit 9ba1160

Browse files
committed
fix(i18n): a rule the packaged owner does not declare keeps its translation
A rule an objectExtensions entry folds onto a packaged object is absent from the owner's declaration by construction and is translated under its own name. Counting it as diverged served it untranslated on both readers. A rule with no packaged counterpart now has nothing to diverge from, as with no base at all, and the catalog applies. Claude-Session: https://claude.ai/code/session_01L6bhgW82YGUW8s8X4Dgiqc Co-authored-by: Claude <noreply@anthropic.com>
1 parent 431a7c2 commit 9ba1160

6 files changed

Lines changed: 79 additions & 44 deletions

File tree

‎packages/objectql/src/engine-validation-tenant-rewrite.test.ts‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
* The catalog translates the PACKAGED rule, so under `zh-CN`:
1111
*
1212
* - the rewritten rule must answer the tenant's own sentence, as written;
13-
* - the rule the tenant left alone must still answer its translation.
13+
* - the rule the tenant left alone must still answer its translation;
14+
* - a rule a second package folds on through `objectExtensions` — absent
15+
* from the owner's declaration by construction — must still answer its own
16+
* package's translation: it has no packaged message to diverge from.
1417
*
1518
* Both readers are driven off ONE real registry: the refusal of a real write
1619
* (the engine's validator), and the served object as `/meta` translates it —
@@ -26,14 +29,29 @@ import { ObjectQL } from './index.js';
2629

2730
const OBJ = 'tr_duty';
2831
const PKG = 'com.objectstack.test.tenant-rewrite';
32+
const EXT_PKG = 'com.objectstack.test.tenant-rewrite-extension';
2933

3034
const PACKAGED = {
3135
returned_needs_note: 'Say why the duty is being returned.',
3236
done_needs_owner: 'A finished duty needs an owner.',
37+
ext_blocked_needs_reason: 'Say what is blocking the duty.',
3338
};
3439
const ZH = {
3540
returned_needs_note: '请写明打回的原因。',
3641
done_needs_owner: '已完成的职责需要负责人。',
42+
ext_blocked_needs_reason: '请写明阻塞原因。',
43+
};
44+
45+
/** The extension package's own rule, folded onto the object. */
46+
const EXTENSION = {
47+
name: OBJ,
48+
validations: [{
49+
type: 'script',
50+
name: 'ext_blocked_needs_reason',
51+
message: PACKAGED.ext_blocked_needs_reason,
52+
condition: "record.status == 'blocked' && record.return_note == null",
53+
fields: ['return_note'],
54+
}],
3755
};
3856
const REWRITE = 'Tell the owner what to change before you send it back.';
3957

@@ -72,6 +90,7 @@ const BUNDLE: Record<string, any> = {
7290
_validations: {
7391
returned_needs_note: { message: PACKAGED.returned_needs_note },
7492
done_needs_owner: { message: PACKAGED.done_needs_owner },
93+
ext_blocked_needs_reason: { message: PACKAGED.ext_blocked_needs_reason },
7594
},
7695
},
7796
},
@@ -82,6 +101,7 @@ const BUNDLE: Record<string, any> = {
82101
_validations: {
83102
returned_needs_note: { message: ZH.returned_needs_note },
84103
done_needs_owner: { message: ZH.done_needs_owner },
104+
ext_blocked_needs_reason: { message: ZH.ext_blocked_needs_reason },
85105
},
86106
},
87107
},
@@ -134,6 +154,7 @@ describe('ADR-0029 D9.2a a tenant-rewritten rule message, on the refusal and on
134154
await engine.init();
135155
engine.setI18nService(servedI18n(BUNDLE) as any);
136156
engine.registry.registerObject(body(PACKAGED.returned_needs_note) as any, PKG);
157+
engine.registry.registerObject(EXTENSION as any, EXT_PKG, undefined, 'extend');
137158
if (overlay) {
138159
engine.registry.registerObject({ ...body(REWRITE), _provenance: 'org' } as any, PKG, undefined, 'overlay');
139160
}
@@ -170,6 +191,7 @@ describe('ADR-0029 D9.2a a tenant-rewritten rule message, on the refusal and on
170191
refusal: {
171192
returned_needs_note: await refusal(header, { id: 'r1', status: 'returned' }),
172193
done_needs_owner: await refusal(header, { id: 'r2', status: 'done' }),
194+
ext_blocked_needs_reason: await refusal(header, { id: 'r3', status: 'blocked' }),
173195
},
174196
};
175197
}
@@ -180,24 +202,32 @@ describe('ADR-0029 D9.2a a tenant-rewritten rule message, on the refusal and on
180202
beforeEach(async () => { await boot(true); });
181203

182204
it('the overlay is what both readers see, over a packaged owner that is still there', () => {
205+
const served = (engine.registry.getObject(OBJ) as any).validations.map((r: any) => r.name);
206+
expect(served).toEqual(['returned_needs_note', 'done_needs_owner', 'ext_blocked_needs_reason']);
183207
expect((engine.registry.getObject(OBJ) as any).validations[0].message).toBe(REWRITE);
184-
expect((protocol.getPackagedObjectBase(OBJ) as any).validations[0].message).toBe(PACKAGED.returned_needs_note);
208+
const packaged = protocol.getPackagedObjectBase(OBJ) as any;
209+
expect(packaged.validations.map((r: any) => r.name)).toEqual(['returned_needs_note', 'done_needs_owner']);
210+
expect(packaged.validations[0].message).toBe(PACKAGED.returned_needs_note);
185211
});
186212

187-
it('serves and refuses with the rewrite as written, and still translates the untouched rule', async () => {
188-
const both = (zh: string) => ({ returned_needs_note: REWRITE, done_needs_owner: zh });
213+
it('serves and refuses with the rewrite as written, and still translates the untouched and extension rules', async () => {
214+
const rows = (t: typeof ZH) => ({
215+
returned_needs_note: REWRITE,
216+
done_needs_owner: t.done_needs_owner,
217+
ext_blocked_needs_reason: t.ext_blocked_needs_reason,
218+
});
189219
expect(await table()).toEqual({
190-
'zh-CN': { served: both(ZH.done_needs_owner), refusal: both(ZH.done_needs_owner) },
191-
zh: { served: both(ZH.done_needs_owner), refusal: both(ZH.done_needs_owner) },
192-
en: { served: both(PACKAGED.done_needs_owner), refusal: both(PACKAGED.done_needs_owner) },
220+
'zh-CN': { served: rows(ZH), refusal: rows(ZH) },
221+
zh: { served: rows(ZH), refusal: rows(ZH) },
222+
en: { served: rows(PACKAGED), refusal: rows(PACKAGED) },
193223
});
194224
});
195225
});
196226

197227
describe('control: no overlay', () => {
198228
beforeEach(async () => { await boot(false); });
199229

200-
it('translates both packaged rules on both readers', async () => {
230+
it('translates every rule on both readers', async () => {
201231
expect(await table()).toEqual({
202232
'zh-CN': { served: ZH, refusal: ZH },
203233
zh: { served: ZH, refusal: ZH },

‎packages/objectql/src/validation/record-validator.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ export interface ValidationMessageContext {
413413
* translation) is not applied to it.
414414
*
415415
* Absent when no packaged owner is registered for the object (a
416-
* runtime-authored object, a registry-less host): nothing is inferred and
417-
* the catalog applies, as on `/meta` with no base. Present, `undefined`
418-
* means the packaged declaration carries no rule of that name, so the
419-
* served rule was authored after the fact.
416+
* runtime-authored object, a registry-less host), and `undefined` for a rule
417+
* the packaged declaration does not carry (one an `objectExtensions` entry
418+
* folds on, translated under its own name): either way there is nothing to
419+
* diverge from, and the catalog applies, as on `/meta`.
420420
*/
421421
packagedRule?: (ruleName: string) => unknown;
422422
}

‎packages/objectql/src/validation/rule-validator.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,8 +2659,8 @@ describe('evaluateValidationRules — authored rule messages resolve from the bu
26592659
* rule of the same name, the sentence is the tenant's own and is returned as
26602660
* written, judged by `scalarOverridesPackagedBase` (the one comparison `/meta`
26612661
* judges the same message by). `messages.packagedRule` is the engine's lookup
2662-
* into the packaged owner's declaration; absent means no packaged base is
2663-
* known, and the catalog applies as it always did.
2662+
* into the packaged owner's declaration; absent, or answering no rule of the
2663+
* name, there is nothing to diverge from and the catalog applies as before.
26642664
*/
26652665
describe('evaluateValidationRules — a tenant-rewritten rule message beats the packaged translation (ADR-0029 D9.2a)', () => {
26662666
const PACKAGED = 'A standing duty must declare a frequency.';
@@ -2705,9 +2705,9 @@ describe('evaluateValidationRules — a tenant-rewritten rule message beats the
27052705
expect(err.message).toBe(ZH);
27062706
});
27072707

2708-
it('counts a rule the packaged object does not declare as authored', () => {
2708+
it('translates a rule the packaged object does not declare (an extension rule), under its own name', () => {
27092709
const [err] = failing(schemaWith(REWRITE), {}, { messages: messages({ packagedRule: () => undefined }) });
2710-
expect(err.message).toBe(REWRITE);
2710+
expect(err.message).toBe(ZH);
27112711
});
27122712

27132713
it('infers nothing without a packaged base: the catalog applies, as before', () => {

‎packages/objectql/src/validation/rule-validator.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,17 +3768,16 @@ export function emitValidationAdvisories(
37683768
* `scalarOverridesPackagedBase`, the one comparison `/meta`'s
37693769
* `translateObjectValidations` judges the same message by (⛔ never a second
37703770
* one here), so the served rule and the refusal of the same write keep
3771-
* showing one sentence. A rule the packaged declaration does not carry counts
3772-
* as diverged; no packaged rule lookup means nothing is inferred.
3771+
* showing one sentence. A rule the packaged declaration does not carry, or no
3772+
* lookup at all, has nothing to diverge from: the catalog applies.
37733773
*/
37743774
function authoredRuleMessage(
37753775
rule: Pick<BaseRule, 'name' | 'message'>,
37763776
messages: ValidationMessageContext | undefined,
37773777
): string {
37783778
if (messages?.translate && messages.objectName && messages.locale
37793779
&& typeof rule.name === 'string' && rule.name.length > 0) {
3780-
if (messages.packagedRule
3781-
&& scalarOverridesPackagedBase(messages.packagedRule(rule.name) ?? {}, 'message', rule.message)) {
3780+
if (scalarOverridesPackagedBase(messages.packagedRule?.(rule.name), 'message', rule.message)) {
37823781
return rule.message;
37833782
}
37843783
const key = objectValidationMessageKey(messages.objectName, rule.name);

‎packages/spec/src/system/i18n-resolver.object-validations.test.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,26 @@ describe('ADR-0029 D9.2a a tenant-rewritten rule message beats the packaged tran
271271
vip_branch: 'Every VIP ticket needs a named owner.',
272272
};
273273

274-
/** The tenant's overlay: two messages rewritten, one rule of its own. */
274+
/**
275+
* The tenant's overlay: two messages rewritten, and one rule the packaged
276+
* object does not declare (as an `objectExtensions` rule is folded on).
277+
*/
275278
const tenantDoc = () => {
276279
const doc = clone(DOC);
277280
doc.validations[0].message = REWRITE.closed_is_final;
278281
doc.validations[3].then.message = REWRITE.vip_branch;
279282
doc.validations.push(rule({
280283
type: 'script',
281-
name: 'tenant_only_rule',
282-
message: 'Our own rule, in our own words.',
284+
name: 'unpackaged_rule',
285+
message: 'A rule the package does not declare.',
283286
condition: 'record.owner == null',
284287
}));
285288
return doc;
286289
};
287290

288-
/** A catalog entry for the tenant's own rule too: it still must not apply. */
291+
/** The catalog carries an entry for the unpackaged rule under its own name. */
289292
const bundle = TranslationBundleSchema.parse({
290-
'zh-CN': localeData({ ...ZH, tenant_only_rule: '不该出现的译文。' }),
293+
'zh-CN': localeData({ ...ZH, unpackaged_rule: '套件未声明的规则。' }),
291294
});
292295

293296
it('serves the rewrite as written, and still translates every rule the tenant left alone', () => {
@@ -299,16 +302,17 @@ describe('ADR-0029 D9.2a a tenant-rewritten rule message beats the packaged tran
299302
wrapper: AUTHORED.wrapper,
300303
vip_branch: REWRITE.vip_branch,
301304
standard_branch: ZH.standard_branch,
302-
// A rule the packaged object does not declare was authored after the fact.
303-
tenant_only_rule: 'Our own rule, in our own words.',
305+
// Nothing to diverge from: its own entry still applies.
306+
unpackaged_rule: '套件未声明的规则。',
304307
});
305308
});
306309

307-
it('counts every named rule as authored when the packaged object declares none', () => {
310+
it('applies the catalog to every rule when the packaged object declares none', () => {
308311
const { validations: _dropped, ...bare } = clone(DOC);
309-
const doc = tenantDoc();
310-
const out = translateObject(doc, bundle, { locale: 'zh-CN', packagedBase: bare }) as any;
311-
expect(out.validations).toBe(doc.validations);
312+
const out = translateObject(tenantDoc(), bundle, { locale: 'zh-CN', packagedBase: bare }) as any;
313+
expect(out.validations[0].message).toBe(ZH.closed_is_final);
314+
expect(out.validations[3].then.message).toBe(ZH.vip_branch);
315+
expect(out.validations[4].message).toBe('套件未声明的规则。');
312316
});
313317

314318
it('infers nothing without a packaged base: the catalog applies, as before', () => {

‎packages/spec/src/system/i18n-resolver.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,9 +4003,15 @@ const MAX_VALIDATION_DEPTH = 10;
40034003
* rule and reworded it — is authored data and keeps its text, judged by
40044004
* {@link valueOverridesPackagedBase} (⛔ never a second comparison) against
40054005
* the rule in {@link TranslateDocumentOptions.packagedBase}'s `validations`,
4006-
* a `conditional` branch by its own name. A rule the base does not declare was
4007-
* authored after the fact, and its message counts as diverged; no base
4008-
* supplied means nothing is inferred and the catalog applies.
4006+
* a `conditional` branch by its own name.
4007+
*
4008+
* A rule the base does not declare has no packaged message to diverge from,
4009+
* so nothing is inferred and the catalog applies, as it does with no base at
4010+
* all (the predicate's own first edge). Unlike a list view, a rule is
4011+
* translated under its OWN name: a rule an `objectExtensions` entry folds onto
4012+
* the object, absent from the owner's declaration by construction, is
4013+
* translated by its package's entry for that name, and counting it as
4014+
* diverged would serve it untranslated.
40094015
*
40104016
* The evaluator applies the same rule to its refusal, through the exported
40114017
* {@link scalarOverridesPackagedBase} and the packaged owner's declaration the
@@ -4034,9 +4040,7 @@ function translateObjectValidations(
40344040
};
40354041
}
40364042
if (typeof rule.name !== 'string' || rule.name.length === 0) return raw;
4037-
if (packaged !== undefined && valueOverridesPackagedBase(packaged.get(rule.name) ?? {}, 'message', rule.message)) {
4038-
return raw;
4039-
}
4043+
if (valueOverridesPackagedBase(packaged.get(rule.name), 'message', rule.message)) return raw;
40404044
const path = objectValidationMessageKey(objectName, rule.name).split('.');
40414045
for (const code of localeChain(opts)) {
40424046
const candidate = path.reduce<unknown>(
@@ -4061,14 +4065,11 @@ function translateObjectValidations(
40614065
/**
40624066
* The packaged object's validation rules by `name`, each `conditional` branch
40634067
* under its own name to {@link MAX_VALIDATION_DEPTH}, for
4064-
* {@link translateObjectValidations}' divergence test. `undefined` when no
4065-
* base is known; an empty map when the base declares no rules, so every rule
4066-
* on the served document then counts as authored. The first rule of a name
4067-
* wins.
4068+
* {@link translateObjectValidations}' divergence test — empty when no base is
4069+
* known or it declares no rules. The first rule of a name wins.
40684070
*/
4069-
function packagedRulesByName(packagedBase: unknown): Map<string, Record<string, unknown>> | undefined {
4071+
function packagedRulesByName(packagedBase: unknown): Map<string, Record<string, unknown>> {
40704072
const base = asRecord(packagedBase);
4071-
if (base === undefined) return undefined;
40724073
const byName = new Map<string, Record<string, unknown>>();
40734074
const visit = (raw: unknown, depth: number): void => {
40744075
const rule = asRecord(raw);
@@ -4080,7 +4081,8 @@ function packagedRulesByName(packagedBase: unknown): Map<string, Record<string,
40804081
}
40814082
if (typeof rule.name === 'string' && rule.name.length > 0 && !byName.has(rule.name)) byName.set(rule.name, rule);
40824083
};
4083-
if (Array.isArray(base.validations)) for (const raw of base.validations) visit(raw, 0);
4084+
const rules = base?.validations;
4085+
if (Array.isArray(rules)) for (const raw of rules) visit(raw, 0);
40844086
return byName;
40854087
}
40864088

0 commit comments

Comments
 (0)