From 9af8d789e99603b33b6f734268c038a0a7fa38c5 Mon Sep 17 00:00:00 2001 From: Dustin Do Date: Thu, 8 Aug 2024 12:39:24 +0700 Subject: [PATCH] fix(validation): allow input negative wallet amount (#219) --- apps/mobile/components/wallet/account-form.tsx | 10 +++++----- apps/mobile/mutations/wallet.ts | 6 +++--- packages/validation/src/wallet.zod.ts | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/mobile/components/wallet/account-form.tsx b/apps/mobile/components/wallet/account-form.tsx index 3b6c5d98..44e70cde 100644 --- a/apps/mobile/components/wallet/account-form.tsx +++ b/apps/mobile/components/wallet/account-form.tsx @@ -1,5 +1,5 @@ import { useDefaultCurrency } from '@/stores/user-settings/hooks' -import { type AccountFormValues, zAccountFormValues } from '@6pm/validation' +import { type WalletFormValues, zWalletFormValues } from '@6pm/validation' import { zodResolver } from '@hookform/resolvers/zod' import { t } from '@lingui/macro' import { useLingui } from '@lingui/react' @@ -14,8 +14,8 @@ import { Text } from '../ui/text' import { SelectAccountIconField } from './select-account-icon-field' type AccountFormProps = { - onSubmit: (data: AccountFormValues) => void - defaultValues?: AccountFormValues + onSubmit: (data: WalletFormValues) => void + defaultValues?: WalletFormValues } export const AccountForm = ({ onSubmit, defaultValues }: AccountFormProps) => { @@ -24,8 +24,8 @@ export const AccountForm = ({ onSubmit, defaultValues }: AccountFormProps) => { const balanceInputRef = useRef(null) const defaultCurrency = useDefaultCurrency() - const accountForm = useForm({ - resolver: zodResolver(zAccountFormValues), + const accountForm = useForm({ + resolver: zodResolver(zWalletFormValues), defaultValues: { name: '', preferredCurrency: defaultCurrency, diff --git a/apps/mobile/mutations/wallet.ts b/apps/mobile/mutations/wallet.ts index 02ca7060..f2289462 100644 --- a/apps/mobile/mutations/wallet.ts +++ b/apps/mobile/mutations/wallet.ts @@ -1,7 +1,7 @@ import { getHonoClient } from '@/lib/client' -import type { AccountFormValues } from '@6pm/validation' +import type { WalletFormValues } from '@6pm/validation' -export async function createWallet(data: AccountFormValues) { +export async function createWallet(data: WalletFormValues) { const { balance, ...walletData } = data const hc = await getHonoClient() const result = await hc.v1.wallets.wallets.$post({ @@ -27,7 +27,7 @@ export async function updateWallet({ data, }: { id: string - data: AccountFormValues + data: WalletFormValues }) { const { balance, ...walletData } = data const hc = await getHonoClient() diff --git a/packages/validation/src/wallet.zod.ts b/packages/validation/src/wallet.zod.ts index c6e8f6a9..72e05f0d 100644 --- a/packages/validation/src/wallet.zod.ts +++ b/packages/validation/src/wallet.zod.ts @@ -19,10 +19,10 @@ export const zUpdateWallet = z.object({ }) export type UpdateWallet = z.infer -export const zAccountFormValues = zCreateWallet.extend({ - balance: z.number({ coerce: true }).positive().optional(), +export const zWalletFormValues = zCreateWallet.extend({ + balance: z.number({ coerce: true }).optional(), }) -export type AccountFormValues = z.infer +export type WalletFormValues = z.infer export const WalletAccountWithBalanceSchema = UserWalletAccountSchema.extend({ balance: z.number({ coerce: true }).optional(),