diff --git a/.changeset/gentle-buses-roll.md b/.changeset/gentle-buses-roll.md new file mode 100644 index 00000000..5016fa75 --- /dev/null +++ b/.changeset/gentle-buses-roll.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-validator': patch +--- + +refactor: rename `ValidationType` to `ValidationTargets` diff --git a/packages/zod-validator/README.md b/packages/zod-validator/README.md index e974e171..f0fde26d 100644 --- a/packages/zod-validator/README.md +++ b/packages/zod-validator/README.md @@ -1,7 +1,5 @@ # Zod validator middleware for Hono -**WIP** - 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. @@ -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 Yusuke Wada diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index 7c3051c7..8cc469c3 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 } from 'zod' import type { ZodSchema, ZodError } from 'zod' -type ValidationTypes = 'json' | 'form' | 'query' | 'queries' +type ValidationTargets = 'json' | 'form' | 'query' | 'queries' type Hook = ( result: { success: true; data: T } | { success: false; error: ZodError }, c: Context @@ -11,15 +11,15 @@ type Hook = ( export const zValidator = < T extends ZodSchema, - Type extends ValidationTypes, + Target extends ValidationTargets, E extends Env, P extends string >( - type: Type, + target: Target, schema: T, hook?: Hook> -): MiddlewareHandler }> => - validator(type, (value, c) => { +): MiddlewareHandler }> => + validator(target, (value, c) => { const result = schema.safeParse(value) if (hook) {