fix: enforce transfer invariants when updating a transaction (#3)

The transfer rules — destination required, destination different from the
source account, no category, and no destination on a non-transfer — were
enforced by TransactionBase's validator on create, but the PATCH route only
re-checked category-vs-type.

So `PATCH /api/transactions/{id} {"type": "transfer"}` on an ordinary
expense committed a transfer row with transfer_account_id = NULL, and only
then failed serializing the response (TransactionRead inherits the same
validator). The caller got a 500 and would reasonably assume nothing was
saved — but from that point on every `GET /api/transactions` failed the same
validation, so the transactions list, the app's main screen, returned 500
permanently, with no way left in the UI to delete the offending row.

The rules now live in one place, transfer_rule_violation(), used by both the
create schema and the update route, so the two paths can't drift apart
again. The update path validates the row as it would look after the patch
rather than only the fields sent, since switching type alone invalidates
fields that were left untouched.

The frontend already sends a complete payload on edit (explicit nulls for
category_id / transfer_account_id) and does these same checks client-side,
so this only closes the hole for direct API calls — no UI change needed.


Claude-Session: https://claude.ai/code/session_01UmuMDFrwdvQkce885VaaTh

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
technosexual
2026-08-30 02:42:37 +03:00
committed by GitHub
parent c95eb12d57
commit 949d2df37f
3 changed files with 118 additions and 8 deletions

View File

@@ -10,6 +10,35 @@ from app.schemas.category import CategoryRead
from app.schemas.tag import TagRead
def transfer_rule_violation(
*,
type: TransactionType,
account_id: int,
transfer_account_id: int | None,
category_id: int | None,
) -> str | None:
"""The transfer invariants in one place, returning the problem as text or
None if the combination is valid.
Both halves of the write path go through this: the create schema below
(as a pydantic validator) and the PATCH route (see
routes/transactions.py). Enforcing it on create only used to let an edit
produce a row that create would have rejected — most damagingly a
transfer with no transfer_account_id, which TransactionRead itself can't
serialize, so the row broke every later read of the transactions list.
"""
if type == TransactionType.TRANSFER:
if not transfer_account_id:
return "transfer_account_id is required for transfer transactions"
if transfer_account_id == account_id:
return "transfer_account_id must differ from account_id"
if category_id:
return "category_id is not valid for transfer transactions"
elif transfer_account_id:
return "transfer_account_id is only valid for transfer transactions"
return None
class TransactionBase(BaseModel):
account_id: int
category_id: int | None = None
@@ -30,14 +59,14 @@ class TransactionBase(BaseModel):
@model_validator(mode="after")
def _validate_type_specific_fields(self) -> "TransactionBase":
if self.type == TransactionType.TRANSFER and not self.transfer_account_id:
raise ValueError("transfer_account_id is required for transfer transactions")
if self.type == TransactionType.TRANSFER and self.transfer_account_id == self.account_id:
raise ValueError("transfer_account_id must differ from account_id")
if self.type != TransactionType.TRANSFER and self.transfer_account_id:
raise ValueError("transfer_account_id is only valid for transfer transactions")
if self.type == TransactionType.TRANSFER and self.category_id:
raise ValueError("category_id is not valid for transfer transactions")
violation = transfer_rule_violation(
type=self.type,
account_id=self.account_id,
transfer_account_id=self.transfer_account_id,
category_id=self.category_id,
)
if violation:
raise ValueError(violation)
return self