Skip to content

Commit

Permalink
zodでのクライアントバリデーション
Browse files Browse the repository at this point in the history
  • Loading branch information
Inlet-back committed Dec 6, 2024
1 parent c39be6e commit 475165d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions task_yell/src/app/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default function Home() {
newEvent: Event,
notification: { date: Date | null; type: "call" | "push" },
) => {
console.log("addEvent",newEvent);
setEvents([...events, newEvent]);
setIsEventModalOpen(false);
setRemovedStickyNote(null);
Expand Down
21 changes: 21 additions & 0 deletions task_yell/src/components/event-creator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { z } from "zod";
import { DateTimeInput } from "@/components/date-time-input";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
Expand Down Expand Up @@ -64,6 +65,20 @@ export function EventCreator({
const [notificationType, setNotificationType] = useState<"call" | "push">(
"call",
);
// Zod スキーマの定義
const eventSchema = z.object({
title: z.string().nonempty("タイトルは空白にできません。"),
start: z.date(),
end: z.date(),
description: z.string().optional(),
category: z.string().optional(),
priority: z.string().optional(),
location: z.string().optional(),
invitees: z.array(z.string()).optional(),
isTask: z.boolean(),
isLocked: z.boolean(),
});


const handleSave = () => {
if (targetDate) {
Expand All @@ -80,6 +95,12 @@ export function EventCreator({
isTask,
isLocked,
};
// バリデーションの実行
const result = eventSchema.safeParse(newEvent);
if (!result.success) {
alert(result.error.errors.map((err) => err.message).join("\n"));
return;
}
onSave(newEvent, { date: notificationDate, type: notificationType });
}
};
Expand Down

0 comments on commit 475165d

Please sign in to comment.