Skip to content

Commit 2b51218

Browse files
committed
fix(spec): refuse a .custom overrides.package with a published pattern, not a refinement
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2fi9zfJhUTubyJYb6V
1 parent 5c2f55d commit 2b51218

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎packages/spec/src/kernel/override-declaration.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { describe, expect, it } from 'vitest';
18+
import { z } from 'zod';
1819
import { OverrideDeclarationSchema, OverrideDeclarationFields, CUSTOMIZATION_PACKAGE_SUFFIX } from './override-declaration.zod';
1920
import { OVERRIDE_TYPE_CLASSIFICATION, OVERRIDABLE_METADATA_TYPES } from './customization-package';
2021
import { getMetadataTypeSchema } from './metadata-type-schemas';
@@ -62,7 +63,20 @@ describe('OverrideDeclarationSchema — the shape', () => {
6263
it(`refuses a \`${CUSTOMIZATION_PACKAGE_SUFFIX}\` package — the declaration names the BASE, never a pair`, () => {
6364
const r = OverrideDeclarationSchema.safeParse({ package: `com.acme.crm${CUSTOMIZATION_PACKAGE_SUFFIX}`, basedOn: '3.2.0' });
6465
expect(r.success).toBe(false);
65-
expect(issuesOf(r)).toContainEqual(expect.objectContaining({ code: 'custom', path: 'package' }));
66+
expect(issuesOf(r)).toContainEqual(expect.objectContaining({ code: 'invalid_format', path: 'package' }));
67+
});
68+
69+
it('accepts an id that merely contains the suffix\'s letters', () => {
70+
for (const id of ['com.acme.customx', 'com.acme.custom-crm', 'custom']) {
71+
expect(OverrideDeclarationSchema.safeParse({ package: id, basedOn: '1.0.0' }).success, id).toBe(true);
72+
}
73+
});
74+
75+
it('publishes the refusal into the JSON Schema as a pattern (no dropped refinement)', () => {
76+
const json = z.toJSONSchema(OverrideDeclarationSchema) as { properties: { package: { pattern?: string } } };
77+
const pattern = new RegExp(json.properties.package.pattern ?? '');
78+
expect(pattern.test('com.acme.crm')).toBe(true);
79+
expect(pattern.test(`com.acme.crm${CUSTOMIZATION_PACKAGE_SUFFIX}`)).toBe(false);
6680
});
6781

6882
it('describes basedOn as recorded and never judged (ADR-0146 D2)', () => {

‎packages/spec/src/kernel/override-declaration.zod.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ import { strictObject } from '../shared/strict-object';
5555
*/
5656
export const CUSTOMIZATION_PACKAGE_SUFFIX = '.custom';
5757

58+
/** {@link CUSTOMIZATION_PACKAGE_SUFFIX}, escaped for a regular expression. */
59+
const ESCAPED_CUSTOMIZATION_PACKAGE_SUFFIX = CUSTOMIZATION_PACKAGE_SUFFIX.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
60+
61+
/** Any non-empty string that does not end in {@link CUSTOMIZATION_PACKAGE_SUFFIX}. */
62+
const NOT_A_CUSTOMIZATION_PACKAGE_ID = new RegExp(`^(?![\\s\\S]*${ESCAPED_CUSTOMIZATION_PACKAGE_SUFFIX}$)[\\s\\S]+$`);
63+
5864
/**
5965
* The override declaration's body: which base package's item this item
6066
* replaces, and which version of that item the author started from.
@@ -71,7 +77,10 @@ export const OverrideDeclarationSchema = strictObject({
7177
}, {
7278
package: z.string()
7379
.min(1)
74-
.refine((id) => !id.endsWith(CUSTOMIZATION_PACKAGE_SUFFIX), {
80+
// A pattern, not a `.refine()`: a pattern publishes into the JSON Schema
81+
// (`json-schema/**`), so a document the file accepts is one the parse
82+
// accepts. Built from the constant, so the suffix has one spelling.
83+
.regex(NOT_A_CUSTOMIZATION_PACKAGE_ID, {
7584
message:
7685
`\`overrides.package\` names the BASE package whose item this item replaces, never a \`${CUSTOMIZATION_PACKAGE_SUFFIX}\` package: `
7786
+ `the declaring item lives in the base's paired \`<base>${CUSTOMIZATION_PACKAGE_SUFFIX}\` package and names the base it overrides `

0 commit comments

Comments
 (0)