The validator middleware using Zod for Hono applications. You can write a schema with Zod and validate the incoming values.
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);
}
})
//...
);
维护者 Owner: Willin Wang
如果您对本项目感兴趣,可以通过以下方式支持我:
Donation ways:
- Github: https://github.com/sponsors/willin
- Paypal: https://paypal.me/willinwang
- Alipay or Wechat Pay: QRCode
Apache-2.0