How to document Zod v4 function schemas
mainIn Zod v4, z.function is a function factory rather than a Zod schema. To ensure zod2md can find and document the input/output schemas of a function, you must use a workaround that attaches the factory to the schema's metadata using the $ZodFunction key.
Use the following helper pattern to wrap your function factories:
import { z } from 'zod/v4';
import type { $ZodFunction } from 'zod/v4/core';
export function convertZodFunctionToSchema<T extends $ZodFunction>(factory: T) {
return z
.custom()
.transform((arg, ctx) => {
if (typeof arg !== 'function') {
ctx.addIssue('Must be a function');
return z.NEVER;
}
return factory.implement(arg as Parameters<T['implement']>[0]);
})
.meta({
$ZodFunction: factory, // 👈 This metadata enables zod2md to find input/output schemas
});
}
// Usage
export const predicateSchema = convertZodFunctionToSchema(
z.function({ input: [z.string(), z.number()], output: z.boolean() }),
);