Skip to content

Commit

Permalink
Stop processing further validators if a JSON field validator fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-saeon committed Jan 9, 2019
1 parent 3cc00c9 commit 2416291
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions ckanext/metadata/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def _generate_name(*strings):
text = '-'.join(strings)
return re.sub(r'[^a-z0-9_-]+', '-', text.lower())


def _abort(error_dict, key, message):
"""
Set an error message and stop further validator processing for the specified schema key.
"""
error_dict.setdefault(key, [])
error_dict[key].append(message)
raise tk.StopOnError

# endregion


Expand All @@ -54,9 +63,7 @@ def not_missing(key, data, errors, context):
"""
value = data.get(key)
if value is tk.missing:
errors.setdefault(key, [])
errors[key].append(_('Missing parameter'))
raise tk.StopOnError
_abort(errors, key, _('Missing parameter'))


def not_empty(key, data, errors, context):
Expand All @@ -67,67 +74,69 @@ def not_empty(key, data, errors, context):
not_missing(key, data, errors, context)
value = data.get(key)
if not value:
errors.setdefault(key, [])
errors[key].append(_('Missing value'))
raise tk.StopOnError
_abort(errors, key, _('Missing value'))


def json_object_validator(value):
def json_object_validator(key, data, errors, context):
"""
Checks for well-formed JSON.
"""
value = data.get(key)
if value:
try:
json.loads(value)
except ValueError, e:
raise tk.Invalid(_("JSON decode error: %s") % e.message)
_abort(errors, key, _("JSON decode error: %s") % e.message)

return value


def json_dict_validator(value):
def json_dict_validator(key, data, errors, context):
"""
Checks for well-formed JSON, and that the supplied JSON represents a dictionary.
"""
value = data.get(key)
if value:
try:
obj = json.loads(value)
except ValueError, e:
raise tk.Invalid(_("JSON decode error: %s") % e.message)
_abort(errors, key, _("JSON decode error: %s") % e.message)

if type(obj) is not dict:
raise tk.Invalid(_("Expecting a JSON dictionary"))
_abort(errors, key, _("Expecting a JSON dictionary"))

return value


def json_schema_validator(value):
def json_schema_validator(key, data, errors, context):
"""
Checks that the value represents a valid JSON schema.
"""
value = data.get(key)
if value:
try:
schema = json.loads(value)
JSONValidator.check_schema(schema)
except ValueError, e:
raise tk.Invalid(_("JSON decode error: %s") % e.message)
_abort(errors, key, _("JSON decode error: %s") % e.message)
except AttributeError, e:
raise tk.Invalid(_("Expecting a JSON dictionary"))
_abort(errors, key, _("Expecting a JSON dictionary"))
except jsonschema.SchemaError, e:
raise tk.Invalid(_("Invalid JSON schema: %s") % e.message)
_abort(errors, key, _("Invalid JSON schema: %s") % e.message)

return value


def json_pointer_validator(value):
def json_pointer_validator(key, data, errors, context):
"""
Checks that the value is a valid JSON pointer.
"""
value = data.get(key)
if value:
try:
jsonpointer.JsonPointer(value)
except jsonpointer.JsonPointerException, e:
raise tk.Invalid(_("Invalid JSON pointer: %s") % e.message)
_abort(errors, key, _("Invalid JSON pointer: %s") % e.message)

return value

Expand Down

0 comments on commit 2416291

Please sign in to comment.