refactor: enforce enum machine codes #18
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:
@@ -11,7 +11,7 @@ import {
|
||||
useBankOperations,
|
||||
useBankSummary,
|
||||
} from "@/entities/bank-card";
|
||||
import { apiErrorCode } from "@/shared/api";
|
||||
import { ApiErrorCode, apiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
import { formatDate, formatMoney } from "@/shared/lib";
|
||||
|
||||
const PAGE_SIZES = new Set(["5", "10", "20", "50", "100", "all"]);
|
||||
@@ -81,7 +81,7 @@ function useCardPageVm() {
|
||||
deleting,
|
||||
editingCard,
|
||||
history,
|
||||
notFound: apiErrorCode(card.error) === "BANK_NOT_FOUND",
|
||||
notFound: isApiErrorCode(apiErrorCode(card.error), ApiErrorCode.BankNotFound),
|
||||
operation,
|
||||
page,
|
||||
pageSize,
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user