summaryrefslogtreecommitdiff
path: root/lib/compliance/validations.ts
blob: 605270fa51841bbfaead592f492661eff31bea3c (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
import { complianceSurveyTemplates } from "@/db/schema/compliance"
import {
    createSearchParamsCache,
    parseAsArrayOf,
    parseAsInteger,
    parseAsString,
    parseAsStringEnum,
} from "nuqs/server"
import * as z from "zod"
import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"

export const searchParamsComplianceCache = createSearchParamsCache({
    // Common flags
    flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]),
    from: parseAsString.withDefault(""),
    to: parseAsString.withDefault(""),
    // Paging
    page: parseAsInteger.withDefault(1),
    perPage: parseAsInteger.withDefault(10),

    // Sorting
    sort: getSortingStateParser<typeof complianceSurveyTemplates.$inferSelect>().withDefault([
        { id: "createdAt", desc: true },
    ]),

    // Advanced filter
    filters: getFiltersStateParser<typeof complianceSurveyTemplates.$inferSelect>().withDefault([]),
    joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),

    // Global search
    search: parseAsString.withDefault(""),
})

// Export the type you can use in your server action:
export type GetComplianceSurveyTemplatesSchema = Awaited<ReturnType<typeof searchParamsComplianceCache.parse>>