Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/spec/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
export * from './identifiers.zod';
export * from './mapping.zod';
export * from './http.zod';
export * from './zod-helpers';
24 changes: 24 additions & 0 deletions packages/spec/src/shared/zod-helpers.ts
Original file line number Diff line number Diff line change
@@ -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<number>
* ```
*
* @see https://github.com/colinhacks/zod/issues/4143
*/
export const createAsyncFunctionSchema = <T extends z.ZodFunction<any, any>>(_schema: T) =>
z.custom<Parameters<T['implementAsync']>[0]>((fn) => typeof fn === 'function');
11 changes: 7 additions & 4 deletions packages/spec/src/system/events.zod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { EventNameSchema } from '../shared/identifiers.zod';
import { createAsyncFunctionSchema } from '../shared/zod-helpers';

// ==========================================
// Event Priority
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions packages/spec/src/system/job.zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { createAsyncFunctionSchema } from '../shared/zod-helpers';

/**
* Cron Schedule Schema
Expand Down Expand Up @@ -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'),
Expand Down
71 changes: 43 additions & 28 deletions packages/spec/src/system/plugin.zod.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -101,30 +106,40 @@ export const PluginContextSchema = z.object({
export type PluginContextData = z.infer<typeof PluginContextSchema>;

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<typeof PluginLifecycleSchema>;
Expand Down