Skip to content

The body validator middleware using Zod for Hono applications.

License

Notifications You must be signed in to change notification settings

willin/hono-zod-body-validator

Repository files navigation

Zod body 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 { zBodyValidator } from '@hono-dev/zod-body-validator';

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

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

Hook:

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

赞助 Sponsor

维护者 Owner: Willin Wang

如果您对本项目感兴趣,可以通过以下方式支持我:

Donation ways:

许可证 License

Apache-2.0