if you're facing the same issue, here is a workout that works for me:
image: z
.union([
z
.instanceof(File, { message: "Image is required" })
.refine(
(file) => !file || file.size !== 0 || file.size <= 5000000,
`Max image size is ${5000000}MB`
)
.refine(
(file) =>
!file ||
file.type === "" ||
["image/jpeg", "image/png", "image/jpg"].includes(file.type),
"Only .jpg, .jpeg, and .png formats are supported"
),
z.string().optional(), // Allow the existing image URL for editing mode
])
.refine((value) => value instanceof File || typeof value === "string", {
message: "Image is required",
}),