-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.py
53 lines (44 loc) · 1.45 KB
/
validator.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
from utils import Status, bot
def validate(**kwargs):
"""Add comments here"""
def wrapper(fun):
def wrap(message, **_):
args = message.text.split(' ')
if len(args) > 1:
del args[0]
else:
args = []
res_kwargs = {}
for i, k in enumerate(kwargs.keys()):
validator, default, required = kwargs[k]
if i < len(args):
value = args[i]
else:
if required:
return bot.reply_to(
message,
f'Required field "{k}" of {validator.__name__} '
'type'
)
value = default
try:
res_kwargs[k] = validator(value)
except ValueError as e:
return bot.reply_to(
message,
f'Invalid type for "{k}" field: {str(e)} '
)
return fun(message, **res_kwargs)
return wrap
return wrapper
def arg(_type, default=None, required=False):
return _type, default, required
def status_enum(v):
if v is None:
return None
v = v.upper()
if v in Status.ALL or v == 'ALL':
return v
raise ValueError(
f'Invalid enum type, expected one of: "{Status.ALL}" or "all"'
)