fix: restore channel sdk schema typing

This commit is contained in:
Peter Steinberger
2026-04-04 08:03:13 +01:00
parent f6df3ed70c
commit 04b539e98c
5 changed files with 97 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";
import { z } from "zod";
import { buildChannelConfigSchema } from "./config-schema.js";
import { buildChannelConfigSchema, emptyChannelConfigSchema } from "./config-schema.js";
describe("buildChannelConfigSchema", () => {
it("builds json schema when toJSONSchema is available", () => {
@@ -46,3 +46,22 @@ describe("buildChannelConfigSchema", () => {
});
});
});
describe("emptyChannelConfigSchema", () => {
it("accepts undefined and empty objects only", () => {
const result = emptyChannelConfigSchema();
expect(result.runtime?.safeParse(undefined)).toEqual({
success: true,
data: undefined,
});
expect(result.runtime?.safeParse({})).toEqual({
success: true,
data: {},
});
expect(result.runtime?.safeParse({ enabled: true })).toEqual({
success: false,
issues: [{ path: [], message: "config must be empty" }],
});
});
});

View File

@@ -104,3 +104,33 @@ export function buildChannelConfigSchema(
},
};
}
export function emptyChannelConfigSchema(): ChannelConfigSchema {
return {
schema: {
type: "object",
additionalProperties: false,
properties: {},
},
runtime: {
safeParse(value) {
if (value === undefined) {
return { success: true, data: undefined };
}
if (!value || typeof value !== "object" || Array.isArray(value)) {
return {
success: false,
issues: [{ path: [], message: "expected config object" }],
};
}
if (Object.keys(value as Record<string, unknown>).length > 0) {
return {
success: false,
issues: [{ path: [], message: "config must be empty" }],
};
}
return { success: true, data: value };
},
},
};
}