aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo.casagrande@linaro.org>2015-03-06 16:13:05 +0100
committerMilo Casagrande <milo.casagrande@linaro.org>2015-03-06 16:13:05 +0100
commitb637947c2dee57083d06777890a0df30bfad136b (patch)
treeefe9d6453f1e55574b1398ceb63f1c2dd313d138
parent438af4f1b2e97e96f0774b2786f030897d9de252 (diff)
Fix corner case in validator logic.
-rw-r--r--app/utils/validator.py28
1 files changed, 15 insertions, 13 deletions
diff --git a/app/utils/validator.py b/app/utils/validator.py
index f131da8..a558cc8 100644
--- a/app/utils/validator.py
+++ b/app/utils/validator.py
@@ -35,18 +35,19 @@ def is_valid_json(json_obj, accepted_keys):
valid_json = False
error_message = "No valid keys defined for this request"
- if accepted_keys:
- if isinstance(accepted_keys, types.ListType):
- # Simple case, where the valid keys is just a list of keys.
- valid_json, error_message = _simple_json_validation(
- json_obj, accepted_keys
- )
- elif isinstance(accepted_keys, types.DictionaryType):
- # More complex case where accepted_keys is a a dictionary with
- # mandatory and all the valid keys.
- valid_json, error_message = _complex_json_validation(
- json_obj, accepted_keys
- )
+ if not isinstance(json_obj, types.DictionaryType):
+ error_message = "Provided data is not JSON"
+ else:
+ if accepted_keys:
+ if isinstance(accepted_keys, types.ListType):
+ # Simple case, where the valid keys is just a list of keys.
+ valid_json, error_message = _simple_json_validation(
+ json_obj, accepted_keys)
+ elif isinstance(accepted_keys, types.DictionaryType):
+ # More complex case where accepted_keys is a a dictionary with
+ # mandatory and all the valid keys.
+ valid_json, error_message = _complex_json_validation(
+ json_obj, accepted_keys)
return valid_json, error_message
@@ -118,7 +119,8 @@ def _complex_json_validation(json_obj, accepted_keys):
strange_keys = list(json_keys - valid_keys)
if strange_keys:
error_message = (
- "Found non recognizable keys, they will not be considered: %s" %
+ "Found non recognizable keys, they will not be "
+ "considered: %s" %
", ".join(strange_keys)
)
# If we have keys that are not defined in our model, remove them.