honojs-middleware/packages/zod-validator
github-actions[bot] 118c43bd74
Version Packages (#49)
2023-02-18 23:29:55 +09:00
..
src refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` (#46) 2023-02-14 06:37:46 +09:00
test chore(zod-validator): bump up Hono (#40) 2023-02-09 20:32:51 +09:00
CHANGELOG.md Version Packages (#49) 2023-02-18 23:29:55 +09:00
README.md refactor(zod-validator): rename `VaildatorTypes` to `ValidatoerTargets` (#46) 2023-02-14 06:37:46 +09:00
jest.config.js feat: introduce zod-validator (#16) 2023-01-02 00:03:44 +09:00
package.json Version Packages (#49) 2023-02-18 23:29:55 +09:00
tsconfig.cjs.json feat: introduce zod-validator (#16) 2023-01-02 00:03:44 +09:00
tsconfig.esm.json feat: introduce zod-validator (#16) 2023-01-02 00:03:44 +09:00
tsconfig.json feat: introduce zod-validator (#16) 2023-01-02 00:03:44 +09:00

README.md

Zod validator middleware for Hono

The validator middleware using Zod for Hono applications. You can write a schema with Zod and validate the incoming values.

Usage

import { z } from 'zod'
import { zValidator } from '../src'

const schema = z.object({
  name: z.string(),
  age: z.number(),
})

app.post('/author', zValidator('json', schema), (c) => {
  const data = c.req.valid()
  return c.json({
    success: true,
    message: `${data.name} is ${data.age}`,
  })
})

Hook:

app.post(
  '/post',
  zValidator('json', schema, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400)
    }
  })
  //...
)

Author

Yusuke Wada https://github.com/yusukebe

License

MIT