Comments (12)
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.
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.
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.
Able to get this sorted out?
from jsonschema.
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.
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.
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.
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.
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.
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.
It's coming, patches welcome of course if you figure out something you think would help others of course though :)
from jsonschema.
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)
- Inaccurate error message while using "unevaluatedProperties": False HOT 1
- Support encountering of a `"deprecated": true` keyword by propagating a deprecation warning HOT 7
- Bug: checking required fields doesn't produce error when not present. HOT 2
- Failed to validate the schema when shifted from RefResolver to Registry HOT 4
- ModuleNotFoundError: No module named 'rpds.rpds' HOT 6
- PyO3 error HOT 5
- Draft07Valiadator doesn't validate dates HOT 1
- Inconsistent behaviour of validator_for depending on http vs https HOT 2
- Validation starting from sub-definition
- Getting schema contents after refs are resolved HOT 1
- `format: uri` doesn't work properly HOT 4
- Issue with oneOf validation HOT 6
- Unable to resolve a realtive `$ref` from a schema with `$id` pointed to a subschema in `$defs` with `$id` HOT 2
- Self referenced schema encapsulation breaks reference resolution HOT 1
- jsonschema.TypeChecker with additional types fails to work after several reference resolutions by the referencing.Registry HOT 2
- validate fails to identify incorrect uuid strings HOT 3
- 'Tuple' arrays not handled correctly HOT 3
- validate() throwing exception for a valid schema HOT 1
- Draft7Validator(TyperError: create.<locals>.Validator.__init__() got an unexpected keyword argument 'registry' HOT 2
- Which is the last version not requiring Rust via its dependencies?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.