Last active
August 29, 2015 13:57
-
-
Save boris317/9675336 to your computer and use it in GitHub Desktop.
Format a `JsonWeb` `ValidationError` stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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