feat: add validators for base16 and base32 encodings by msamsami · Pull Request #386 · python-validators/validators · GitHub
Skip to content
Merged
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
18 changes: 18 additions & 0 deletions CHANGES.md
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

| Version | Supported |
| ---------- | ------------------ |
| `>=0.30.0` | :white_check_mark: |
| `>=0.31.0` | :white_check_mark: |

## Reporting a Vulnerability

Expand Down
2 changes: 2 additions & 0 deletions docs/api/encoding.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# encoding

::: validators.encoding.base16
::: validators.encoding.base32
::: validators.encoding.base58
::: validators.encoding.base64
2 changes: 2 additions & 0 deletions docs/api/encoding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ encoding
--------

.. module:: validators.encoding
.. autofunction:: base16
.. autofunction:: base32
.. autofunction:: base58
.. autofunction:: base64
6 changes: 4 additions & 2 deletions src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .crypto_addresses import btc_address, eth_address, trx_address
from .domain import domain
from .email import email
from .encoding import base58, base64
from .encoding import base16, base32, base58, base64
from .finance import cusip, isin, sedol
from .hashes import md5, sha1, sha224, sha256, sha512
from .hostname import hostname
Expand Down Expand Up @@ -60,6 +60,8 @@
# ...
"email",
# encodings
"base16",
"base32",
"base58",
"base64",
# finance
Expand Down Expand Up @@ -105,4 +107,4 @@
"validator",
)

__version__ = "0.30.0"
__version__ = "0.31.0"
42 changes: 42 additions & 0 deletions src/validators/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
from .utils import validator


@validator
def base16(value: str, /):
"""Return whether or not given value is a valid base16 encoding.

Examples:
>>> base16('a3f4b2')
# Output: True
>>> base16('a3f4Z1')
# Output: ValidationError(func=base16, args={'value': 'a3f4Z1'})

Args:
value:
base16 string to validate.

Returns:
(Literal[True]): If `value` is a valid base16 encoding.
(ValidationError): If `value` is an invalid base16 encoding.
"""
return re.match(r"^[0-9A-Fa-f]+$", value) if value else False


@validator
def base32(value: str, /):
"""Return whether or not given value is a valid base32 encoding.

Examples:
>>> base32('MFZWIZLTOQ======')
# Output: True
>>> base32('MfZW3zLT9Q======')
# Output: ValidationError(func=base32, args={'value': 'MfZW3zLT9Q======'})

Args:
value:
base32 string to validate.

Returns:
(Literal[True]): If `value` is a valid base32 encoding.
(ValidationError): If `value` is an invalid base32 encoding.
"""
return re.match(r"^[A-Z2-7]+=*$", value) if value else False


@validator
def base58(value: str, /):
"""Return whether or not given value is a valid base58 encoding.
Expand Down
66 changes: 65 additions & 1 deletion tests/test_encoding.py