Skip to content

Commit ad9481f

Browse files
fix(plugins): validate constrained empty config schemas
Route pattern, dependency, and active conditional schemas through the real plugin config validator while keeping standalone conditional annotations on the empty-config path. Co-Authored-By: Claude <[email protected]>
1 parent 923d92b commit ad9481f

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎src/plugins/loader-shared.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, expect, it } from "vitest";
2+
import { validatePluginConfig } from "./loader-shared.js";
3+
4+
const emptyObjectSchema = {
5+
type: "object",
6+
additionalProperties: false,
7+
properties: {},
8+
} as const;
9+
10+
function withSchemaKeyword(key: "if" | "then" | "else", value: unknown) {
11+
return { [key]: value };
12+
}
13+
14+
describe("validatePluginConfig empty schema classification", () => {
15+
it("validates pattern properties instead of requiring empty config", () => {
16+
const schema = {
17+
...emptyObjectSchema,
18+
patternProperties: { "^S_": { type: "string" } },
19+
};
20+
21+
expect(validatePluginConfig({ schema, value: { S_SETTING: "configured" } })).toEqual({
22+
ok: true,
23+
value: { S_SETTING: "configured" },
24+
});
25+
expect(validatePluginConfig({ schema, value: { S_SETTING: 42 } })).toMatchObject({
26+
ok: false,
27+
});
28+
});
29+
30+
it("validates dependent schemas instead of using the empty-config shortcut", () => {
31+
const result = validatePluginConfig({
32+
schema: {
33+
...emptyObjectSchema,
34+
dependentSchemas: { mode: { required: ["token"] } },
35+
},
36+
value: { mode: true },
37+
});
38+
39+
expect(result).toMatchObject({ ok: false });
40+
if (!result.ok) {
41+
expect(result.error.join(" ")).not.toContain("config must be empty");
42+
}
43+
});
44+
45+
it.each([
46+
{
47+
branch: "then",
48+
schema: {
49+
...withSchemaKeyword("if", true),
50+
...withSchemaKeyword("then", { minProperties: 1 }),
51+
},
52+
},
53+
{
54+
branch: "else",
55+
schema: {
56+
...withSchemaKeyword("if", false),
57+
...withSchemaKeyword("else", { minProperties: 1 }),
58+
},
59+
},
60+
])("applies an active $branch conditional", ({ schema }) => {
61+
expect(
62+
validatePluginConfig({ schema: { ...emptyObjectSchema, ...schema }, value: {} }),
63+
).toMatchObject({ ok: false });
64+
});
65+
66+
it.each([
67+
withSchemaKeyword("if", true),
68+
withSchemaKeyword("then", { minProperties: 1 }),
69+
withSchemaKeyword("else", { minProperties: 1 }),
70+
])("keeps standalone conditional keywords on the empty-config path: %o", (keyword) => {
71+
expect(
72+
validatePluginConfig({
73+
schema: { ...emptyObjectSchema, ...keyword },
74+
value: { unexpected: true },
75+
}),
76+
).toEqual({ ok: false, error: ["<root>: config must be empty"] });
77+
});
78+
});

‎src/plugins/loader-shared.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,19 @@ function isEmptyPluginConfigJsonSchema(schema: Record<string, unknown>): boolean
240240
) {
241241
return false;
242242
}
243+
const hasConditional = "if" in schema && ("then" in schema || "else" in schema);
243244
return !(
244245
"required" in schema ||
245246
"dependentRequired" in schema ||
247+
"dependentSchemas" in schema ||
246248
"dependencies" in schema ||
247249
"minProperties" in schema ||
248250
"allOf" in schema ||
249251
"anyOf" in schema ||
250252
"oneOf" in schema ||
251-
"not" in schema
253+
"not" in schema ||
254+
"patternProperties" in schema ||
255+
hasConditional
252256
);
253257
}
254258

0 commit comments

Comments
 (0)