# Reject Nonfinite 2Captcha Balance Values Before Storing Them

> Source: <https://dev.to/gresiuseo/reject-nonfinite-2captcha-balance-values-before-storing-them-o0p>
> Published: 2026-11-23 09:37:53+00:00

**Recommendation:** Add an is_finite() check after Decimal parsing and test malformed values through the same import path as valid rows to confirm they never overwrite the stored balance.
More in the playbook "Verifying model output": <https://wpnews.pro/playbooks/output-verification>

I’d place a small validation step between a local account adapter for [2Captcha](https://2captcha.com/) and the dashboard database. Once an amount is parsed, it often gets trusted downstream. A nonfinite value can be accepted by a general-purpose numeric parser and then poison a sum, a chart range, or the comparison that triggers an operational alert.

*Written with AI assistance. The Python example uses synthetic data and was checked locally.*

Using Decimal preserves useful monetary precision but it still accepts inputs such as NaN and Infinity. Check the parsed value with `is_finite()` before attempting a range comparison. The rule in this fixture accepts finite decimals only. A separate business rule may apply a bound or forbid negative balances, depending on what the internal field represents.

The example starts with a synthetic adapter value. It makes no claim about values returned by the live account API. In a pipeline I’d keep an invalid-row count and the adapter’s run identifier, then retain the time of the last valid balance shown in the dashboard.

``` python
from decimal import Decimal, InvalidOperation

def balance(raw):
    try:
        value = Decimal(raw)
    except InvalidOperation as error:
        raise ValueError("invalid decimal balance") from error
    if not value.is_finite():
        raise ValueError("nonfinite balance")
    return value

assert balance("0") == Decimal("0")
assert balance("0.0001") == Decimal("0.0001")
rejected = 0
for raw in ("NaN", "Infinity", "-Infinity", "bad"):
    try:
        balance(raw)
    except ValueError:
        rejected += 1
assert rejected == 4
print("Four malformed balance values rejected")
```

The fixture prints `Four malformed balance values rejected`. The zero check guards against a common mistake where invalid or empty input is mapped to zero, which itself is a valid amount. I’d keep a separate test for a missing field if that is possible in the adapter’s input.

In a real test suite, feed this fixture through the same import call as an ordinary row and assert that it does not replace the previous balance. This connects parser correctness to the behavior the dashboard owner actually sees.
