Browse Source

Add JSON validation as a CI stage

Alexander Wilms 2 years ago
parent
commit
f38f2d5b6a
2 changed files with 30 additions and 0 deletions
  1. 25 0
      .github/validate_json.py
  2. 5 0
      .github/workflows/github.yml

+ 25 - 0
.github/validate_json.py

@@ -0,0 +1,25 @@
+import jstyleson
+from pathlib import Path
+from pprint import pprint
+
+errors = []
+for path in sorted(Path('.').glob('**/*.json')):
+    # because path is an object and not a string
+    path_str = str(path)
+    try:
+        with open(path_str, 'r') as file:
+            jstyleson.load(file)
+        print(f"Validation of {path_str} succeeded")
+    except Exception as exc:
+        print(f"Validation of {path_str} failed")
+        pprint(exc)
+        # https://stackoverflow.com/a/72850269/2278742
+        if hasattr(exc, 'pos'):
+            position_msg = f"{path_str}:{exc.lineno}:{exc.colno}"
+            print(position_msg)
+            errors.append({"position": position_msg, "exception": exc})
+
+if errors:
+    print("Summary of errors:")
+    pprint(errors)
+    raise Exception("Not all JSON files are valid")

+ 5 - 0
.github/workflows/github.yml

@@ -145,6 +145,11 @@ jobs:
       with:
         submodules: recursive
 
+    - name: Validate JSON
+      run: |
+        pip install jstyleson
+        python3 .github/validate_json.py
+
     - name: Dependencies
       run: source '${{github.workspace}}/CI/${{matrix.platform}}/before_install.sh'
       env: