summaryrefslogtreecommitdiff
path: root/lib/notice/validations.ts
blob: 05e84af978940141eb0bafa06b83a0c4ccf91a95 (plain)
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
import { z } from "zod"
import {
  createSearchParamsCache,
  parseAsArrayOf,
  parseAsInteger,
  parseAsString,
  parseAsStringEnum,
  parseAsBoolean,
} from "nuqs/server"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { Notice } from "@/db/schema/notice"

// 공지사항 생성 스키마
export const createNoticeSchema = z.object({
  pagePath: z.string().min(1, "페이지 경로를 입력해주세요"),
  title: z.string().min(1, "제목을 입력해주세요"),
  content: z.string().min(1, "내용을 입력해주세요"),
  authorId: z.number().min(1, "작성자를 선택해주세요"),
  isActive: z.boolean().default(true),
})

// 공지사항 수정 스키마
export const updateNoticeSchema = z.object({
  id: z.number(),
  pagePath: z.string().min(1, "페이지 경로를 입력해주세요"),
  title: z.string().min(1, "제목을 입력해주세요"),
  content: z.string().min(1, "내용을 입력해주세요"),
  isActive: z.boolean().default(true),
})

// 현대적인 검색 파라미터 캐시
export const searchParamsNoticeCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<Notice>().withDefault([
    { id: "createdAt", desc: true },
  ]),

  // 기본 검색 필드들
  pagePath: parseAsString.withDefault(""),
  title: parseAsString.withDefault(""),
  content: parseAsString.withDefault(""),
  authorId: parseAsInteger,
  isActive: parseAsBoolean,
  
  // 고급 필터
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),
  
  // 날짜 범위
  from: parseAsString.withDefault(""),
  to: parseAsString.withDefault(""),
})

// 타입 추출
export type CreateNoticeSchema = z.infer<typeof createNoticeSchema>
export type UpdateNoticeSchema = z.infer<typeof updateNoticeSchema>
export type GetNoticeSchema = Awaited<ReturnType<typeof searchParamsNoticeCache.parse>>

// 기존 스키마 (하위 호환성을 위해 유지)
export const getNoticeSchema = z.object({
  page: z.coerce.number().default(1),
  per_page: z.coerce.number().default(10),
  sort: z.string().optional(),
  pagePath: z.string().optional(),
  title: z.string().optional(),
  authorId: z.coerce.number().optional(),
  isActive: z.coerce.boolean().optional(),
  from: z.string().optional(),
  to: z.string().optional(),
})

// 페이지 경로별 공지사항 조회 스키마
export const getPageNoticeSchema = z.object({
  pagePath: z.string().min(1, "페이지 경로를 입력해주세요"),
})

export type GetPageNoticeSchema = z.infer<typeof getPageNoticeSchema>