-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.py
More file actions
53 lines (44 loc) · 1.56 KB
/
Copy pathstring.py
File metadata and controls
53 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re
from dataclasses import dataclass
from typing import Optional
from .base import BaseProperty
@dataclass
class StringProperty(BaseProperty):
regex: Optional[str]
min_len: Optional[int]
max_len: Optional[int]
def type_is_valid(self):
"""
Validate that the property looks like
its of the correct type
"""
try:
str(self._value)
except Exception as err:
raise Exception(
f"Cannot cast {self.name} value {self._value} to string."
) from err
def secondary_validation(self):
"""
Non type based validation you might want to
run against a configuration value of this kind.
"""
if len(self._value) == 0:
raise ValueError(f"Str value for {self.name} is an empty string")
if self.regex:
# TODO - confirm the value matches the regex
regex_search = re.search(self.regex, self._value)
if not regex_search:
raise ValueError(
f"Str value for {self.name} does not match the given regex."
)
if self.min_len:
if len(self._value) < self.min_len:
raise ValueError(
f"Str value for {self.name} is shorter than minimum length {self.min_len}"
)
if self.max_len:
if len(self._value) > self.max_len:
raise ValueError(
f"Str value for {self.name} is longer than maximum length {self.max_len}"
)