summaryrefslogtreecommitdiff
path: root/lib/items/validations.ts
blob: 14fc27b109b5d23baa67468e32e0f483fa3abd98 (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
import {
  createSearchParamsCache,
  parseAsArrayOf,
  parseAsInteger,
  parseAsString,
  parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"

import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { Item } from "@/db/schema/items";

export const searchParamsCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
    []
  ),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<Item>().withDefault([
    { id: "createdAt", desc: true },
  ]),
  itemCode: parseAsString.withDefault(""),
  itemName: parseAsString.withDefault(""),
  description: parseAsString.withDefault(""),
  parentItemCode: parseAsString.withDefault(""),
  itemLevel: parseAsInteger.withDefault(5),
  deleteFlag: parseAsString.withDefault(""),
  unitOfMeasure: parseAsString.withDefault(""),
  steelType: parseAsString.withDefault(""),
  gradeMaterial: parseAsString.withDefault(""),
  changeDate: parseAsString.withDefault(""),
  baseUnitOfMeasure: parseAsString.withDefault(""),

  // advanced filter
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),

})

export const createItemSchema = z.object({
  itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
  itemName: z.string().min(1, "아이템명은 필수입니다"),
  description: z.string().nullable().optional(),
  parentItemCode: z.string().max(18).nullable().optional(),
  itemLevel: z.number().int().min(1).max(5).nullable().optional(),
  deleteFlag: z.string().max(1).nullable().optional(),
  unitOfMeasure: z.string().max(3).nullable().optional(),
  steelType: z.string().max(2).nullable().optional(),
  gradeMaterial: z.string().max(50).nullable().optional(),
  changeDate: z.string().max(8).nullable().optional(),
  baseUnitOfMeasure: z.string().max(3).nullable().optional(),
})

export const updateItemSchema = z.object({
  itemCode: z.string().optional(),
  itemName: z.string().optional(),
  description: z.string().optional(),
  parentItemCode: z.string().max(18).nullable().optional(),
  itemLevel: z.number().int().min(1).max(5).nullable().optional(),
  deleteFlag: z.string().max(1).nullable().optional(),
  unitOfMeasure: z.string().max(3).nullable().optional(),
  steelType: z.string().max(2).nullable().optional(),
  gradeMaterial: z.string().max(50).nullable().optional(),
  changeDate: z.string().max(8).nullable().optional(),
  baseUnitOfMeasure: z.string().max(3).nullable().optional(),
})

export type GetItemsSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
export type CreateItemSchema = z.infer<typeof createItemSchema>
export type UpdateItemSchema = z.infer<typeof updateItemSchema>