/
formats.py
267 lines (194 loc) 路 7.78 KB
/
formats.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import datetime
import ipaddress
import re
import typing
import uuid
from urllib.parse import urlparse
from typesystem.base import ValidationError
DATE_REGEX = re.compile(r"(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$")
TIME_REGEX = re.compile(
r"(?P<hour>\d{1,2}):(?P<minute>\d{1,2})"
r"(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?"
)
DATETIME_REGEX = re.compile(
r"(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})"
r"[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})"
r"(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?"
r"(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$"
)
UUID_REGEX = re.compile(
r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
)
EMAIL_REGEX = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*'
r")@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,63}(?<!-)\.?$",
re.IGNORECASE,
)
IPV4_REGEX = re.compile(
r"(?:0|25[0-5]|2[0-4]\d|1\d?\d?|[1-9]\d?)"
r"(?:\.(?:0|25[0-5]|2[0-4]\d|1\d?\d?|[1-9]\d?)){3}"
)
IPV6_REGEX = re.compile(r"(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}")
class BaseFormat:
errors: typing.Dict[str, str] = {}
def validation_error(self, code: str) -> ValidationError:
text = self.errors[code].format(**self.__dict__)
return ValidationError(text=text, code=code)
def is_native_type(self, value: typing.Any) -> bool:
raise NotImplementedError() # pragma: no cover
def validate(self, value: typing.Any) -> typing.Union[typing.Any, ValidationError]:
raise NotImplementedError() # pragma: no cover
def serialize(self, obj: typing.Any) -> typing.Optional[str]:
raise NotImplementedError() # pragma: no cover
class DateFormat(BaseFormat):
errors = {
"format": "Must be a valid date format.",
"invalid": "Must be a real date.",
}
def is_native_type(self, value: typing.Any) -> bool:
return isinstance(value, datetime.date)
def validate(self, value: typing.Any) -> datetime.date:
match = DATE_REGEX.match(value)
if not match:
raise self.validation_error("format")
kwargs = {k: int(v) for k, v in match.groupdict().items()}
try:
return datetime.date(**kwargs)
except ValueError:
raise self.validation_error("invalid")
def serialize(self, obj: typing.Optional[datetime.date]) -> typing.Optional[str]:
if obj is None:
return None
assert isinstance(obj, datetime.date)
return obj.isoformat()
class TimeFormat(BaseFormat):
errors = {
"format": "Must be a valid time format.",
"invalid": "Must be a real time.",
}
def is_native_type(self, value: typing.Any) -> bool:
return isinstance(value, datetime.time)
def validate(self, value: typing.Any) -> datetime.time:
match = TIME_REGEX.match(value)
if not match:
raise self.validation_error("format")
groups = match.groupdict()
if groups["microsecond"]:
groups["microsecond"] = groups["microsecond"].ljust(6, "0")
kwargs = {k: int(v) for k, v in groups.items() if v is not None}
try:
return datetime.time(tzinfo=None, **kwargs)
except ValueError:
raise self.validation_error("invalid")
def serialize(self, obj: typing.Optional[datetime.time]) -> typing.Optional[str]:
if obj is None:
return None
assert isinstance(obj, datetime.time)
return obj.isoformat()
class DateTimeFormat(BaseFormat):
errors = {
"format": "Must be a valid datetime format.",
"invalid": "Must be a real datetime.",
}
def is_native_type(self, value: typing.Any) -> bool:
return isinstance(value, datetime.datetime)
def validate(self, value: typing.Any) -> datetime.datetime:
match = DATETIME_REGEX.match(value)
if not match:
raise self.validation_error("format")
groups = match.groupdict()
if groups["microsecond"] is not None:
groups["microsecond"] = groups["microsecond"].ljust(6, "0")
tzinfo_str = groups.pop("tzinfo")
if tzinfo_str == "Z":
tzinfo = datetime.timezone.utc
elif tzinfo_str is not None:
offset_mins = int(tzinfo_str[-2:]) if len(tzinfo_str) > 3 else 0
offset_hours = int(tzinfo_str[1:3])
delta = datetime.timedelta(hours=offset_hours, minutes=offset_mins)
if tzinfo_str[0] == "-":
delta = -delta
tzinfo = datetime.timezone(delta)
else:
tzinfo = None
kwargs = {k: int(v) for k, v in groups.items() if v is not None}
try:
return datetime.datetime(**kwargs, tzinfo=tzinfo) # type: ignore
except ValueError:
raise self.validation_error("invalid")
def serialize(
self, obj: typing.Optional[datetime.datetime]
) -> typing.Optional[str]:
if obj is None:
return None
assert isinstance(obj, datetime.datetime)
value = obj.isoformat()
if value.endswith("+00:00"):
value = value[:-6] + "Z"
return value
class UUIDFormat(BaseFormat):
errors = {"format": "Must be a valid UUID format."}
def is_native_type(self, value: typing.Any) -> bool:
return isinstance(value, uuid.UUID)
def validate(self, value: typing.Any) -> uuid.UUID:
match = UUID_REGEX.match(value)
if not match:
raise self.validation_error("format")
return uuid.UUID(value)
def serialize(self, obj: typing.Optional[uuid.UUID]) -> typing.Optional[str]:
if obj is None:
return None
assert isinstance(obj, uuid.UUID)
return str(obj)
class EmailFormat(BaseFormat):
errors = {"format": "Must be a valid email format."}
def is_native_type(self, value: typing.Any) -> bool:
return False
def validate(self, value: str) -> str:
match = EMAIL_REGEX.match(value)
if not match:
raise self.validation_error("format")
return value
def serialize(self, obj: typing.Optional[str]) -> typing.Optional[str]:
if obj is None:
return None
return obj
class IPAddressFormat(BaseFormat):
errors = {
"format": "Must be a valid IP format.",
"invalid": "Must be a real IP.",
}
def is_native_type(self, value: typing.Any) -> bool:
return isinstance(value, (ipaddress.IPv4Address, ipaddress.IPv6Address))
def validate(
self, value: typing.Any
) -> typing.Union[ipaddress.IPv4Address, ipaddress.IPv6Address]:
match_ipv4 = IPV4_REGEX.match(value)
match_ipv6 = IPV6_REGEX.match(value)
if not match_ipv4 and not match_ipv6:
raise self.validation_error("format")
try:
return ipaddress.ip_address(value)
except ValueError:
raise self.validation_error("invalid")
def serialize(
self, obj: typing.Union[ipaddress.IPv4Address, ipaddress.IPv6Address, None]
) -> typing.Optional[str]:
if obj is None:
return None
assert isinstance(obj, (ipaddress.IPv4Address, ipaddress.IPv6Address))
return str(obj)
class URLFormat(BaseFormat):
errors = {"invalid": "Must be a real URL."}
def is_native_type(self, value: typing.Any) -> bool:
return False
def validate(self, value: typing.Any) -> str:
url = urlparse(value)
if not all([url.scheme, url.netloc]):
raise self.validation_error("invalid")
return str(value)
def serialize(self, obj: typing.Optional[str]) -> typing.Optional[str]:
if obj is None:
return None
return obj