Skip to content
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

Get more understandable error message on deep structure #24

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"},
},
}
},
},
},
},
)

Choose a reason for hiding this comment

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

strange indentation no ?

Copy link
Owner Author

Choose a reason for hiding this comment

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

For me, it's correct, It's done by Black...

assert errors == [
"-- test.yaml root.level2.test: 8 is not of type 'string'",
]


Expand Down