-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo-schema.ts
More file actions
83 lines (71 loc) · 2.19 KB
/
Copy pathtodo-schema.ts
File metadata and controls
83 lines (71 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { TODO_ICON_VALUES } from "@repo/timo-design-system/ui";
import { z } from "zod";
import { apiResponseSchema } from "@/api/schema/response";
export const todoIconSchema = z.enum(TODO_ICON_VALUES);
export const todoPrioritySchema = z.enum([
"VERY_HIGH",
"HIGH",
"MEDIUM",
"LOW",
]);
export const todoRepeatTypeSchema = z.enum([
"NONE",
"DAILY",
"WEEKLY",
"MONTHLY",
]);
export const todoRepeatWeekdaySchema = z.enum([
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT",
"SUN",
]);
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
const DURATION_PATTERN = /^\d{1,3}:[0-5]\d$/;
export const createTodoRequestSchema = z
.object({
icon: todoIconSchema.nullable(),
title: z.string().trim().min(1),
subtasks: z.array(z.string().trim().min(1)).nullable(),
date: z.string().regex(DATE_PATTERN),
duration: z.string().regex(DURATION_PATTERN),
priority: todoPrioritySchema.nullable(),
tagId: z.number().int().positive().nullable(),
repeatType: todoRepeatTypeSchema,
repeatWeekdays: z.array(todoRepeatWeekdaySchema).nullable(),
repeatDayOfMonth: z.number().int().min(1).max(31).nullable(),
memo: z.string().nullable(),
})
.superRefine((value, ctx) => {
if (
value.repeatType === "WEEKLY" &&
(!value.repeatWeekdays || value.repeatWeekdays.length === 0)
) {
ctx.addIssue({
code: "custom",
path: ["repeatWeekdays"],
message: "반복할 요일을 선택해주세요.",
});
}
if (value.repeatType === "MONTHLY" && !value.repeatDayOfMonth) {
ctx.addIssue({
code: "custom",
path: ["repeatDayOfMonth"],
message: "반복할 날짜를 선택해주세요.",
});
}
});
export type CreateTodoRequest = z.infer<typeof createTodoRequestSchema>;
export type TodoIcon = z.infer<typeof todoIconSchema>;
export type TodoPriority = z.infer<typeof todoPrioritySchema>;
export type TodoRepeatType = z.infer<typeof todoRepeatTypeSchema>;
export type TodoRepeatWeekday = z.infer<typeof todoRepeatWeekdaySchema>;
export const createTodoResponseSchema = apiResponseSchema(
z.object({
todoId: z.number(),
}),
);
export type CreateTodoResponse = z.infer<typeof createTodoResponseSchema>;