fix(zod-validator): passing data to hook (#107)
* fix(zod-validator): passing data to hook * changesetpull/108/head
parent
fb34f25205
commit
8eb3967477
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/zod-validator': patch
|
||||
---
|
||||
|
||||
fix passing data to hook
|
|
@ -3,7 +3,7 @@ import { validator } from 'hono/validator'
|
|||
import type { z, ZodSchema, ZodError } from 'zod'
|
||||
|
||||
type Hook<T, E extends Env, P extends string> = (
|
||||
result: { success: true; data: T } | { success: false; error: ZodError },
|
||||
result: { success: true; data: T } | { success: false; error: ZodError; data: T },
|
||||
c: Context<E, P>
|
||||
) => Response | Promise<Response> | void | Promise<Response | void>
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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!')
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue