Skip to content

Commit 475165d

Browse files
committed
zodでのクライアントバリデーション
1 parent c39be6e commit 475165d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

task_yell/src/app/home/page.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export default function Home() {
157157
newEvent: Event,
158158
notification: { date: Date | null; type: "call" | "push" },
159159
) => {
160+
console.log("addEvent",newEvent);
160161
setEvents([...events, newEvent]);
161162
setIsEventModalOpen(false);
162163
setRemovedStickyNote(null);

task_yell/src/components/event-creator.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { z } from "zod";
34
import { DateTimeInput } from "@/components/date-time-input";
45
import { Button } from "@/components/ui/button";
56
import { Checkbox } from "@/components/ui/checkbox";
@@ -64,6 +65,20 @@ export function EventCreator({
6465
const [notificationType, setNotificationType] = useState<"call" | "push">(
6566
"call",
6667
);
68+
// Zod スキーマの定義
69+
const eventSchema = z.object({
70+
title: z.string().nonempty("タイトルは空白にできません。"),
71+
start: z.date(),
72+
end: z.date(),
73+
description: z.string().optional(),
74+
category: z.string().optional(),
75+
priority: z.string().optional(),
76+
location: z.string().optional(),
77+
invitees: z.array(z.string()).optional(),
78+
isTask: z.boolean(),
79+
isLocked: z.boolean(),
80+
});
81+
6782

6883
const handleSave = () => {
6984
if (targetDate) {
@@ -80,6 +95,12 @@ export function EventCreator({
8095
isTask,
8196
isLocked,
8297
};
98+
// バリデーションの実行
99+
const result = eventSchema.safeParse(newEvent);
100+
if (!result.success) {
101+
alert(result.error.errors.map((err) => err.message).join("\n"));
102+
return;
103+
}
83104
onSave(newEvent, { date: notificationDate, type: notificationType });
84105
}
85106
};

0 commit comments

Comments
 (0)