Skip to content

Commit fa27cfd

Browse files
author
electis
committed
ValidateMixin, validate password
1 parent 23a3c30 commit fa27cfd

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

genesis_core/user_api/iam/api/controllers.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,36 @@ class ValidationException(ra_e.RestAlchemyException):
5353

5454

5555
class ValidateMixin:
56-
min_length = 8
57-
not_contain: list[str] = [string.whitespace]
58-
must_contain: list[str] = None # [digits, ascii_uppercase, punctuation]
59-
regex: str = None
56+
__validate_min_length__ = 8
57+
__validate_not_contain__: list[str] = [string.whitespace]
58+
__validate_must_contain__: list[str] = None # [digits, punctuation]
59+
__validate_regex__: str = None
6060

6161
def validate(self, value):
6262
error = None
6363
if value is None:
6464
error = "Value is required"
65-
elif self.min_length and len(value) < self.min_length:
66-
error = f"Value must be at least {self.min_length} characters long"
67-
elif self.not_contain:
65+
elif (
66+
self.__validate_min_length__
67+
and len(value) < self.__validate_min_length__
68+
):
69+
error = f"Value must be at least {self.__validate_min_length__} characters long"
70+
elif self.__validate_not_contain__:
6871
value_set = set(value)
69-
for not_contain in self.not_contain:
72+
for not_contain in self.__validate_not_contain__:
7073
if set(not_contain) & value_set:
71-
error = f"Value must not contain {self.not_contain}"
74+
error = f"Value must not contain {self.__validate_not_contain__}"
7275
break
73-
elif self.must_contain:
76+
elif self.__validate_must_contain__:
7477
value_set = set(value)
75-
for required in self.must_contain:
78+
for required in self.__validate_must_contain__:
7679
if not set(required) & value_set:
7780
error = f"Value must contain one of {required}"
7881
break
79-
elif self.regex and not re.match(self.regex, value):
80-
error = f"Value must match regex {self.regex}"
82+
elif self.__validate_regex__ and not re.match(
83+
self.__validate_regex__, value
84+
):
85+
error = f"Value must match regex {self.__validate_regex__}"
8186
if error:
8287
raise ValidationException(description=error)
8388

0 commit comments

Comments
 (0)