@@ -3852,10 +3852,21 @@ function builtinSystemFieldLabel(
38523852 *
38533853 * ⛔ The three conservative edges above are therefore load-bearing in two
38543854 * places. Widening any of them widens the fold's precedence rule too.
3855+ *
3856+ * ## `message` — a validation rule's, judged on both of its readers
3857+ *
3858+ * A rule's `message` is served on `/meta` ({@link translateObjectValidations})
3859+ * and returned in the refusal of a write by `@objectstack/objectql`'s rule
3860+ * evaluator, and both apply the catalog only while the served message still
3861+ * equals the packaged rule's of the same name (ADR-0029 D9.2a). The evaluator
3862+ * asks through this export, with the packaged RULE as `base`, so a tenant's
3863+ * rewrite is judged by one comparison on both readers and the two keep
3864+ * answering one sentence. The key is the only thing it adds: the three edges
3865+ * above hold for it unchanged.
38553866 */
38563867export function scalarOverridesPackagedBase (
38573868 base : unknown ,
3858- key : 'label' | 'pluralLabel' | 'description' ,
3869+ key : 'label' | 'pluralLabel' | 'description' | 'message' ,
38593870 value : unknown ,
38603871) : boolean {
38613872 return valueOverridesPackagedBase ( base , key , value ) ;
@@ -3985,21 +3996,30 @@ const MAX_VALIDATION_DEPTH = 10;
39853996 * branches are translated and the wrapper's `message` is left as authored, the
39863997 * way the extractor leaves it unscaffolded.
39873998 *
3988- * ## No packaged-base comparison
3989- *
3990- * Unlike the object's scalars, a rule message is a flat `catalog ?? document`,
3991- * as a field `label` is. The evaluator translates a refusal without consulting
3992- * any packaged base, and a divergence rule applied here alone would serve one
3993- * sentence on `/meta` and another in the refusal of the same write, which is
3994- * the split this overlay exists to close.
3999+ * ## The catalog loses to a tenant's rewrite — ADR-0029 D9.2a
4000+ *
4001+ * The catalog translates the PACKAGED rule, so a served message that diverged
4002+ * from the packaged rule of the same name — a tenant overlay that kept the
4003+ * rule and reworded it — is authored data and keeps its text, judged by
4004+ * {@link valueOverridesPackagedBase} (⛔ never a second comparison) against
4005+ * 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.
4009+ *
4010+ * The evaluator applies the same rule to its refusal, through the exported
4011+ * {@link scalarOverridesPackagedBase} and the packaged owner's declaration the
4012+ * engine hands it, so `/meta` and the refusal of the same write still show one
4013+ * sentence for one rule and locale.
39954014 */
39964015function translateObjectValidations (
39974016 validations : unknown ,
39984017 bundle : TranslationBundle | undefined ,
39994018 objectName : string ,
4000- opts : ResolveOptions | undefined ,
4019+ opts : TranslateDocumentOptions | undefined ,
40014020) : unknown {
40024021 if ( ! Array . isArray ( validations ) || ! bundle ) return validations ;
4022+ const packaged = packagedRulesByName ( opts ?. packagedBase ) ;
40034023 const translateRule = ( raw : unknown , depth : number ) : unknown => {
40044024 const rule = asRecord ( raw ) ;
40054025 if ( rule === undefined || depth >= MAX_VALIDATION_DEPTH ) return raw ;
@@ -4014,6 +4034,9 @@ function translateObjectValidations(
40144034 } ;
40154035 }
40164036 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+ }
40174040 const path = objectValidationMessageKey ( objectName , rule . name ) . split ( '.' ) ;
40184041 for ( const code of localeChain ( opts ) ) {
40194042 const candidate = path . reduce < unknown > (
@@ -4035,6 +4058,32 @@ function translateObjectValidations(
40354058 return changed ? next : validations ;
40364059}
40374060
4061+ /**
4062+ * The packaged object's validation rules by `name`, each `conditional` branch
4063+ * 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+ */
4069+ function packagedRulesByName ( packagedBase : unknown ) : Map < string , Record < string , unknown > > | undefined {
4070+ const base = asRecord ( packagedBase ) ;
4071+ if ( base === undefined ) return undefined ;
4072+ const byName = new Map < string , Record < string , unknown > > ( ) ;
4073+ const visit = ( raw : unknown , depth : number ) : void => {
4074+ const rule = asRecord ( raw ) ;
4075+ if ( rule === undefined || depth >= MAX_VALIDATION_DEPTH ) return ;
4076+ if ( rule . type === 'conditional' ) {
4077+ visit ( rule . then , depth + 1 ) ;
4078+ visit ( rule . otherwise , depth + 1 ) ;
4079+ return ;
4080+ }
4081+ if ( typeof rule . name === 'string' && rule . name . length > 0 && ! byName . has ( rule . name ) ) byName . set ( rule . name , rule ) ;
4082+ } ;
4083+ if ( Array . isArray ( base . validations ) ) for ( const raw of base . validations ) visit ( raw , 0 ) ;
4084+ return byName ;
4085+ }
4086+
40384087/**
40394088 * Apply the active locale to an object metadata document. Translates the
40404089 * object's `label` / `pluralLabel` / `description`, walks each field to
@@ -4091,6 +4140,8 @@ function translateObjectValidations(
40914140 * application of the same rule (ADR-0029 D9.2a, as {@link translateView}
40924141 * applies it), judged per view against the packaged object's own `listViews` —
40934142 * see {@link translateObjectListViews}.
4143+ * Each `validations[].message` follows it too, judged against the packaged
4144+ * rule of the same name — see {@link translateObjectValidations}.
40944145 *
40954146 * ## A field's translated help is served on `description` — ADR-0029 D9.2a
40964147 *
0 commit comments