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
11 changes: 6 additions & 5 deletions src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from .cron import cron
from .domain import domain
from .email import email
from .hashes import base58, base64, md5, sha1, sha224, sha256, sha512
from .encoding import base58, base64
from .hashes import md5, sha1, sha224, sha256, sha512
from .hostname import hostname
from .i18n import es_cif, es_doi, es_nie, es_nif, fi_business_id, fi_ssn, fr_department, fr_ssn
from .iban import iban
Expand All @@ -25,9 +26,8 @@
__all__ = (
# ...
"between",
# crypto addresses
# crypto_addresses
"btc_address",
# "eth_address",
# cards
"amex",
"card_number",
Expand All @@ -45,9 +45,10 @@
"domain",
# ...
"email",
# hashes
# encodings
"base58",
"base64",
# hashes
"md5",
"sha1",
"sha224",
Expand All @@ -66,7 +67,7 @@
"fr_ssn",
# ...
"iban",
# ip addresses
# ip_addresses
"ipv4",
"ipv6",
# ...
Expand Down
53 changes: 53 additions & 0 deletions src/validators/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Encoding."""

# standard
import re

# local
from .utils import validator


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

Examples:
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
# Output: True
>>> base58('cUSECm5YzcXJwP')
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})

Args:
value:
base58 string to validate.

Returns:
(Literal[True]): If `value` is a valid base58 encoding.
(ValidationError): If `value` is an invalid base58 encoding.
"""
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False


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

Examples:
>>> base64('Y2hhcmFjdGVyIHNldA==')
# Output: True
>>> base64('cUSECm5YzcXJwP')
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})

Args:
value:
base64 string to validate.

Returns:
(Literal[True]): If `value` is a valid base64 encoding.
(ValidationError): If `value` is an invalid base64 encoding.
"""
return (
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
if value
else False
)
46 changes: 0 additions & 46 deletions src/validators/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,6 @@
from .utils import validator


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

Examples:
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
# Output: True
>>> base58('cUSECm5YzcXJwP')
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})

Args:
value:
base58 string to validate.

Returns:
(Literal[True]): If `value` is a valid base58 hash.
(ValidationError): If `value` is an invalid base58 hash.
"""
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False


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

Examples:
>>> base64('Y2hhcmFjdGVyIHNldA==')
# Output: True
>>> base64('cUSECm5YzcXJwP')
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})

Args:
value:
base64 string to validate.

Returns:
(Literal[True]): If `value` is a valid base64 hash.
(ValidationError): If `value` is an invalid base64 hash.
"""
return (
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
if value
else False
)


@validator
def md5(value: str, /):
"""Return whether or not given value is a valid MD5 hash.
Expand Down