feat(zod-validator): add `validationFunction` option (#1140)
Co-authored-by: migawka <migawka@amadeustech.dev>pull/1143/head
parent
b9fa57530a
commit
8ed99d9d79
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/zod-validator': minor
|
||||
---
|
||||
|
||||
feat: add `validationFunction` option
|
|
@ -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
|
||||
|
||||
Yusuke Wada <https://github.com/yusukebe>
|
||||
|
|
|
@ -378,3 +378,85 @@ describe('Case-Insensitive Headers', () => {
|
|||
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,
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
||||
import { validator } from 'hono/validator'
|
||||
import { ZodObject } from 'zod'
|
||||
import type { ZodError, ZodSchema, z } from 'zod'
|
||||
import type { SafeParseReturnType, ZodError, ZodSchema, z } from 'zod'
|
||||
|
||||
export type Hook<
|
||||
T,
|
||||
|
@ -43,7 +43,14 @@ export const zValidator = <
|
|||
>(
|
||||
target: Target,
|
||||
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> =>
|
||||
// @ts-expect-error not typed well
|
||||
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) {
|
||||
const hookResult = await hook({ data: validatorValue, ...result, target }, c)
|
||||
|
|
Loading…
Reference in New Issue