diff --git a/.changeset/modern-dancers-lick.md b/.changeset/modern-dancers-lick.md new file mode 100644 index 000000000..88bf31a01 --- /dev/null +++ b/.changeset/modern-dancers-lick.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-validator': patch +--- + +fix(zod-validator): support TypedResponse in hook diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index 8b1e6227d..8e0bbda74 100644 --- a/packages/zod-validator/src/index.ts +++ b/packages/zod-validator/src/index.ts @@ -1,11 +1,18 @@ -import type { Context, MiddlewareHandler, Env, ValidationTargets } from 'hono' +import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono' import { validator } from 'hono/validator' import type { z, ZodSchema, ZodError } from 'zod' -type Hook = ( +export type Hook = ( result: { success: true; data: T } | { success: false; error: ZodError; data: T }, c: Context -) => Response | Promise | void | Promise +) => + | Response + | Promise + | void + | Promise + | TypedResponse + | Promise> + | Promise | void> export const zValidator = < T extends ZodSchema, @@ -29,7 +36,12 @@ export const zValidator = < if (hook) { const hookResult = hook({ data: value, ...result }, c) - if (hookResult instanceof Response || hookResult instanceof Promise) { + if ( + hookResult && + (hookResult instanceof Response || + hookResult instanceof Promise || + 'response' in hookResult) + ) { return hookResult } }