diff options
Diffstat (limited to 'lib/roles/validations.ts')
| -rw-r--r-- | lib/roles/validations.ts | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/roles/validations.ts b/lib/roles/validations.ts new file mode 100644 index 00000000..10cfe33b --- /dev/null +++ b/lib/roles/validations.ts @@ -0,0 +1,80 @@ +import { + createSearchParamsCache, + parseAsArrayOf, + parseAsInteger, + parseAsString, + parseAsStringEnum, +} from "nuqs/server" +import * as z from "zod" +import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers" +import { RoleView, users } from "@/db/schema/users"; + +export const searchParamsCache = createSearchParamsCache({ + flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault( + [] + ), + page: parseAsInteger.withDefault(1), + perPage: parseAsInteger.withDefault(10), + sort: getSortingStateParser<RoleView>().withDefault([ + { id: "created_at", desc: true }, + ]), + name: parseAsString.withDefault(""), + // advanced filter + filters: getFiltersStateParser().withDefault([]), + joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"), + search: parseAsString.withDefault(""), + + }) + + export const createRoleSchema = z.object({ + name: z.string().min(1), + description: z.string().min(1), + companyId:z + .number() + .int() + .positive() + .nullish(), // number | nullish + domain: z.enum(users.domain.enumValues), // "evcp" | "partners" + }); + + export const createRoleAssignmentSchema = z.object({ + evcpRoles:z.array(z.string()), + + }); + + + + export const updateRoleSchema = z.object({ + name: z.string().min(1), + description: z.string().min(1), + domain: z.enum(users.domain.enumValues), // "evcp" | "partners" + company_id: z + .number() + .int() + .positive() + .nullish(), // number | nullish + }).superRefine((data, ctx) => { + // domain이 partners 이면 companyId는 필수 + if (data.domain === "partners" && !data.company_id) { + ctx.addIssue({ + code: "custom", + path: ["company_id"], + message: "협력업체(domain=partners)일 경우 companyId는 필수입니다.", + }) + } + + // domain이 evcp 이면 companyId는 null이어야 한다면(정책상) + if (data.domain === "evcp" && data.company_id) { + ctx.addIssue({ + code: "custom", + path: ["company_id"], + message: "domain=evcp이면 companyId를 입력할 수 없습니다.", + }) + } + }) + +// TypeScript에서 사용할 타입 +export type GetRolesSchema = Awaited<ReturnType<typeof searchParamsCache.parse>> +export type CreateRoleSchema = z.infer<typeof createRoleSchema> +export type UpdateRoleSchema = z.infer<typeof updateRoleSchema> +export type CreateRoleAssignmentSchema = z.infer<typeof createRoleAssignmentSchema>
\ No newline at end of file |
