Skip to content

Commit 7fa871b

Browse files
authored
Merge pull request #29 from OrionReed/master
Throw error if search parameters are not all strings
2 parents ca568b3 + 8d6a01b commit 7fa871b

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

typesense/documents.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from .document import Document
66
from .logger import logger
7+
from .validation import validate_search
8+
from .preprocess import stringify_search_params
79
from collections.abc import Iterable
810

911
class Documents(object):
@@ -94,7 +96,9 @@ def export(self, params=None):
9496
return api_response
9597

9698
def search(self, search_parameters):
97-
return self.api_call.get(self._endpoint_path('search'), search_parameters)
99+
stringified_search_params = stringify_search_params(search_parameters)
100+
validate_search(stringified_search_params)
101+
return self.api_call.get(self._endpoint_path('search'), stringified_search_params)
98102

99103
def delete(self, params=None):
100-
return self.api_call.delete(self._endpoint_path(), params)
104+
return self.api_call.delete(self._endpoint_path(), params)

typesense/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ class ServiceUnavailable(TypesenseClientError):
4545

4646
class HTTPStatus0Error(TypesenseClientError):
4747
pass
48+
49+
50+
class InvalidParameter(TypesenseClientError):
51+
pass

typesense/preprocess.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def stringify_search_params(params):
2+
return {key:stringify(val) for key, val in params.items()}
3+
4+
def stringify(val):
5+
if isinstance(val, bool) or isinstance(val, int):
6+
return str(val).lower()
7+
else:
8+
return val

typesense/validation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typesense.exceptions import InvalidParameter
2+
3+
4+
def validate_search(params):
5+
for key in params:
6+
if not isinstance(params[key], str):
7+
raise InvalidParameter(f"'{key}' field expected a string but was given {type(params[key]).__name__}")

0 commit comments

Comments
 (0)