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

import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
import { VendorTypes } from "@/db/schema"

export const searchParamsCache = createSearchParamsCache({
  flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
    []
  ),
  page: parseAsInteger.withDefault(1),
  perPage: parseAsInteger.withDefault(10),
  sort: getSortingStateParser<VendorTypes>().withDefault([
    { id: "createdAt", desc: true },
  ]),
  nameKo: parseAsString.withDefault(""),
  nameEn: parseAsString.withDefault(""),
  from: parseAsString.withDefault(""),
  to: parseAsString.withDefault(""),
  // advanced filter
  filters: getFiltersStateParser().withDefault([]),
  joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
  search: parseAsString.withDefault(""),

})

export const createVendorTypeSchema = z.object({
  nameKo: z.string(),
  nameEn: z.string(),

})

export const updateVendorTypeSchema = z.object({
  nameKo: z.string().optional(),
  nameEn: z.string().optional(),
})

export type GetVendorTypesSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
export type CreateVendorTypeSchema = z.infer<typeof createVendorTypeSchema>
export type UpdateVendorTypeSchema = z.infer<typeof updateVendorTypeSchema>