33 lines
815 B
TypeScript
33 lines
815 B
TypeScript
import { z } from "zod";
|
|
|
|
import { validationMessage } from "@/shared/i18n";
|
|
import { moscowDate } from "@/shared/lib";
|
|
|
|
import { parseDraftDate } from "./period";
|
|
|
|
const draftDate = z
|
|
.string()
|
|
.refine(value => parseDraftDate(value) !== null, {
|
|
error: validationMessage("income.validation.date"),
|
|
})
|
|
.refine(
|
|
(value) => {
|
|
const date = parseDraftDate(value);
|
|
|
|
return !date || date <= moscowDate(new Date());
|
|
},
|
|
{ error: validationMessage("income.validation.future") },
|
|
);
|
|
|
|
const incomeFormSchema = z.object({ from: draftDate, to: draftDate }).refine(
|
|
({ from, to }) => {
|
|
const start = parseDraftDate(from);
|
|
const end = parseDraftDate(to);
|
|
|
|
return !start || !end || start <= end;
|
|
},
|
|
{ path: ["to"], ...validationMessage("income.validation.period")({}) },
|
|
);
|
|
|
|
export { incomeFormSchema };
|