diff --git a/packages/zod-validator/README.md b/packages/zod-validator/README.md index 34357e13..b7a887e5 100644 --- a/packages/zod-validator/README.md +++ b/packages/zod-validator/README.md @@ -37,6 +37,32 @@ app.post( ) ``` +Throw Error: + +throw a zod validate error instead of directly returning an error response. + +```ts +// file: validator-wrapper.ts +import { ZodSchema } from "zod"; +import type { ValidationTargets } from "hono"; +import { zValidator as zv } from "@hono/zod-validator"; + +export const zValidator = (target: keyof ValidationTargets, schema: ZodSchema) => +zv(target, schema, (result, c) => { + if (!result.success) { + throw new HTTPException(400, { cause: result.error }); + } +}) + +// usage +import { zValidator } from './validator-wrapper' +app.post( + '/post', + zValidator('json', schema) + //... +) +``` + ## Author Yusuke Wada