Skip to content

Commit

Permalink
Use files and type rather than a regexp for dashboard validation
Browse files Browse the repository at this point in the history
  • Loading branch information
samdoran committed Mar 15, 2023
1 parent b06efd9 commit 1c1f276
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ repos:
description: Ensures dashboard files are valid JSON
entry: dev/scripts/validate_dashboards.py
language: python
pass_filenames: false
files: dashboards/.*\.yaml
# pass_filenames: true
files: dashboards/
types:
- yaml
additional_dependencies:
- pyyaml
25 changes: 18 additions & 7 deletions dev/scripts/validate_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@


def main() -> None:
dashboards_path = pathlib.Path(__file__).parent.parent.parent / "dashboards"
errors = {}
for file in dashboards_path.glob("*.yaml"):
yaml_data = yaml.safe_load(file.read_text())
for item in yaml_data["data"]:
files = [pathlib.Path(file) for file in sys.argv[1:]]
for file in files:
try:
yaml_data = yaml.safe_load(file.read_text())
except yaml.error.YAMLError as yaml_exc:
errors[file.name] = f"Error loading YAML: {yaml_exc}"
continue

try:
dashboards = yaml_data["data"]
except (KeyError, TypeError) as exc:
errors[file.name] = f"Error getting 'data' field: {exc}"
continue

for dashboard in dashboards:
try:
json.loads(yaml_data["data"][item])
except json.decoder.JSONDecodeError as exc:
errors[file.name] = exc
json.loads(yaml_data["data"][dashboard])
except (json.decoder.JSONDecodeError, TypeError) as exc:
errors[file.name] = f"Error loading JSON data from '{dashboard}': {exc}"

if errors:
sys.exit("\n".join(f"{k}: {v}" for k, v in errors.items()))
Expand Down

0 comments on commit 1c1f276

Please sign in to comment.