From 695330375f44c2c3016a06588a3355938da439ac Mon Sep 17 00:00:00 2001 From: David Date: Wed, 23 Sep 2026 15:27:43 +0200 Subject: [PATCH] Add validation for non-positive values --- src/model_router/models.py | 8 ++++++++ tests/test_models.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/model_router/models.py b/src/model_router/models.py index 0ff700c..8a3e926 100644 --- a/src/model_router/models.py +++ b/src/model_router/models.py @@ -36,3 +36,11 @@ def from_openrouter(cls, raw): class Limits: output_tokens: int = 1024 max_cost_usd: float | None = None + + def __post_init__(self): + if self.output_tokens < 1: + raise ValueError(f"'output_tokens' expected positive integer, got '{self.output_tokens}'") + if self.max_cost_usd is None: + pass + elif self.max_cost_usd < 0: + raise ValueError(f"'max_cost_usd' expected positive integer, got '{self.max_cost_usd}'") diff --git a/tests/test_models.py b/tests/test_models.py index f415f38..b104f3d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -60,6 +60,14 @@ def test_is_immutable(self): with self.assertRaises(dataclasses.FrozenInstanceError): Limits().output_tokens = 5 + def test_not_negative_output_tokens(self): + with self.assertRaises(ValueError): + Limits(output_tokens=0, max_cost_usd=1) + + def test_not_negative_max_cost_usd(self): + with self.assertRaises(ValueError): + Limits(output_tokens=1024, max_cost_usd=-1) + if __name__ == "__main__": unittest.main()