Skip to content

Commit

Permalink
fix: tx creation route validation
Browse files Browse the repository at this point in the history
  • Loading branch information
letehaha committed Sep 17, 2024
1 parent 911c9bd commit 69c05d7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/controllers/transactions.controller/create-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const bodyZodSchema = z
.optional(),
destinationAccountId: recordId().optional(),
destinationTransactionId: recordId().optional(),
categoryId: recordId(),
categoryId: z.union([recordId(), z.undefined()]),
transferNature: z.nativeEnum(TRANSACTION_TRANSFER_NATURE),
refundsTxId: recordId().optional(),
})
Expand Down Expand Up @@ -145,6 +145,21 @@ const bodyZodSchema = z
message: `For ${TRANSACTION_TRANSFER_NATURE.common_transfer} without "destinationTransactionId" - "destinationAccountId", and "destinationAmount" must be provided`,
path: ['destinationAccountId', 'destinationAmount', 'destinationTransactionId'],
},
)
.refine(
(data) => {
if (
data.transferNature === TRANSACTION_TRANSFER_NATURE.not_transfer &&
data.categoryId === undefined
) {
return false;
}
return true;
},
{
message: "'categoryId' is required for non-transfer transactions.",
path: ['categoryId', 'transferNature'],
},
);

export const createTransactionSchema = z.object({
Expand Down

0 comments on commit 69c05d7

Please sign in to comment.