Skip to content

Validationerror context #86

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

Merged
merged 19 commits into from
Apr 10, 2013

Conversation

gazpachoking
Copy link
Contributor

Moving this to a pull for easier tracking of changes. Adds more attributes to ValidationError as discussed in #79

@gazpachoking
Copy link
Contributor Author

I ended up refactoring a few more things, let me know if you are in to it, I took out most of the arguments from ValidationError.__init__, as they should be set by iter_errors, not the validator functions themselves. Draft 3 requirements is the only exception, (that validator also sucks because it's the only one where path points to a non existing place in the instance.) I also changed the ValidationError -> SchemaError conversion method so as to grab all attributes, not sure if it's too hacky, maybe it should just be changed to manually copy all of the attributes.

error.message, validator=error.validator, path=error.path,
)
schema_error = SchemaError(error.message)
schema_error.__dict__.update(error.__dict__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if all the properties should be manually transferred instead...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer that, yeah.

Mostly the reason __init__ took parameters to begin with was to allow constructing SchemaErrors from ValidationErrors and passing them the existing parameters..

@gazpachoking
Copy link
Contributor Author

Okay, I updated the docs too, the wording may need tweaked on a couple of these though.

if error.validator is None:
error.validator = k
# set details if they weren't already set by the called fn
error.set_details(k, v, instance, _schema)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: these names are kind of uninformative, so let's use keyword args to pass these here.

@gazpachoking
Copy link
Contributor Author

Yeah, I had considered that too, I think it does sound a bit better.

from the subschemas will be available on this property. The
``schema_path`` and ``path`` of these errors will be relative to the
parent error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be helpful if we also added one more (very short) prose section to this file showing an example using all these new attributes. Say something that used anyOf or one of those validators and pulled off this information from it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I added a bit on this, not sure if it's what you had in mind, documentation is not my strong suit. :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually need an attribute for the instance do we? The caller knows what they passed in, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep it, the instance you pass in may be a deeply nested structure, the instance on the error is the one that was passed into the validator that failed. You could trace it out using the path within the instance you passed in, I just put it there because it seems much more convenient to see what actual failed the specific validator without having to find it in the original instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK we should make that clear.
On Apr 9, 2013 11:22 PM, "Chase Sterling" [email protected] wrote:

In docs/errors.rst:

 .. attribute:: path

     A deque containing the path to the offending element (or an empty deque
     if the error happened globally).
  • .. attribute:: instance
  •    The instance that was being validated.
    
  • .. attribute:: context
  •    If the error was caused by errors in subschemas, the list of errors
    
  •    from the subschemas will be available on this property. The
    
  •    `schema_path` and `path` of these errors will be relative to the
    
  •    parent error.
    

I think we should keep it, the instance you pass in may be a deeply nested
structure, the instance on the error is the one that was passed into the
validator that failed. You could trace it out using the path within the
instance you passed in, I just put it there because it seems much more
convenient to see what actual failed the specific validator without having
to find it in the original instance.


Reply to this email directly or view it on GitHubhttps://github.com//pull/86/files#r3727202
.

@gazpachoking
Copy link
Contributor Author

I believe that should sort out the hash ordering issues.

@Julian
Copy link
Member

Julian commented Apr 7, 2013

I actually only meant in the documentation that's being added, but if the tests relied on it that definitely is worth changing too.

Anyways, I'll take another pass over this to double check but think we're almost done here. Thanks :D.

@@ -56,12 +57,34 @@


class _Error(Exception):
def __init__(self, message, validator=None, path=(), cause=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason why we're not having all the new stuff be arguments to __init__ again?

Also, I think we should make Validator.to_schema_error instead be _Error.create_from and then do SchemaError.create_from(validation_error).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and, the call to super (which for Exception.__init__ just puts all the stuff you give it in .args), all the args to __init__ (whatever those may be) should be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stuff I left in __init__ should be set on creation time, the paths are modified by iter_errors, or descend, and the rest should be set by set_details. The only real exception to this is the required keyword from draft 3, and that's because it's being validated from outside validate_required. Those were my thoughts at least, I'm fine with switching it though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no that sounds fine I think. As long as we have create_from / to_validation_error it doesn't really matter much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for backwards compatibility reasons we'll need to leave at least the arguments that were there originally, despite the fact that I agree with your division. And while we're doing that we may as well just have all of them there. It looks like it'd clean up tests a tiny bit too, where we construct fully ready errors.

I started merging, so I can do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds fine, we need to be a bit careful though, as the properties set by set_details should only be set once, and at the same time, (so that when the error gets passed down the chain they aren't overwritten) so if somebody put some of the details in the constructor, then tried to call set_details...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think users shouldn't really be calling set_details, or setting the properties it sets, as they are supposed to be the exact properties that were passed into the validation func. The exception is draft 3 required keyword, because it's validating a keyword on a subschema that is never being validated itself (I think that's sortof a design mistake with draft 3, and one of the reasons it was changed for draft 4.) I am unsure whether users may want to write other validator funcs that work similarly though...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We can document that. Probably also can make this private - even if they do want to change the behavior they can just set the attributes.

@Julian Julian merged commit 16ec13c into python-jsonschema:master Apr 10, 2013
@Julian
Copy link
Member

Julian commented Apr 10, 2013

OK, merged, and did some tidying.

Check it out, if it looks OK we can do a release tomorrow 🎆.

@gazpachoking
Copy link
Contributor Author

Sweet, everything looked good to me at first glance. I'll try to take a closer peek tomorrow, but I didn't see anything that would hold up a release. I'm a little hesitant about the _set stuff, but it shouldn't be needed by users, and your solution seems pretty good to keep backwards compatibility, and I don't have any better ideas at the moment.

@gazpachoking
Copy link
Contributor Author

I changed my mind about the _set stuff after sleeping on it. I like that solution quite a bit now. 👍

@Julian
Copy link
Member

Julian commented Apr 10, 2013

:) awesome.

@@ -15,15 +15,102 @@ raised or returned, depending on which method or function is used.

A human readable message explaining the error.

.. attribute:: validator
.. attribute:: validator_keyword
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the fact that I also like validator_keyword better, I think to avoid the API churn we should leave it as is. It's going to be a pain -- we couldn't remove validator until at least v2.0 and it's just going to annoy for a tiny purity benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You da boss. 😄 That's fine by me.

@gazpachoking gazpachoking deleted the validationerror_context branch April 11, 2013 01:32
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 16ec13c on gazpachoking:validationerror_context into * on Julian:master*.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 16ec13c on gazpachoking:validationerror_context into * on Julian:master*.

Julian added a commit that referenced this pull request Mar 22, 2015
0b657e8 Merge pull request #86 from kylef/patch-1
1bd4151 [README] JSONSchema.swift uses these tests too
8f86716 Revert "Add jon, JSON parser for the fishshell."
db9c629 Merge pull request #82 from bucaran/patch-1
875fa42 Add jon, JSON parser for the fishshell.
64c556c Merge pull request #81 from s-panferov/patch-1
43105d4 Add new Rust library to the list
aa4d927 Merge pull request #80 from seagreen/implementations
20ef067 Add new haskell implementation.
1274070 Merge pull request #79 from Muscula/json-schema-benchmark
6d8cf45 Merge pull request #78 from JamesNK/patch-1
55c4992 Add json-schema-benchmark to list of users of this test suite
645623d Added Newtonsoft.Json.Schema implementation
a7944d1 Merge pull request #76 from Prestaul/patch-1
5729cdf Added skeemas to list of suite users
4600fe3 Make the implementation list a bit less unwieldy now that it's so long (hooray!)
11d6905 Merge remote-tracking branch 'mafintosh/patch-1' into develop
689b80f Merge pull request #74 from bugventure/develop
c36f888 Add request-validator as a user of the test suite
41876b1 Update README.md
aabcb34 Merge pull request #71 from seagreen/additionalproperties
b3d160b Add tests for additionalProperties by itself.

git-subtree-dir: json
git-subtree-split: 0b657e8b0d21a4099d3ce378ba7208073a9295e6
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

Successfully merging this pull request may close these issues.

3 participants