feat(zod-validator): add `validationFunction` option (#1140)

Co-authored-by: migawka <migawka@amadeustech.dev>
pull/1143/head
Yusuke Wada 2025-04-27 20:12:13 +09:00 committed by GitHub
parent b9fa57530a
commit 8ed99d9d79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 124 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-validator': minor
---
feat: add `validationFunction` option

View File

@ -68,6 +68,30 @@ app.post(
) )
``` ```
### Custom validation function
By default, this Validator validates values using `.safeParseAsync`.
```ts
await schema.safeParseAsync(value)
```
But, if you want to use the [`.passthrough`](https://zod.dev/?id=passthrough), you can specify your own function in `validationFunction`.
```ts
app.post(
'/',
zValidator('json', schema, undefined, {
validationFunction: async (schema, value) => {
return await schema.passthrough().safeParseAsync(value)
},
}),
(c) => {
// ...
}
)
```
## Author ## Author
Yusuke Wada <https://github.com/yusukebe> Yusuke Wada <https://github.com/yusukebe>

View File

@ -378,3 +378,85 @@ describe('Case-Insensitive Headers', () => {
type verify = Expect<Equal<Expected, Actual>> type verify = Expect<Equal<Expected, Actual>>
}) })
}) })
describe('With options + validationFunction', () => {
const app = new Hono()
const jsonSchema = z.object({
name: z.string(),
age: z.number(),
})
const route = app
.post('/', zValidator('json', jsonSchema), (c) => {
const data = c.req.valid('json')
return c.json({
success: true,
data,
})
})
.post(
'/extended',
zValidator('json', jsonSchema, undefined, {
validationFunction: async (schema, value) => {
return await schema.passthrough().safeParseAsync(value)
},
}),
(c) => {
const data = c.req.valid('json')
return c.json({
success: true,
data,
})
}
)
it('Should be ok due to passthrough schema', async () => {
const req = new Request('http://localhost/extended', {
body: JSON.stringify({
name: 'Superman',
age: 20,
length: 170,
weight: 55,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
success: true,
data: {
name: 'Superman',
age: 20,
length: 170,
weight: 55,
},
})
})
it('Should be ok due to required schema', async () => {
const req = new Request('http://localhost', {
body: JSON.stringify({
name: 'Superman',
age: 20,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
success: true,
data: {
name: 'Superman',
age: 20,
},
})
})
})

View File

@ -1,7 +1,7 @@
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono' import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
import { validator } from 'hono/validator' import { validator } from 'hono/validator'
import { ZodObject } from 'zod' import { ZodObject } from 'zod'
import type { ZodError, ZodSchema, z } from 'zod' import type { SafeParseReturnType, ZodError, ZodSchema, z } from 'zod'
export type Hook< export type Hook<
T, T,
@ -43,7 +43,14 @@ export const zValidator = <
>( >(
target: Target, target: Target,
schema: T, schema: T,
hook?: Hook<z.infer<T>, E, P, Target> hook?: Hook<z.infer<T>, E, P, Target>,
options?: {
validationFunction: (
schema: T,
value: ValidationTargets[Target]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => SafeParseReturnType<any, any> | Promise<SafeParseReturnType<any, any>>
}
): MiddlewareHandler<E, P, V> => ): MiddlewareHandler<E, P, V> =>
// @ts-expect-error not typed well // @ts-expect-error not typed well
validator(target, async (value, c) => { validator(target, async (value, c) => {
@ -63,7 +70,10 @@ export const zValidator = <
) )
} }
const result = await schema.safeParseAsync(validatorValue) const result =
options && options.validationFunction
? await options.validationFunction(schema, validatorValue)
: await schema.safeParseAsync(validatorValue)
if (hook) { if (hook) {
const hookResult = await hook({ data: validatorValue, ...result, target }, c) const hookResult = await hook({ data: validatorValue, ...result, target }, c)