refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` (#46)
* refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` * add changesetpull/47/head
parent
95ca0fcad3
commit
72604c74da
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-validator': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
refactor: rename `ValidationType` to `ValidationTargets`
|
|
@ -1,7 +1,5 @@
|
||||||
# Zod validator middleware for Hono
|
# Zod validator middleware for Hono
|
||||||
|
|
||||||
**WIP**
|
|
||||||
|
|
||||||
The validator middleware using [Zod](https://zod.dev) for [Hono](https://honojs.dev) applications.
|
The validator middleware using [Zod](https://zod.dev) for [Hono](https://honojs.dev) applications.
|
||||||
You can write a schema with Zod and validate the incoming values.
|
You can write a schema with Zod and validate the incoming values.
|
||||||
|
|
||||||
|
@ -25,6 +23,20 @@ app.post('/author', zValidator('json', schema), (c) => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Hook:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
app.post(
|
||||||
|
'/post',
|
||||||
|
zValidator('json', schema, (result, c) => {
|
||||||
|
if (!result.success) {
|
||||||
|
return c.text('Invalid!', 400)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
//...
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
## Author
|
## Author
|
||||||
|
|
||||||
Yusuke Wada <https://github.com/yusukebe>
|
Yusuke Wada <https://github.com/yusukebe>
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { validator } from 'hono/validator'
|
||||||
import type { z } from 'zod'
|
import type { z } from 'zod'
|
||||||
import type { ZodSchema, ZodError } from 'zod'
|
import type { ZodSchema, ZodError } from 'zod'
|
||||||
|
|
||||||
type ValidationTypes = 'json' | 'form' | 'query' | 'queries'
|
type ValidationTargets = 'json' | 'form' | 'query' | 'queries'
|
||||||
type Hook<T> = (
|
type Hook<T> = (
|
||||||
result: { success: true; data: T } | { success: false; error: ZodError },
|
result: { success: true; data: T } | { success: false; error: ZodError },
|
||||||
c: Context
|
c: Context
|
||||||
|
@ -11,15 +11,15 @@ type Hook<T> = (
|
||||||
|
|
||||||
export const zValidator = <
|
export const zValidator = <
|
||||||
T extends ZodSchema,
|
T extends ZodSchema,
|
||||||
Type extends ValidationTypes,
|
Target extends ValidationTargets,
|
||||||
E extends Env,
|
E extends Env,
|
||||||
P extends string
|
P extends string
|
||||||
>(
|
>(
|
||||||
type: Type,
|
target: Target,
|
||||||
schema: T,
|
schema: T,
|
||||||
hook?: Hook<z.infer<T>>
|
hook?: Hook<z.infer<T>>
|
||||||
): MiddlewareHandler<E, P, { [K in Type]: z.infer<T> }> =>
|
): MiddlewareHandler<E, P, { [K in Target]: z.infer<T> }> =>
|
||||||
validator(type, (value, c) => {
|
validator(target, (value, c) => {
|
||||||
const result = schema.safeParse(value)
|
const result = schema.safeParse(value)
|
||||||
|
|
||||||
if (hook) {
|
if (hook) {
|
||||||
|
|
Loading…
Reference in New Issue