33 lines
829 B
TypeScript
33 lines
829 B
TypeScript
import { z } from "zod";
|
|
|
|
import { validationMessage } from "@/shared/i18n";
|
|
|
|
import type { ExpenseCategory } from "../api/types";
|
|
const MAX_CATEGORY_NAME_LENGTH = 80;
|
|
|
|
type CategoryValues = z.infer<ReturnType<typeof categorySchema>>;
|
|
|
|
function categorySchema(categories?: ExpenseCategory[], editingId?: string) {
|
|
return z.object({
|
|
name: z
|
|
.string()
|
|
.trim()
|
|
.refine(
|
|
value => value.length > 0 && value.length <= MAX_CATEGORY_NAME_LENGTH,
|
|
{ error: validationMessage("bank.validation.category") },
|
|
)
|
|
.refine(
|
|
value =>
|
|
!categories?.some(
|
|
c =>
|
|
c.id !== editingId
|
|
&& c.name.toLocaleLowerCase("ru-RU")
|
|
=== value.toLocaleLowerCase("ru-RU"),
|
|
),
|
|
{ error: validationMessage("bank.validation.duplicate") },
|
|
),
|
|
});
|
|
}
|
|
|
|
export { categorySchema, type CategoryValues };
|