Skip to content

Commit e32dd96

Browse files
committed
Add some API parameter validation
Where it matters: when misuse would raise confusing errors. (Assisted by Claude Code; any errors are mine.)
1 parent ea516ef commit e32dd96

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/cjdk/_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ def cache_file(
331331
The check for SHA-1/SHA-256/SHA-512 hashes is only performed after a
332332
download; it is not performed if the file already exists in the cache.
333333
"""
334+
_conf.check_str("name", name)
335+
_conf.check_str("url", url, allow_empty=False)
336+
_conf.check_str("filename", filename, allow_empty=False)
334337
if ttl is None:
335338
ttl = 2**63
336339
check_hashes = _make_hash_checker(
@@ -397,6 +400,8 @@ def cache_package(
397400
unextracted archive) after a download; it is not performed if the directory
398401
already exists in the cache.
399402
"""
403+
_conf.check_str("name", name)
404+
_conf.check_str("url", url, allow_empty=False)
400405
check_hashes = _make_hash_checker(
401406
dict(sha1=sha1, sha256=sha256, sha512=sha512)
402407
)

src/cjdk/_conf.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,29 @@ class Configuration:
5151
_allow_insecure_for_testing: bool
5252

5353

54+
def check_str(
55+
name: str,
56+
value: object,
57+
*,
58+
allow_none: bool = False,
59+
allow_empty: bool = True,
60+
) -> None:
61+
if value is None:
62+
if allow_none:
63+
return
64+
raise TypeError(f"{name} must be a string, got None")
65+
if not isinstance(value, str):
66+
raise TypeError(f"{name} must be a string, got {type(value).__name__}")
67+
if not allow_empty and value == "":
68+
raise ConfigError(f"{name} must not be empty")
69+
70+
5471
def configure(**kwargs: Unpack[ConfigKwargs]) -> Configuration:
5572
# kwargs must have API-specific items removed before passing here.
5673

74+
for name in ("jdk", "os", "arch", "vendor", "version"):
75+
check_str(name, kwargs.get(name), allow_none=True)
76+
5777
jdk = kwargs.pop("jdk", None)
5878
if jdk:
5979
if kwargs.get("vendor"):

0 commit comments

Comments
 (0)