Files
repomix-mirror/website/client/scripts/normalizeJsonSchema.ts
Kazuki Yamada 53ddb5ac36 test(website): Extract and cover normalizeObjectNode
intent(test-coverage): normalizeObjectNode is the load-bearing workaround for @valibot/to-json-schema's output gaps (missing additionalProperties:false, noisy empty required arrays). A bug in its tree walk would silently break the published repomix.config.json IntelliSense schema. Extract to its own module and pin the behavior: adding/respecting additionalProperties, stripping empty required, recursing through properties / anyOf / oneOf / items, handling null and primitives
2026-04-18 23:13:43 +09:00

25 lines
1.0 KiB
TypeScript

// @valibot/to-json-schema quirks:
// - Does not emit `additionalProperties: false` for `v.object`, even though
// Valibot strips unknown keys at runtime. We add it so editors flag typos.
// - Emits an empty `required: []` on every object node, which is valid but noisy.
// We strip empty arrays to match the previous zod-generated output.
// Mutates the tree in place — the caller passes the root schema and discards
// the return value.
export const normalizeObjectNode = (node: unknown): void => {
if (!node || typeof node !== 'object') return;
if (Array.isArray(node)) {
for (const item of node) normalizeObjectNode(item);
return;
}
const obj = node as Record<string, unknown>;
if (obj.type === 'object' && obj.properties && typeof obj.properties === 'object') {
if (!('additionalProperties' in obj)) {
obj.additionalProperties = false;
}
if (Array.isArray(obj.required) && obj.required.length === 0) {
delete obj.required;
}
}
for (const value of Object.values(obj)) normalizeObjectNode(value);
};