🐈
【Drizzle ORM】NextJs14 と Drizzle ORM【#11 Hono Zod Validator 】
【#11 Hono Zod Validator 】
YouTube: https://youtu.be/gGpI7ESOHcU
今回は、Zod Validatorを使用して、
各ルートにバリデーションを設定します。
npm i zod
app/api/[[...route]]/users.ts
import { Hono } from "hono";
import { z } from "zod";
import { zValidator } from "@hono/zod-validator";
import { db } from "@/db/drizzle";
import { users } from "@/db/schema";
import { eq } from "drizzle-orm";
const app = new Hono()
.get("/", async (c) => {
const data = await db.select().from(users);
return c.json({
users: data,
});
})
.get(
"/:id",
zValidator(
"param",
z.object({
id: z.string(),
})
),
async (c) => {
// const id = c.req.param("id");
const { id } = c.req.valid("param");
const [data] = await db.select().from(users).where(eq(users.id, id));
if (!data) {
return c.json({ error: "User not found" }, 404);
}
// if (data.length === 0) {
// return c.json({ error: "User not found" }, 404);
// }
return c.json({
user: data,
});
}
);
export default app;
Discussion