import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono' import { validator } from 'hono/validator' import type { z, ZodSchema, ZodError } from 'zod' export type Hook = ( result: { success: true; data: T } | { success: false; error: ZodError; data: T }, c: Context ) => Response | Promise | void | Promise | TypedResponse type HasUndefined = undefined extends T ? true : false export const zValidator = < T extends ZodSchema, Target extends keyof ValidationTargets, E extends Env, P extends string, I = z.input, O = z.output, V extends { in: HasUndefined extends true ? { [K in Target]?: I } : { [K in Target]: I } out: { [K in Target]: O } } = { in: HasUndefined extends true ? { [K in Target]?: I } : { [K in Target]: I } out: { [K in Target]: O } } >( target: Target, schema: T, hook?: Hook, E, P> ): MiddlewareHandler => validator(target, async (value, c) => { const result = await schema.safeParseAsync(value) if (hook) { const hookResult = hook({ data: value, ...result }, c) if (hookResult) { if (hookResult instanceof Response || hookResult instanceof Promise) { return hookResult } if ('response' in hookResult) { return hookResult.response } } } if (!result.success) { return c.json(result, 400) } const data = result.data as z.infer return data })