diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-06 16:13:05 +0100 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2015-03-06 16:13:05 +0100 |
commit | b637947c2dee57083d06777890a0df30bfad136b (patch) | |
tree | efe9d6453f1e55574b1398ceb63f1c2dd313d138 | |
parent | 438af4f1b2e97e96f0774b2786f030897d9de252 (diff) |
Fix corner case in validator logic.
-rw-r--r-- | app/utils/validator.py | 28 |
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. |