Skip to content

Commit 1a6ba01

Browse files
fix: strip only the error objects ajv assigns
The regex removing error objects also matched a schema string or pattern that only looked like one, such as `\{instancePathx,keyword:y,`, and changed what it matched. It now needs the assignment and the exact shape ajv writes. webpack's minified options check is byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QYPkbvwyRWt2SHgXDoYRdo
1 parent dd1e1a5 commit 1a6ba01

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/precompile.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,12 @@ async function minify(code, esm) {
272272
});
273273
}
274274

275-
// Keep in sync with webpack's `tooling/precompile-schemas.js`, which does the same
276-
const stripped = code.replace(/\{instancePath[^{}]+,keyword:[^{}]+,/g, "{");
275+
// Only an error object ajv assigns, never a string or regex of the schema that merely looks like
276+
// one: `= {instancePath:instancePath+…,schemaPath:"…",keyword:"…",` loses all but its `params`
277+
const stripped = code.replace(
278+
/(=\s*\[?)\{instancePath(?::instancePath\b[^{}]*?)?,schemaPath:"(?:[^"\\]|\\.)*",keyword:"\w+",/g,
279+
"$1{",
280+
);
277281
const result = await terser.minify(stripped, {
278282
compress: { passes: 3 },
279283
mangle: true,

‎test/precompile.test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ describe("precompile", () => {
189189
expect(check({ other: true })).toBe(false);
190190
});
191191

192+
it("should keep a schema pattern that looks like an error object", async () => {
193+
const check = load(
194+
await precompile(
195+
{ type: "string", pattern: "\\{instancePathx,keyword:y," },
196+
{ minify: true },
197+
),
198+
);
199+
200+
expect(check("{instancePathx,keyword:y,")).toBe(true);
201+
expect(check("other")).toBe(false);
202+
});
203+
192204
it("should keep an ES module", async () => {
193205
const code = await precompile(
194206
{ type: "object", properties: { date: { format: "date" } } },

0 commit comments

Comments
 (0)