70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { z } from "zod";
|
|
import { EYE_COLORS } from "./types";
|
|
|
|
const MAX_FILE_SIZE = 5 * 1024 * 1024;
|
|
const ACCEPTED_IMAGE_TYPES = ["image/jpeg", "image/png", "image/webp"];
|
|
|
|
const imageFileSchema = z
|
|
.instanceof(File, { error: "A file is required" })
|
|
.refine((file) => file.size > 0, "A file is required")
|
|
.refine((file) => file.size <= MAX_FILE_SIZE, "Image must be smaller than 5MB")
|
|
.refine((file) => ACCEPTED_IMAGE_TYPES.includes(file.type), "Only JPEG, PNG, or WEBP images are allowed");
|
|
|
|
const baseFields = {
|
|
height: z.coerce
|
|
.number({ error: "Height is required" })
|
|
.int("Height must be a whole number")
|
|
.min(60, "Height must be at least 60 cm")
|
|
.max(240, "Height must be at most 240 cm"),
|
|
eyeColor: z.enum(EYE_COLORS, { error: "Eye colour is required" }),
|
|
isEuResident: z.enum(["yes", "no"]),
|
|
regionId: z.string().optional(),
|
|
city: z.string().trim().optional(),
|
|
street: z.string().trim().optional(),
|
|
customAddress: z.string().trim().optional(),
|
|
profile: imageFileSchema,
|
|
signature: imageFileSchema,
|
|
};
|
|
|
|
const previousCitizenshipSchema = z
|
|
.object({
|
|
applicationType: z.literal("PREVIOUS_CITIZENSHIP"),
|
|
previousCountry: z.enum(["DUS", "REA"], { error: "Invalid country of previous citizenship" }),
|
|
previousRegion: z.string().trim().min(1, "Region of previous citizenship is required"),
|
|
...baseFields,
|
|
})
|
|
.superRefine(validateAddress);
|
|
|
|
const naturalizationSchema = z
|
|
.object({
|
|
applicationType: z.literal("NATURALIZATION"),
|
|
naturalizationReason: z.string().trim().min(1, "Reason for naturalisation is required"),
|
|
...baseFields,
|
|
})
|
|
.superRefine(validateAddress);
|
|
|
|
function validateAddress(
|
|
data: {
|
|
isEuResident: string;
|
|
regionId?: string;
|
|
city?: string;
|
|
street?: string;
|
|
customAddress?: string;
|
|
},
|
|
ctx: z.RefinementCtx
|
|
) {
|
|
if (data.isEuResident === "yes") {
|
|
if (!data.regionId) ctx.addIssue({ code: "custom", path: ["regionId"], message: "Region is required" });
|
|
if (!data.city) ctx.addIssue({ code: "custom", path: ["city"], message: "City is required" });
|
|
if (!data.street) ctx.addIssue({ code: "custom", path: ["street"], message: "Street is required" });
|
|
} else {
|
|
if (!data.customAddress) {
|
|
ctx.addIssue({ code: "custom", path: ["customAddress"], message: "Address is required" });
|
|
}
|
|
}
|
|
}
|
|
|
|
export const citizenshipApplicationSchema = z.discriminatedUnion("applicationType", [
|
|
previousCitizenshipSchema,
|
|
naturalizationSchema,
|
|
]); |