admin/validation/withdrawal.ts
alisaza 76e65f4f79 refactor: improve code readability and validation structure
- Added a new line for better readability in the BankAccountsPage component when showing alert messages.
- Refactored the test case for isRequestHostAllowed to enhance clarity.
- Improved formatting in the CreateBankAccountFormValidation schema for the nationalCode field.
2026-09-05 16:31:02 +03:30

51 lines
1.6 KiB
TypeScript

import { z } from 'zod'
import { convertPersianToEnglish } from '@/helpers'
import { texts } from '@/texts'
const v = texts.validation.withdrawal
const nationalId = texts.validation.nationalId
const toEnglishDigits = (value: string) => convertPersianToEnglish(value).trim()
export const CreateBankAccountFormValidation = (requiresNationalCode: boolean) =>
z
.object({
iban: z
.string()
.trim()
.transform((value) => toEnglishDigits(value).toUpperCase().replace(/\s+/g, ''))
.refine((value) => /^IR\d{24}$/.test(value), v.ibanInvalid),
nationalCode: z
.string()
.trim()
.transform((value) => toEnglishDigits(value)),
})
.superRefine((value, ctx) => {
if (!requiresNationalCode) return
if (!value.nationalCode) {
ctx.addIssue({ code: 'custom', path: ['nationalCode'], message: nationalId.nationalCodeRequired })
return
}
if (!/^\d{10}$/.test(value.nationalCode)) {
ctx.addIssue({ code: 'custom', path: ['nationalCode'], message: nationalId.nationalCodeDigits })
}
})
export type CreateBankAccountFormValues = z.infer<ReturnType<typeof CreateBankAccountFormValidation>>
export const CreateWithdrawalRequestFormValidation = z.object({
bankAccountId: z.string().min(1, v.bankAccountRequired),
amount: z
.string()
.trim()
.min(1, v.amountRequired)
.transform((value) => toEnglishDigits(value))
.refine((value) => /^\d+$/.test(value), v.amountInteger)
.refine((value) => Number(value) > 0, v.amountPositive),
})
export type CreateWithdrawalRequestFormValues = z.infer<typeof CreateWithdrawalRequestFormValidation>