7
7
8
8
from pyslicer .utils .data_utils import is_str_empty
9
9
10
+ MAX_QUERY_SIZE = 10
11
+
12
+ MAX_INDEXATION_SIZE = 1000
13
+
10
14
11
15
class SDBaseValidator (object ):
12
16
"""Base field, query and index validator."""
13
17
__metaclass__ = abc .ABCMeta
14
18
19
+ def check_dictionary_value (self , dictionary_value ):
20
+ print dictionary_value
21
+ if isinstance (dictionary_value , dict ):
22
+ self .check_dictionary (dictionary_value )
23
+ elif isinstance (dictionary_value , list ):
24
+ self .check_list (dictionary_value )
25
+ else :
26
+ if dictionary_value is None or dictionary_value == "" :
27
+ raise exceptions .InvalidQueryException (
28
+ "This query has invalid keys or values." )
29
+
30
+ def check_dictionary (self , dictionary ):
31
+ for key in dictionary :
32
+ dictionary_value = dictionary [key ]
33
+ self .check_dictionary_value (dictionary_value )
34
+
35
+ def check_list (self , dictionary ):
36
+ for dictionary_value in dictionary :
37
+ self .check_dictionary_value (dictionary_value )
38
+
15
39
def __init__ (self , dictionary ):
16
40
if not dictionary :
17
- for key in dictionary :
18
- if not dictionary [key ]:
19
- raise exceptions .InvalidQueryException (
20
- "This query has invalid keys or values." )
41
+ raise exceptions .InvalidQueryException (
42
+ "This query has invalid keys or values." )
43
+
44
+ self .check_dictionary (dictionary )
45
+
21
46
self .data = dictionary
22
47
23
48
@abc .abstractmethod
@@ -76,7 +101,7 @@ def validator(self):
76
101
if "bypass-cache" in self .data :
77
102
query_size -= 1
78
103
79
- if query_size > 10 :
104
+ if query_size > MAX_QUERY_SIZE :
80
105
raise exceptions .MaxLimitException (
81
106
"The query count entity has a limit of 10 queries by request." )
82
107
return True
@@ -193,11 +218,6 @@ def _has_empty_field(self):
193
218
Returns:
194
219
false if dictionary don't have empty fields
195
220
"""
196
- if not self .data :
197
- for key in self .data :
198
- if not self .data [key ]:
199
- raise exceptions .InvalidIndexException (
200
- "This index has invalid keys or values." )
201
221
for value in self .data .values ():
202
222
# Value is a dictionary when it is an entity being indexed:
203
223
# "my-entity": {"year": 2016}
@@ -217,7 +237,7 @@ def check_indexation_size(self):
217
237
if "auto-create-fields" in self .data :
218
238
indexation_size -= 1
219
239
220
- if indexation_size > 1000 :
240
+ if indexation_size > MAX_INDEXATION_SIZE :
221
241
raise exceptions .InvalidIndexException (
222
242
"Your index command shouldn't have more than 1000 values." )
223
243
0 commit comments