GithubHelp home page GithubHelp logo

Comments (12)

Julian avatar Julian commented on May 14, 2024

Hi.

Can you clarify what you mean? It's certainly not always empty, see the example in the README (and the ones in the tests). It's filled up in __init__.

Do you have an example that you expected to contain something that doesn't?

Cheers.

from jsonschema.

lbeltrame avatar lbeltrame commented on May 14, 2024

Here's an example:

In [5]: import jsonschema
In [6]: val = jsonschema.Validator()
In [7]: tree = jsonschema.ErrorTree(val.iter_errors(instance, schema))
In [8]: tree.errors # Notice how this is empty
Out[8]: {}
In [9]: tree
Out[9]: <ErrorTree (1 errors)> # However the instance had one error

from jsonschema.

Julian avatar Julian commented on May 14, 2024

Hi. That's not an example because you haven't provided any way for me to reproduce it since there's no instance and schema. Anyways, there's nothing wrong with the example, I think you're just misunderstanding how ErrorTree works. We're in need of some actual documentation so I sympathize. For now all there is is the examples in the README, which was why I was sending you there.

To elaborate, ErrorTree is a tree, whose children are more nodes of the same class. The nodes correspond to the nesting of the instance. ErrorTree.errors are the errors at the current node. So your tree has 0 global errors (errors on the root node), but a child has an error. You can get at child nodes by indexing the tree for the child you want to check and looking up its .errors.

Also, as an aside, assignment never copies anything in Python. Look at the code. It's just recursing down the tree. Closing this as invalid, feel free to reopen if you've found some unexpected behavior.

from jsonschema.

Julian avatar Julian commented on May 14, 2024

Able to get this sorted out?

from jsonschema.

malfus avatar malfus commented on May 14, 2024

Here;s a complete example:

from jsonschema import ErrorTree, Draft3Validator

schema = {
"type": "object" ,
"additionalProperties" : False,
"properties": {

"data":
{
    "type": "object" ,
    "additionalProperties" : False,
    "id": "data",
    "required": True,
    "properties": {

    "user_id":
    {
        "type": "number" ,
        "id": "user_id",
        "required": True
    }
    }
}

}

}

example = {
"extra_prop": 12345,
"data": {
"user_id": "spam"
},
}

v = Draft3Validator(schema)
tree = ErrorTree(v.iter_errors(example))

sorted(tree.errors)
['additionalProperties']

0 in tree
False
1 in tree
False
sorted(tree[0].errors)
[]

for err in sorted(v.iter_errors(example),key=str):
... print(err)
...
'spam' is not of type 'number'
Additional properties are not allowed ('extra_prop' was unexpected)

ErrorTree is empty though there are clearly 2 errors...

from jsonschema.

Julian avatar Julian commented on May 14, 2024

That's correct.

0 in tree semantically means "Does the item in my instance at index 0 have any errors?". It does not (there in fact is not even a 0 key in your dict, so arguably that should raise an IndexError but it doesn't do so for a reason I forget at the moment)

If you try "data" in tree you will get True, which means there's an error in the data property of your object. You can dig into the errors there with tree["data"].errors et al.

from jsonschema.

Julian avatar Julian commented on May 14, 2024

Oh, right, the reason it doesn't is because ErrorTrees don't have any access to the instance themselves at the moment. That might change eventually, maybe we can store a reference to the instance in the error I guess, in which case the behavior will be a bit more tight.

from jsonschema.

malfus avatar malfus commented on May 14, 2024

Nevermind the [0] business (I was just slavishly repeating the example from the documentation) - how can I use ErrorTree to process these 2 errors?

I am able to see the "additionalProperties" error, but not the "type" error. I've tried various things - here are my results:

sorted(tree.errors)
['additionalProperties']

print (tree.errors["additionalProperties"].message) # so far so good...
Additional properties are not allowed ('extra_prop' was unexpected)

now trying to use 'data' as a tree index...

...
'data' in tree
True
sorted(tree['data'].errors)
[]

looks like the error I can't see is a "type" error, so I'll try indexing errors by type

...
print(tree.errors["type"].message)
Traceback (most recent call last):
File "", line 1, in
KeyError: 'type'

#if I do this, I can iterate over the errors:
for error in v.iter_errors(example):
... print(error)
...
Additional properties are not allowed ('extra_prop' was unexpected)
'spam' is not of type 'number'

So, for the given example, please show me how to see both of these errors - I'm missing something.

Thanks!

from jsonschema.

Julian avatar Julian commented on May 14, 2024

Sure, again I sympathize, there needs to be a comprehensive example for how to use ErrorTree but docs are a thing on the long list of things to do :).

So the way you'd get those two are:

additionalProperties is a global error (meaning the error takes place at the root of the instance), so that you get at as you have done. I.e. you'd do if "additionalProperties" in tree.errors: to see if your instance had additional properties for instance.

The other error occurs in the user_id key of the data key's value.

So you'd do tree["data"]["user_id"] to get at that child tree, and then peek at that's .errors or whatever you wanted to do (so tree["data"]["user_id"].errors will show that error).

from jsonschema.

malfus avatar malfus commented on May 14, 2024

OK - thanks - that works:

print(tree['data']['user_id'].errors)
{'type': ValidationError(u"'spam' is not of type 'number'",)}

For the moment, I guess I'll stick to the "v.iter_errors(example)" approach - looking forward to documentation on the use of ErrorTree (or at least a serious example)!

from jsonschema.

Julian avatar Julian commented on May 14, 2024

It's coming, patches welcome of course if you figure out something you think would help others of course though :)

from jsonschema.

Julian avatar Julian commented on May 14, 2024

For anyone finding this ticket, the current documentation now has examples. See https://python-jsonschema.readthedocs.org/en/latest/errors.html#errortrees

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.