Skip to content

Instantly share code, notes, and snippets.

@boris317
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save boris317/9675336 to your computer and use it in GitHub Desktop.

Select an option

Save boris317/9675336 to your computer and use it in GitHub Desktop.
Format a `JsonWeb` `ValidationError` stack
def format_validation_error(error, parent=None, indent=""):
def recurse(e, parent):
return format_validation_error(e, parent=p(parent),
indent=indent + " ")
def p(child):
if parent is None:
return child
return (child.startswith("[") and "" or ".").join([parent, child])
output = ["{0}{1} {2}".format(
indent,
parent and fail(parent + ":") or "",
str(error)
)]
if error.errors is None:
return output[0]
if isinstance(error.errors, dict):
for k, e in error.errors.iteritems():
output.append(recurse(e, k))
elif isinstance(error.errors, list):
for e in error.errors:
output.append(recurse(e, "[{0}]".format(e.extras["index"])))
return "\n".join(output)
def fail(text):
return "\033[91m{0}\033[0m".format(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment