fix(CORE-001D3A): accept decimal boundary inputs

This commit is contained in:
Bu5hm4nn
2026-03-26 13:19:18 +01:00
parent bb557009c7
commit 753e9d3146
6 changed files with 105 additions and 69 deletions

View File

@@ -0,0 +1,25 @@
from __future__ import annotations
from decimal import Decimal, InvalidOperation
from app.domain.units import decimal_from_float, to_decimal
def boundary_decimal(value: object, *, field_name: str) -> Decimal:
if value is None:
raise ValueError(f"{field_name} must be present")
if isinstance(value, bool):
raise TypeError(f"{field_name} must be numeric, got bool")
if isinstance(value, float):
return decimal_from_float(value)
if isinstance(value, (Decimal, int)):
return to_decimal(value)
if isinstance(value, str):
stripped = value.strip()
if not stripped:
raise ValueError(f"{field_name} must be present")
try:
return to_decimal(stripped)
except InvalidOperation as exc:
raise ValueError(f"{field_name} must be numeric, got {value!r}") from exc
raise TypeError(f"{field_name} must be numeric, got {type(value)!r}")