diff --git a/packages/spec/src/shared/index.ts b/packages/spec/src/shared/index.ts index efa0e42b508..ffc74e35473 100644 --- a/packages/spec/src/shared/index.ts +++ b/packages/spec/src/shared/index.ts @@ -6,3 +6,4 @@ export * from './identifiers.zod'; export * from './mapping.zod'; export * from './http.zod'; +export * from './zod-helpers'; diff --git a/packages/spec/src/shared/zod-helpers.ts b/packages/spec/src/shared/zod-helpers.ts new file mode 100644 index 00000000000..877a5c45e39 --- /dev/null +++ b/packages/spec/src/shared/zod-helpers.ts @@ -0,0 +1,24 @@ +import { z } from 'zod'; + +/** + * Helper to create async function schema compatible with Zod v4. + * + * In Zod v4, z.function with z.promise doesn't correctly infer async function types. + * This helper wraps the function schema with z.custom to ensure proper TypeScript + * type inference for async functions. + * + * @example + * ```ts + * const asyncFn = createAsyncFunctionSchema( + * z.function({ + * input: z.tuple([z.string()]), + * output: z.promise(z.number()) + * }) + * ); + * // Type is inferred as: (arg: string) => Promise + * ``` + * + * @see https://github.com/colinhacks/zod/issues/4143 + */ +export const createAsyncFunctionSchema = >(_schema: T) => + z.custom[0]>((fn) => typeof fn === 'function'); diff --git a/packages/spec/src/system/events.zod.ts b/packages/spec/src/system/events.zod.ts index 6adda635a9c..21b182629b4 100644 --- a/packages/spec/src/system/events.zod.ts +++ b/packages/spec/src/system/events.zod.ts @@ -1,5 +1,6 @@ import { z } from 'zod'; import { EventNameSchema } from '../shared/identifiers.zod'; +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; // ========================================== // Event Priority @@ -136,10 +137,12 @@ export const EventHandlerSchema = z.object({ /** * Handler function */ - handler: z.function({ - input: z.tuple([EventSchema]), - output: z.promise(z.void()) - }).describe('Handler function'), + handler: createAsyncFunctionSchema( + z.function({ + input: z.tuple([EventSchema]), + output: z.promise(z.void()) + }) + ).describe('Handler function'), /** * Execution priority diff --git a/packages/spec/src/system/job.zod.ts b/packages/spec/src/system/job.zod.ts index d0e52fe2528..ade743531f4 100644 --- a/packages/spec/src/system/job.zod.ts +++ b/packages/spec/src/system/job.zod.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; /** * Cron Schedule Schema @@ -64,10 +65,12 @@ export const JobSchema = z.object({ id: z.string().describe('Unique job identifier'), name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Job name (snake_case)'), schedule: ScheduleSchema.describe('Job schedule configuration'), - handler: z.function({ - input: z.tuple([]), - output: z.promise(z.void()) - }).describe('Job handler function'), + handler: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]), + output: z.promise(z.void()) + }) + ).describe('Job handler function'), retryPolicy: RetryPolicySchema.optional().describe('Retry policy configuration'), timeout: z.number().int().positive().optional().describe('Timeout in milliseconds'), enabled: z.boolean().default(true).describe('Whether the job is enabled'), diff --git a/packages/spec/src/system/plugin.zod.ts b/packages/spec/src/system/plugin.zod.ts index 28273fdeaa4..05745c28c4f 100644 --- a/packages/spec/src/system/plugin.zod.ts +++ b/packages/spec/src/system/plugin.zod.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; // We use z.any() for services that are interfaces with methods, // as Zod cannot easily validate function signatures at runtime. @@ -49,14 +50,18 @@ export const PluginContextSchema = z.object({ input: z.tuple([]).rest(z.any()), output: z.any() }), - set: z.function({ - input: z.tuple([]).rest(z.any()), - output: z.promise(z.void()) - }), - delete: z.function({ - input: z.tuple([]).rest(z.any()), - output: z.promise(z.void()) - }), + set: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]).rest(z.any()), + output: z.promise(z.void()) + }) + ), + delete: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]).rest(z.any()), + output: z.promise(z.void()) + }) + ), }).passthrough().describe('Storage Interface'), i18n: z.object({ @@ -101,30 +106,40 @@ export const PluginContextSchema = z.object({ export type PluginContextData = z.infer; export const PluginLifecycleSchema = z.object({ - onInstall: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onInstall: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onEnable: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onEnable: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onDisable: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onDisable: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onUninstall: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onUninstall: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onUpgrade: z.function({ - input: z.tuple([PluginContextSchema, z.string(), z.string()]), - output: z.promise(z.void()) - }).optional(), + onUpgrade: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema, z.string(), z.string()]), + output: z.promise(z.void()) + }) + ).optional(), }); export type PluginLifecycleHooks = z.infer;