From 72604c74dafffeebf360e35f2fc03d94385214ab Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 14 Feb 2023 06:37:46 +0900 Subject: [PATCH] refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` (#46) * refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` * add changeset --- .changeset/gentle-buses-roll.md | 5 +++++ packages/zod-validator/README.md | 16 ++++++++++++++-- packages/zod-validator/src/index.ts | 10 +++++----- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 .changeset/gentle-buses-roll.md 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) {