-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidates.py
87 lines (71 loc) · 3.03 KB
/
validates.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
from chalice import BadRequestError
class Validates:
SUBJECT_MIN_LEN = 1
SUBJECT_MAX_LEN = 80
DESCRIPTION_MAX_LEN = 1000
USERNAME_MIN_LEN = 2
USERNAME_MAX_LEN = 128
STATE_ENUM = ["unstarted", "started", "completed"]
@classmethod
def subject(cls, subject):
if subject is None:
raise BadRequestError(
f"There is no subject (Your Request: {subject}) "
f"REQUIRED: subject is required.")
subject_type = type(subject)
if subject_type is not str:
raise BadRequestError(
f"subject type (Your Request: {subject_type}) "
f"REQUIRED: {str}")
subject_length = len(subject)
if not cls.SUBJECT_MIN_LEN <= subject_length <= cls.SUBJECT_MAX_LEN:
raise BadRequestError(
f"subject length (Your Request: {subject_length}) "
f"REQUIRED: subject length is greater than or equal to {cls.SUBJECT_MIN_LEN}, "
f"less than or equal to {cls.SUBJECT_MAX_LEN}")
@classmethod
def description(cls, description):
if description is None:
return
description_type = type(description)
if description_type is not str:
raise BadRequestError(
f"description type (Your Request: {description_type}) "
f"REQUIRED: {str}")
description_length = len(description)
if not description_length <= cls.DESCRIPTION_MAX_LEN:
raise BadRequestError(
f"subject length (Your Request: {description_length}) "
f"REQUIRED: less than or equal to {cls.DESCRIPTION_MAX_LEN}")
@classmethod
def state(cls, state):
if state is None:
raise BadRequestError(
f"there is no state. (Your Request: {None}) "
f"REQUIRED: state is required.")
state_type = type(state)
if state_type is not str:
raise BadRequestError(
f"state type (Your Request: {state_type}) "
f"REQUIRED: {str}")
if not state in cls.STATE_ENUM:
raise BadRequestError(
f"state enum (Your Request: {state}) "
f"REQUIRED: {', '.join(cls.STATE_ENUM)}")
@classmethod
def username(cls, username):
if username is None:
raise BadRequestError(
f"there is no username. (Your Request: {None}) "
f"REQUIRED: state is required.")
username_type = type(username)
if username_type is not str:
raise BadRequestError(
f"username type (Your Request: {username_type}) "
f"REQUIRED: {str}")
username_length = len(username)
if not cls.USERNAME_MIN_LEN <= username_length <= cls.USERNAME_MAX_LEN:
raise BadRequestError(
f"username length (Your Request: {username_length}) "
f"REQUIRED: greater than or equal to {cls.USERNAME_MIN_LEN}, "
f"less than {cls.USERNAME_MAX_LEN}")