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 targetpull/925/head
parent
88135cbee5
commit
8a2d4651b0
|
@ -47,12 +47,18 @@ import { ZodSchema } from "zod";
|
||||||
import type { ValidationTargets } from "hono";
|
import type { ValidationTargets } from "hono";
|
||||||
import { zValidator as zv } from "@hono/zod-validator";
|
import { zValidator as zv } from "@hono/zod-validator";
|
||||||
|
|
||||||
export const zValidator = (target: keyof ValidationTargets, schema: ZodSchema) =>
|
export const zValidator = <
|
||||||
zv(target, schema, (result, c) => {
|
T extends ZodSchema,
|
||||||
if (!result.success) {
|
Target extends keyof ValidationTargets
|
||||||
throw new HTTPException(400, { cause: result.error });
|
>(
|
||||||
}
|
target: Target,
|
||||||
})
|
schema: T
|
||||||
|
) =>
|
||||||
|
zv(target, schema, (result, c) => {
|
||||||
|
if (!result.success) {
|
||||||
|
throw new HTTPException(400, { cause: result.error });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// usage
|
// usage
|
||||||
import { zValidator } from './validator-wrapper'
|
import { zValidator } from './validator-wrapper'
|
||||||
|
|
Loading…
Reference in New Issue