Skip to content

Commit

Permalink
docs(zod-validator): add usage docs for zErrValidator (#900)
Browse files Browse the repository at this point in the history
* docs(zod-validator): add usage docs for `zErrValidator`, throw a validating error instead of directly returning an error response. (#890)

* docs(zod-validator): change zod error to Hono HTTPException

* remove useless generic
  • Loading branch information
ktKongTong authored Dec 25, 2024
1 parent c9d96ee commit 755d5cb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/zod-validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/yusukebe>
Expand Down

0 comments on commit 755d5cb

Please sign in to comment.