schema
schema copied to clipboard
Reference to location within data in SchemaError
I would like SchemaError to tell me where in the data structure the error occurred.
Something like:
# Schema:
Schema({
'key1': {
'child1': {
'child2': [
{'pedo': bool}
]
}
},
Optional('key2'): {
'foo': str
}
})
# Data:
{
'key1': {
'child1': {
'child2': [
{'pedo': 1},
{'pedo': false},
]
}
}
}
# Error message:
{
'key1': {
'child1': {
'child2': {
0: {'pedo': 'this value should be an instance of bool not int'}
}
}
}
}
# Or as a list:
['key1', 'child1', 'child2', 0, 'pedo']
This way I would be able to find the location of the error in my parsed YAML file. It's also just a lot more helpful error message. Something like 1 should be bool tells me almost nothing.
I agree. The current message is quite useless. Having a simple list with the path to the error would be super helpful.
Would be really helpful.
+1
Totally agree, I'm going to take a shot at a PR.
Edit: taking a quick look, I think this can be added pretty readily by doing try/except around the recursive calls and amending raised exceptions with additional information. If the validation is successful, this is basically free; if the validation fails the call stack unwinding builds up the path information.
looks like this may be handled recently by this: https://github.com/keleshev/schema/commit/61cc0bf84dd3efc830a366fb200c386bae35c243
Here's the behavior on your example:
>>> # Schema:
... Schema({
... 'key1': {
... 'child1': {
... 'child2': [
... {'pedo': bool}
... ]
... }
... },
... Optional('key2'): {
... 'foo': str
... }
... }).validate(
... # Data:
... {
... 'key1': {
... 'child1': {
... 'child2': [
... {'pedo': 1},
... {'pedo': False},
... ]
... }
... }
... })
Traceback (most recent call last):
File "<stdin>", line 20, in <module>
File "/Users/kurtrose/schema/schema.py", line 244, in validate
raise SchemaError([k] + x.autos, [e] + x.errors)
schema.SchemaError: Key 'key1' error:
Key 'child1' error:
Key 'child2' error:
Or({'pedo': <type 'bool'>}) did not validate {'pedo': 1}
Key 'pedo' error:
1 should be instance of 'bool'