{"slug": "reject-nonfinite-2captcha-balance-values-before-storing-them", "title": "Reject Nonfinite 2Captcha Balance Values Before Storing Them", "summary": "A developer demonstrated a small validation step for a 2Captcha account adapter that rejects nonfinite balance values before they reach a dashboard database. The Python example uses Decimal parsing plus an is_finite() check to reject NaN, Infinity, -Infinity and malformed strings, while a separate zero check guards against mapping invalid or empty input to a valid amount. The fixture, built on synthetic data, reports four malformed balance values rejected and recommends running it through the same import path as an ordinary row to confirm it does not overwrite the previous balance.", "body_md": "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.\n\n*Written with AI assistance. The Python example uses synthetic data and was checked locally.*\n\nUsing 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.\n\nThe 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.\n\n``` python\nfrom decimal import Decimal, InvalidOperation\n\ndef balance(raw):\n    try:\n        value = Decimal(raw)\n    except InvalidOperation as error:\n        raise ValueError(\"invalid decimal balance\") from error\n    if not value.is_finite():\n        raise ValueError(\"nonfinite balance\")\n    return value\n\nassert balance(\"0\") == Decimal(\"0\")\nassert balance(\"0.0001\") == Decimal(\"0.0001\")\nrejected = 0\nfor raw in (\"NaN\", \"Infinity\", \"-Infinity\", \"bad\"):\n    try:\n        balance(raw)\n    except ValueError:\n        rejected += 1\nassert rejected == 4\nprint(\"Four malformed balance values rejected\")\n```\n\nThe 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.\n\nIn 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.", "url": "https://wpnews.pro/news/reject-nonfinite-2captcha-balance-values-before-storing-them", "canonical_source": "https://dev.to/gresiuseo/reject-nonfinite-2captcha-balance-values-before-storing-them-o0p", "published_at": "2026-11-23 09:37:53+00:00", "updated_at": "2026-11-23 09:42:15.382898+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["2Captcha", "Python"], "also_reported_by": [], "takeaway": "Validate parsed monetary values with Decimal.is_finite() before range comparisons, since Decimal still accepts NaN and Infinity that can poison sums, chart ranges and alert thresholds.", "recommendation": {"advice": "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.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/reject-nonfinite-2captcha-balance-values-before-storing-them", "markdown": "https://wpnews.pro/news/reject-nonfinite-2captcha-balance-values-before-storing-them.md", "text": "https://wpnews.pro/news/reject-nonfinite-2captcha-balance-values-before-storing-them.txt", "jsonld": "https://wpnews.pro/news/reject-nonfinite-2captcha-balance-values-before-storing-them.jsonld"}}