import { z, type ZodType } from 'zod';
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
function isValidIsoDate(value: string): boolean {
if (!ISO_DATE_REGEX.test(value)) return false;
const [yearPart, monthPart, dayPart] = value.split('-');
const year = Number(yearPart);
const month = Number(monthPart);
const day = Number(dayPart);
const date = new Date(Date.UTC(year, month - 1, day));
return (
date.getUTCFullYear() === year &&
date.getUTCMonth() === month - 1 &&
date.getUTCDate() === day
);
}
export const IsoDateSchema: ZodType<string> = z
.string()
.refine((value) => isValidIsoDate(value), {
error: 'Invalid date (YYYY-MM-DD)',
});
export const IsoDateTimeSchema: ZodType<string> = z.iso.datetime({
offset: true,
});