Skip to content

Commit 55e40ac

Browse files
authored
Merge pull request #354 from yozachar/workshop
patch: moves `base58` and `base64` into `encoding`
2 parents 3b36ec7 + 1aba4f7 commit 55e40ac

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
lines changed

src/validators/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from .cron import cron
99
from .domain import domain
1010
from .email import email
11-
from .hashes import base58, base64, md5, sha1, sha224, sha256, sha512
11+
from .encoding import base58, base64
12+
from .hashes import md5, sha1, sha224, sha256, sha512
1213
from .hostname import hostname
1314
from .i18n import es_cif, es_doi, es_nie, es_nif, fi_business_id, fi_ssn, fr_department, fr_ssn
1415
from .iban import iban
@@ -25,9 +26,8 @@
2526
__all__ = (
2627
# ...
2728
"between",
28-
# crypto addresses
29+
# crypto_addresses
2930
"btc_address",
30-
# "eth_address",
3131
# cards
3232
"amex",
3333
"card_number",
@@ -45,9 +45,10 @@
4545
"domain",
4646
# ...
4747
"email",
48-
# hashes
48+
# encodings
4949
"base58",
5050
"base64",
51+
# hashes
5152
"md5",
5253
"sha1",
5354
"sha224",
@@ -66,7 +67,7 @@
6667
"fr_ssn",
6768
# ...
6869
"iban",
69-
# ip addresses
70+
# ip_addresses
7071
"ipv4",
7172
"ipv6",
7273
# ...

src/validators/encoding.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Encoding."""
2+
3+
# standard
4+
import re
5+
6+
# local
7+
from .utils import validator
8+
9+
10+
@validator
11+
def base58(value: str, /):
12+
"""Return whether or not given value is a valid base58 encoding.
13+
14+
Examples:
15+
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
16+
# Output: True
17+
>>> base58('cUSECm5YzcXJwP')
18+
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})
19+
20+
Args:
21+
value:
22+
base58 string to validate.
23+
24+
Returns:
25+
(Literal[True]): If `value` is a valid base58 encoding.
26+
(ValidationError): If `value` is an invalid base58 encoding.
27+
"""
28+
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False
29+
30+
31+
@validator
32+
def base64(value: str, /):
33+
"""Return whether or not given value is a valid base64 encoding.
34+
35+
Examples:
36+
>>> base64('Y2hhcmFjdGVyIHNldA==')
37+
# Output: True
38+
>>> base64('cUSECm5YzcXJwP')
39+
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})
40+
41+
Args:
42+
value:
43+
base64 string to validate.
44+
45+
Returns:
46+
(Literal[True]): If `value` is a valid base64 encoding.
47+
(ValidationError): If `value` is an invalid base64 encoding.
48+
"""
49+
return (
50+
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
51+
if value
52+
else False
53+
)

src/validators/hashes.py

-46
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,6 @@
77
from .utils import validator
88

99

10-
@validator
11-
def base58(value: str, /):
12-
"""Return whether or not given value is a valid base58 hash.
13-
14-
Examples:
15-
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx')
16-
# Output: True
17-
>>> base58('cUSECm5YzcXJwP')
18-
# Output: ValidationError(func=base58, args={'value': 'cUSECm5YzcXJwP'})
19-
20-
Args:
21-
value:
22-
base58 string to validate.
23-
24-
Returns:
25-
(Literal[True]): If `value` is a valid base58 hash.
26-
(ValidationError): If `value` is an invalid base58 hash.
27-
"""
28-
return re.match(r"^[1-9A-HJ-NP-Za-km-z]+$", value) if value else False
29-
30-
31-
@validator
32-
def base64(value: str, /):
33-
"""Return whether or not given value is a valid base64 hash.
34-
35-
Examples:
36-
>>> base64('Y2hhcmFjdGVyIHNldA==')
37-
# Output: True
38-
>>> base64('cUSECm5YzcXJwP')
39-
# Output: ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})
40-
41-
Args:
42-
value:
43-
base64 string to validate.
44-
45-
Returns:
46-
(Literal[True]): If `value` is a valid base64 hash.
47-
(ValidationError): If `value` is an invalid base64 hash.
48-
"""
49-
return (
50-
re.match(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", value)
51-
if value
52-
else False
53-
)
54-
55-
5610
@validator
5711
def md5(value: str, /):
5812
"""Return whether or not given value is a valid MD5 hash.

0 commit comments

Comments
 (0)