mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-15 20:03:15 +02:00
JSON validation: Support various levels of strictness
This commit is contained in:
40
.github/validate_json.py
vendored
40
.github/validate_json.py
vendored
@@ -1,6 +1,15 @@
|
|||||||
import jstyleson
|
import jstyleson
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import yaml
|
||||||
|
import json5
|
||||||
|
import re
|
||||||
|
|
||||||
|
# 'json', 'json5' or 'yaml'
|
||||||
|
# json: strict, but doesn't preserve line numbers necessarily, since it strips comments before parsing
|
||||||
|
# json5: strict and preserves line numbers even for files will with line comments
|
||||||
|
# yaml: less strict, allows e.g. leading zeros
|
||||||
|
VALIDATION_TYPE = 'json5'
|
||||||
|
|
||||||
errors = []
|
errors = []
|
||||||
for path in sorted(Path('.').glob('**/*.json')):
|
for path in sorted(Path('.').glob('**/*.json')):
|
||||||
@@ -8,16 +17,37 @@ for path in sorted(Path('.').glob('**/*.json')):
|
|||||||
path_str = str(path)
|
path_str = str(path)
|
||||||
try:
|
try:
|
||||||
with open(path_str, 'r') as file:
|
with open(path_str, 'r') as file:
|
||||||
jstyleson.load(file)
|
if VALIDATION_TYPE == 'json':
|
||||||
|
jstyleson.load(file)
|
||||||
|
if VALIDATION_TYPE == 'json5':
|
||||||
|
|
||||||
|
json5.load(file)
|
||||||
|
elif VALIDATION_TYPE == 'yaml':
|
||||||
|
|
||||||
|
file = file.read().replace("\t", " ")
|
||||||
|
file = file.replace("//", "#")
|
||||||
|
yaml.safe_load(file)
|
||||||
print(f"Validation of {path_str} succeeded")
|
print(f"Validation of {path_str} succeeded")
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(f"Validation of {path_str} failed")
|
print(f"Validation of {path_str} failed")
|
||||||
pprint(exc)
|
pprint(exc)
|
||||||
# https://stackoverflow.com/a/72850269/2278742
|
|
||||||
|
error_pos = path_str
|
||||||
|
|
||||||
if hasattr(exc, 'pos'):
|
if hasattr(exc, 'pos'):
|
||||||
position_msg = f"{path_str}:{exc.lineno}:{exc.colno}"
|
# https://stackoverflow.com/a/72850269/2278742
|
||||||
print(position_msg)
|
error_pos = f"{path_str}:{exc.lineno}:{exc.colno}"
|
||||||
errors.append({"position": position_msg, "exception": exc})
|
print(error_pos)
|
||||||
|
# error_msg = "position": position_msg, "exception": exc
|
||||||
|
if hasattr(exc, 'problem_mark'):
|
||||||
|
mark = exc.problem_mark
|
||||||
|
error_pos = f"{path_str}:{mark.line+1}:{mark.column+1}"
|
||||||
|
print(error_pos)
|
||||||
|
if VALIDATION_TYPE == 'json5':
|
||||||
|
pos = re.findall(r'\d+', str(exc))
|
||||||
|
error_pos = f"{path_str}:{pos[0]}:{pos[-1]}"
|
||||||
|
|
||||||
|
errors.append({"error_pos": error_pos, "error_msg": exc})
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
print("Summary of errors:")
|
print("Summary of errors:")
|
||||||
|
2
.github/workflows/github.yml
vendored
2
.github/workflows/github.yml
vendored
@@ -147,7 +147,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Validate JSON
|
- name: Validate JSON
|
||||||
run: |
|
run: |
|
||||||
pip install jstyleson
|
pip3 install json5 jstyleson
|
||||||
python3 .github/validate_json.py
|
python3 .github/validate_json.py
|
||||||
|
|
||||||
- name: Dependencies
|
- name: Dependencies
|
||||||
|
Reference in New Issue
Block a user