refactor: enforce enum machine codes #18
Frontend verify / commits (push) Skipped
Frontend verify / security (push) Successful in 11s
Frontend verify / verify (push) Successful in 9m31s

Use owner enums for known codes; preserve wire values and unknown fallbacks.

Verified npm ci and verify: 379 tests, 5 lint checks, app and Storybook builds.
This commit was merged in pull request #41.
This commit is contained in:
2026-09-28 16:36:40 +03:00
committed by ksv741
parent 12c3b72bfb
commit 5b69003516
26 changed files with 248 additions and 79 deletions
@@ -27,6 +27,9 @@ import type {
OperationValues,
} from "./OperationForm.types";
// ZodIssueCode is deprecated; this is the custom refinement code accepted by Zod.
enum FormIssueCode { Custom = "custom" }
const MAX_DESCRIPTION_LENGTH = 500;
const operationSchema = z.object({
@@ -49,13 +52,13 @@ function validateRefund(values: OperationValues, context: z.RefinementCtx,
expense: BankOperation | undefined, limit: BankOperation["availableRefund"] | undefined) {
if (!expense || expense.cardId !== values.cardId)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["originalExpenseId"],
...validationMessage("bank.validation.required")(undefined),
});
if (expense && values.date < expense.date)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["date"],
...validationMessage("bank.validation.start")(undefined),
});
@@ -66,7 +69,7 @@ function validateRefund(values: OperationValues, context: z.RefinementCtx,
&& amount > (moneyCents(limit.value) ?? 0n)
)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["amount"],
...validationMessage("bank.validation.refund")(undefined),
});
@@ -116,7 +119,7 @@ const useOperationFormVm = (props: OperationFormProps) => {
const target = props.cards.find(c => c.id === values.targetCardId);
if (!source || source.archivedAt)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["cardId"],
...validationMessage("bank.validation.required")(undefined),
});
@@ -125,7 +128,7 @@ const useOperationFormVm = (props: OperationFormProps) => {
&& (!target || target.archivedAt || target.id === values.cardId)
)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["targetCardId"],
...validationMessage("bank.validation.destination")(undefined),
});
@@ -136,7 +139,7 @@ const useOperationFormVm = (props: OperationFormProps) => {
&& values.date < target.openingDate)
)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["date"],
...validationMessage("bank.validation.start")(undefined),
});
@@ -152,7 +155,7 @@ const useOperationFormVm = (props: OperationFormProps) => {
)
)
context.addIssue({
code: "custom",
code: FormIssueCode.Custom,
path: ["categoryId"],
...validationMessage("bank.validation.required")(undefined),
});