Skip to content

Commit

Permalink
Get more understandable error message on deeper structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jun 4, 2021
1 parent 547fac6 commit 1c89fce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
28 changes: 18 additions & 10 deletions jsonschema_gentypes/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def validate(

validator = Validator(schema)

def format_error(error: jsonschema.exceptions.ValidationError) -> str:
def format_error(error: jsonschema.exceptions.ValidationError) -> List[str]:
position = filename

if hasattr(error.instance, "lc"):
Expand All @@ -111,21 +111,29 @@ def format_error(error: jsonschema.exceptions.ValidationError) -> str:
parent = None
if hasattr(curent_data, "lc"):
parent = curent_data
for path in error.path:
for path in error.absolute_path:
curent_data = curent_data[path]
if hasattr(curent_data, "lc"):
parent = curent_data
if parent is not None:
position = f"{filename}:{parent.lc.line + 1}:{parent.lc.col + 1}" # type: ignore

return (
f" - {position} "
f'({".".join([str(i) for i in error.path] if error.path else "/")}): '
f"{error.message} (rule: "
f'{".".join([str(i) for i in error.schema_path] if error.schema_path else "/")})'
)

return sorted([format_error(e) for e in validator.iter_errors(data)]), data
if error.context:
results = []
for context in error.context:
results += format_error(context)
return results
else:
return [
f"-- {position} "
f'{".".join([str(i) for i in error.absolute_path] if error.absolute_path else "/")}: '
f"{error.message}"
]

results = []
for error in validator.iter_errors(data):
results += format_error(error)
return sorted(results), data


class ValidationError(Exception):
Expand Down
43 changes: 37 additions & 6 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def test_validate_ruamel():
},
)
assert errors == [
" - test.yaml:3:3 (root.0): 8 is not of type 'object' (rule: properties.root.items.type)",
" - test.yaml:4:5 (root.1.test): 8 is not of type 'string' (rule: properties.root.items.properties.test.type)",
" - test.yaml:5:5 (root.2): 'test' is a required property (rule: properties.root.items.required)",
"-- test.yaml:3:3 root.0: 8 is not of type 'object'",
"-- test.yaml:4:5 root.1.test: 8 is not of type 'string'",
"-- test.yaml:5:5 root.2: 'test' is a required property",
]


Expand Down Expand Up @@ -63,9 +63,40 @@ def test_validate_yaml():
},
)
assert errors == [
" - test.yaml (root.0): 8 is not of type 'object' (rule: properties.root.items.type)",
" - test.yaml (root.1.test): 8 is not of type 'string' (rule: properties.root.items.properties.test.type)",
" - test.yaml (root.2): 'test' is a required property (rule: properties.root.items.required)",
"-- test.yaml root.0: 8 is not of type 'object'",
"-- test.yaml root.1.test: 8 is not of type 'string'",
"-- test.yaml root.2: 'test' is a required property",
]


def test_validate_deep():
errors, data = validate(
"test.yaml",
yaml.load(
"""
root:
level2:
test: 8"""
),
{
"type": "object",
"properties": {
"root": {
"type": "object",
"properties": {
"level2": {
"type": "object",
"properties": {
"test": {"type": "string"},
},
}
},
},
},
},
)
assert errors == [
"-- test.yaml root.level2.test: 8 is not of type 'string'",
]


Expand Down

0 comments on commit 1c89fce

Please sign in to comment.