Skip to content

Commit fe6635b

Browse files
committed
fix(transfer): don't coerce malformed falsy asset values to XLM
Addresses CodeRabbit review on #50: the asset default validator only covers None and "" now, so False/0 correctly fail type validation instead of being silently normalized to "XLM".
1 parent fca00e2 commit fe6635b

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/shade/models/transfer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ class Transfer(ShadeObject):
4949

5050
@field_validator("asset", mode="before")
5151
@classmethod
52-
def _default_asset(cls, value: Optional[str]) -> str:
52+
def _default_asset(cls, value: object) -> object:
5353
# Covers both a missing key (pydantic would already default it) and an
54-
# API response that sends the key with an explicit null/empty value.
55-
return value if value else "XLM"
54+
# API response that sends the key as an explicit null/empty string.
55+
# Other falsy-but-wrong types (e.g. False, 0) are left alone so
56+
# pydantic's normal type validation rejects them.
57+
if value is None or value == "":
58+
return "XLM"
59+
return value
5660

5761
@field_validator("source_address", "destination_address")
5862
@classmethod

tests/test_transfer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def test_asset_defaults_to_xlm_when_null():
6060
assert transfer.asset == "XLM"
6161

6262

63+
def test_malformed_asset_is_rejected_not_defaulted():
64+
# False/0 are falsy but must not be silently coerced to "XLM" — they are
65+
# the wrong type and should surface as a validation error instead.
66+
with pytest.raises(InvalidRequestError) as exc_info:
67+
Transfer.from_dict(_api_response(asset=False))
68+
assert exc_info.value.param == "asset"
69+
70+
6371
def test_status_is_transfer_status_enum():
6472
for raw in ("pending", "processing", "completed", "failed"):
6573
transfer = Transfer.from_dict(_api_response(status=raw))

0 commit comments

Comments
 (0)