docs(zod-validator) - Update README.md to include an example with a custom validator wrapper for improved type inference (#922)

* docs: add custom validator example for improved type inference

* Update README.md - removed default value of target
pull/925/head
Farhad Faraji 2025-01-05 12:34:45 +03:30 committed by GitHub
parent 88135cbee5
commit 8a2d4651b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 6 deletions

View File

@ -47,12 +47,18 @@ 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) =>
export const zValidator = <
T extends ZodSchema,
Target extends keyof ValidationTargets
>(
target: Target,
schema: T
) =>
zv(target, schema, (result, c) => {
if (!result.success) {
throw new HTTPException(400, { cause: result.error });
}
})
});
// usage
import { zValidator } from './validator-wrapper'