Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: python-validators/validators
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.30.0
Choose a base ref
...
head repository: python-validators/validators
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.31.0
Choose a head ref
  • 1 commit
  • 7 files changed
  • 2 contributors

Commits on Jul 8, 2024

  1. feat: add validators for base16 and base32 encodings (#386)

    - add validators for `base16` and `base32` encodings
    - fix: typos in `CHANGES.md`
    
    ---------
    
    Co-authored-by: Yozachar <[email protected]>
    msamsami and yozachar authored Jul 8, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e5d7b4a View commit details
Showing with 134 additions and 4 deletions.
  1. +18 −0 CHANGES.md
  2. +1 −1 SECURITY.md
  3. +2 −0 docs/api/encoding.md
  4. +2 −0 docs/api/encoding.rst
  5. +4 −2 src/validators/__init__.py
  6. +42 −0 src/validators/encoding.py
  7. +65 −1 tests/test_encoding.py
18 changes: 18 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -9,6 +9,24 @@ Note to self: Breaking changes must increment either
-->

## 0.31.0 (2024-07-08)

_**Breaking**_

> No breaking changes were introduced in this version.
_**Features**_

- feat: add validators for `base16` and `base32` encodings by @msamsami in [#386](https://github.com/python-validators/validators/pull/386)

_**Maintenance**_

- maint: bump version by @msamsami in [#386](https://github.com/python-validators/validators/pull/386)

**Full Changelog**: [`0.30.0...0.31.0`](https://github.com/python-validators/validators/compare/0.30.0...0.31.0)

---

## 0.30.0 (2024-07-04)

_**Breaking**_
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

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

## Reporting a Vulnerability

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
@@ -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
@@ -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
@@ -60,6 +60,8 @@
# ...
"email",
# encodings
"base16",
"base32",
"base58",
"base64",
# finance
@@ -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
@@ -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.
66 changes: 65 additions & 1 deletion tests/test_encoding.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,71 @@
import pytest

# local
from validators import ValidationError, base58, base64
from validators import ValidationError, base16, base32, base58, base64

# ==> base16 <== #


@pytest.mark.parametrize(
"value",
[
"a3f4b2",
"01ef",
"abcdef0123456789",
"1234567890abcdef",
"1a2b3c",
"abcdef",
"000102030405060708090A0B0C0D0E0F",
],
)
def test_returns_true_on_valid_base16(value: str):
"""Test returns true on valid base16."""
assert base16(value)


@pytest.mark.parametrize(
"value",
["12345g", "hello world", "1234567890abcdeg", "GHIJKL", "12345G", "!@#$%^", "1a2h3c", "a3f4Z1"],
)
def test_returns_failed_validation_on_invalid_base16(value: str):
"""Test returns failed validation on invalid base16."""
assert isinstance(base16(value), ValidationError)


# ==> base32 <== #


@pytest.mark.parametrize(
"value",
[
"JBSWY3DPEHPK3PXP",
"MFRGGZDFMZTWQ2LK",
"MZXW6YTBOI======",
"MFZWIZLTOQ======",
"GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
"MFRGGZDFMZTWQ2LKNNWG23Q=",
],
)
def test_returns_true_on_valid_base32(value: str):
"""Test returns true on valid base32."""
assert base32(value)


@pytest.mark.parametrize(
"value",
[
"ThisIsNotBase32!",
"12345!",
"Any==invalid=base32=",
"MzXW6yTBOI======",
"JBSWY8DPEHPK9PXP",
"MfZW3zLT9Q======",
],
)
def test_returns_failed_validation_on_invalid_base32(value: str):
"""Test returns failed validation on invalid base32."""
assert isinstance(base32(value), ValidationError)


# ==> base58 <== #