Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/validators/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
def base16(value: str, /):
"""Return whether or not given value is a valid base16 encoding.

Each encoded byte requires two hexadecimal characters.

Examples:
>>> base16('a3f4b2')
True
Expand All @@ -25,7 +27,7 @@ def base16(value: str, /):
(Literal[True]): If `value` is a valid base16 encoding.
(ValidationError): If `value` is an invalid base16 encoding.
"""
return re.fullmatch(r"^[0-9A-Fa-f]+$", value) if value else False
return re.fullmatch(r"(?:[0-9A-Fa-f]{2})+", value) if value else False


@validator
Expand Down
6 changes: 6 additions & 0 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def test_returns_failed_validation_on_invalid_base16(value: str):
assert isinstance(base16(value), ValidationError)


@pytest.mark.parametrize("value", ["0", "a", "ABC", "012345678", "abcdef01234567890"])
def test_base16_rejects_incomplete_bytes(value: str):
"""Reject a dangling half-byte even when every character is hexadecimal."""
assert isinstance(base16(value), ValidationError)


# ==> base32 <== #


Expand Down