-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidation.py
More file actions
126 lines (97 loc) · 3.1 KB
/
validation.py
File metadata and controls
126 lines (97 loc) · 3.1 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
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
import os
import re
KEY_REGEX = r"^[0-9a-f]{40}$"
STAGE_VALUES = [None, "new", "yes", "later", "no"]
SORT_BY_VALUES = [
"created_at",
"updated_at",
"location",
"location_experience",
"location_education",
"searching",
"scoring",
]
ORDER_BY_VALUES = [None, "desc", "asc"]
VALID_EXTENSIONS = [
".pdf",
".png",
".jpg",
".jpeg",
".bmp",
".doc",
".docx",
".rtf",
".dotx",
".odt",
".odp",
".ppt",
".pptx",
".rtf",
".msg",
]
INVALID_FILENAME = [".", ".."]
def validate_boolean(name, value):
"""
This function validates the fact that the value is a boolean. If not, it raises a
TypeError.
If the given value is a string that can be converted to a boolean, it converts it.
:param name: <string> The name of the variable to validate
:param value: <any> The value to validate
:return: <boolean> The value
"""
if not isinstance(value, bool) or (
isinstance(value, str) and value not in ["0", "1"]
):
raise TypeError(
"{name} must be boolean not {value}".format(name=name, value=value)
)
return value if isinstance(value, bool) else bool(int(value))
def validate_key(obj, value, regex=None):
if not isinstance(value, str) and value is not None:
raise TypeError(obj + " key must be string")
if regex and not bool(re.match(regex, value)):
raise ValueError(f"{obj} key must match {regex}")
return value
def validate_value(value, values, message="value"):
if value not in values:
raise ValueError("{} must be in {}".format(message, str(values)))
return value
def validate_reference(value):
if value is None:
return value
if not isinstance(value, str) and value is not None:
raise TypeError("reference must be string not {}".format(value))
return value
def validate_page(value):
if not isinstance(value, int):
raise TypeError("page must be 'int'")
return value
def validate_limit(value):
if not isinstance(value, int):
raise TypeError("limit must be 'int'")
return value
def validate_score(value):
if not isinstance(value, (int, float)):
raise TypeError("score must be a number, not {}".format(type(value).__name__))
if value <= 0 or value >= 1:
raise ValueError("score must be between 0 and 1 (exclusive)")
return value
def validate_provider_keys(value):
if not value or not all(isinstance(elt, str) for elt in value):
raise TypeError("provider_ids must contain list of strings")
return value
def is_valid_extension(file_path):
ext = os.path.splitext(file_path)[1]
if not ext:
return False
return ext in VALID_EXTENSIONS or ext.lower() in VALID_EXTENSIONS
def is_valid_filename(file_path):
name = os.path.basename(file_path)
return name not in INVALID_FILENAME
def validate_response(response):
if response.headers["Content-Type"] != "application/json":
return {
"code": response.status_code,
"message": "A generic error occurred on the server",
}
return response.json()