task-managment / src / lib / schemas / modifyTaskSchema.ts
modifyTaskSchema.ts
Raw
import { priorityEnumSchema, statusEnumSchema } from "@/database/schema";
import { formatDate } from "@/lib/utils";
import { z } from "zod";

export const modifyTaskSchema = z.object({
    id: z.number(),
    status: statusEnumSchema,
    userId:z.string(),
    priority: priorityEnumSchema,
    startDate: z.string().refine((str) => (str ? formatDate(str) : undefined)).optional(),
    expireDate: z.string().refine((str) => (str ? formatDate(str) : undefined)).optional(),
  }).refine(
    (data) => {
      const now = formatDate(new Date())!;
      if (data.startDate) {
        return formatDate(data.startDate)! >= now;
      }
      return true;
    },
    {
      message:
        "La fecha de inicio no puede ser inferior a la fecha actual",
      path: ["startDate"],
    }
  )
  .refine(
    (data) => {
      // Verificar si expireDate es proporcionado y es mayor o igual a startDate
      if (data.expireDate) {
        return formatDate(data.expireDate)! >= formatDate(data.startDate)!;
      }
      return true; // Si expireDate no está proporcionado, no aplicar la validación
    },
    {
      message:
        "La fecha de expiración debe ser igual o posterior a la fecha de inicio",
      path: ["expireDate"],
    }
  )