Skip to content

ValidationError __str__ is far too verbose on large objects #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
naught101 opened this issue Oct 7, 2020 · 5 comments
Closed

ValidationError __str__ is far too verbose on large objects #748

naught101 opened this issue Oct 7, 2020 · 5 comments

Comments

@naught101
Copy link

I have some objects that are not particularly complex, but have a large number of elements (a few nested arrays with up to 150 elements at some levels).

If I hit a ValidationError (e.g. during testing), then the error is massive - more lines than my terminal retains. I've never seen such a large error in python, and it makes testing difficult, because I can't see the backtrace.

A few possibilities for solutions:

  • Just remove all of the extra information in the str return. Personally I'd think this was fine - the user can always get the values from error.schema and error.instance, or
  • Truncate the objects - ideally by sub-selecting the elements that are causing the error.
    • Also hard-truncate the string representations of the objects (maybe 100 chars?), and add an ellipsis at the end. This is often still useful information, because if a JSON object has any kind of ID, it will usually be at the top.

Happy to submit a PR for either of these, after your feedback on the ideas.

@Julian
Copy link
Member

Julian commented Oct 7, 2020

Hi there. This is essentially a dupe of #243, at least #243 (comment)

Certainly happy to review a PR that uses reprlib.repr.

@Julian
Copy link
Member

Julian commented Oct 7, 2020

(Error messages already should subselect the relevant portion of an object)

@naught101
Copy link
Author

Yep, fair enough. Let's close this, and I'll have a proper read over there and see if I can figure out a solution.

@naught101
Copy link
Author

Yeah, they do sub-select, I was just looking at a top-level error.

@ablakey
Copy link

ablakey commented Feb 25, 2021

For posterity, if anyone wants a short workaround, just catch the error, remove instance, and re-raise:

    try:
        validate(instance=jsonfile, schema=schema, resolver=resolver)
    except jsonschema.exceptions.ValidationError as e:
        e.instance = "<Omitted>"
        raise

Thanks Julian for writing and maintaining this library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants