Skip to content

Commit afbc881

Browse files
committed
add md5 hash
1 parent 22b0c51 commit afbc881

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re
2+
3+
from pyvalueobjects.errors.ValueObjectError import ValueObjectError
4+
from pyvalueobjects.strings.non_empty_string import NonEmptyString
5+
6+
7+
class Md5Hash(NonEmptyString):
8+
9+
__MATCHER = re.compile(r'^[a-fA-F0-9]{32}$')
10+
11+
def __init__(self, value: str):
12+
super().__init__(value)
13+
14+
def _validate(self, value: str):
15+
super()._validate(value)
16+
try:
17+
is_correct = Md5Hash.__MATCHER.match(value)
18+
if not is_correct:
19+
raise ValueObjectError('Value must be a valid MD5 hash (32 hexadecimal characters).')
20+
except ValueError:
21+
raise ValueObjectError('Value must be a valid MD5 hash (32 hexadecimal characters).')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pyvalueobjects.abstract.nullablevalueobject import build_nullable
2+
from pyvalueobjects.security.md5_hash import Md5Hash
3+
4+
_nullable_cls = build_nullable(Md5Hash)
5+
6+
7+
class NullableMd5Hash(_nullable_cls):
8+
9+
def __init__(self, value: str = None):
10+
super().__init__(value)

0 commit comments

Comments
 (0)