Skip to content

Improve schema validation error messages #140

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

Merged
merged 1 commit into from
Dec 20, 2024
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
5 changes: 1 addition & 4 deletions codebasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,7 @@ def _main():
raise RuntimeError(f"Analysis file {path} must end in .toml.")

with open(path, "rb") as f:
try:
analysis_toml = util._load_toml(f, "analysis")
except BaseException:
raise ValueError("Analysis file failed validation")
analysis_toml = util._load_toml(f, "analysis")

if "codebase" in analysis_toml:
if "exclude" in analysis_toml["codebase"]:
Expand Down
9 changes: 5 additions & 4 deletions codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ def load_importcfg():
with open(path, "rb") as f:
try:
_importcfg_toml = util._load_toml(f, "cbiconfig")
for name, compiler in _importcfg_toml["compiler"].items():
_importcfg[name] = compiler["options"]
except BaseException:
log.error("Configuration file failed validation")
except ValueError as e:
log.error(str(e))
return
for name, compiler in _importcfg_toml["compiler"].items():
_importcfg[name] = compiler["options"]


class Compiler:
Expand Down
6 changes: 3 additions & 3 deletions codebasin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _validate_json(json_object: object, schema_name: str) -> bool:
If the JSON fails to validate, or the schema name is unrecognized.

RuntimeError
If the schema file cannot be located.
If the schema file cannot be located, or the schema is invalid.
"""
schema_paths = {
"analysis": "schema/analysis.schema",
Expand All @@ -106,8 +106,8 @@ def _validate_json(json_object: object, schema_name: str) -> bool:

try:
jsonschema.validate(instance=json_object, schema=schema)
except jsonschema.exceptions.ValidationError:
msg = f"JSON failed schema validation against {schema_path}"
except jsonschema.exceptions.ValidationError as e:
msg = f"Failed schema validation against {schema_path}: {e.message}"
raise ValueError(msg)
except jsonschema.exceptions.SchemaError:
msg = f"{schema_path} is not a valid schema"
Expand Down
Loading