From 1aba4f7822386bd5941fc693abf578823252b7a0 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Wed, 3 Apr 2024 07:30:27 +0530 Subject: [PATCH] patch: moves `base58` and `base64` into `encoding` --- src/validators/__init__.py | 11 ++++---- src/validators/encoding.py | 53 ++++++++++++++++++++++++++++++++++++++ src/validators/hashes.py | 46 --------------------------------- 3 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 src/validators/encoding.py diff --git a/src/validators/__init__.py b/src/validators/__init__.py index 806531ff..dc8513e0 100644 --- a/src/validators/__init__.py +++ b/src/validators/__init__.py @@ -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 @@ -25,9 +26,8 @@ __all__ = ( # ... "between", - # crypto addresses + # crypto_addresses "btc_address", - # "eth_address", # cards "amex", "card_number", @@ -45,9 +45,10 @@ "domain", # ... "email", - # hashes + # encodings "base58", "base64", + # hashes "md5", "sha1", "sha224", @@ -66,7 +67,7 @@ "fr_ssn", # ... "iban", - # ip addresses + # ip_addresses "ipv4", "ipv6", # ... diff --git a/src/validators/encoding.py b/src/validators/encoding.py new file mode 100644 index 00000000..f2720748 --- /dev/null +++ b/src/validators/encoding.py @@ -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 + ) diff --git a/src/validators/hashes.py b/src/validators/hashes.py index d72c86fe..6004d30a 100644 --- a/src/validators/hashes.py +++ b/src/validators/hashes.py @@ -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.