diff --git a/.changeset/silent-pianos-wash.md b/.changeset/silent-pianos-wash.md new file mode 100644 index 00000000..e4508973 --- /dev/null +++ b/.changeset/silent-pianos-wash.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-validator': patch +--- + +fix passing data to hook diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index b810590a..8b1e6227 100644 --- a/packages/zod-validator/src/index.ts +++ b/packages/zod-validator/src/index.ts @@ -3,7 +3,7 @@ import { validator } from 'hono/validator' import type { z, ZodSchema, ZodError } from 'zod' type Hook = ( - result: { success: true; data: T } | { success: false; error: ZodError }, + result: { success: true; data: T } | { success: false; error: ZodError; data: T }, c: Context ) => Response | Promise | void | Promise @@ -28,7 +28,7 @@ export const zValidator = < const result = schema.safeParse(value) if (hook) { - const hookResult = hook(result, c) + const hookResult = hook({ data: value, ...result }, c) if (hookResult instanceof Response || hookResult instanceof Promise) { return hookResult } diff --git a/packages/zod-validator/test/index.test.ts b/packages/zod-validator/test/index.test.ts index 9ef8af04..5d2622de 100644 --- a/packages/zod-validator/test/index.test.ts +++ b/packages/zod-validator/test/index.test.ts @@ -88,7 +88,7 @@ describe('With Hook', () => { '/post', zValidator('json', schema, (result, c) => { if (!result.success) { - return c.text('Invalid!', 400) + return c.text(`${result.data.id} is invalid!`, 400) } const data = result.data return c.text(`${data.id} is valid!`) @@ -127,5 +127,6 @@ describe('With Hook', () => { const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(400) + expect(await res.text()).toBe('123 is invalid!') }) })