Skip to content
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
26 changes: 26 additions & 0 deletions .github/workflows/check-json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Validate JSON Files

on:
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
run-script:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v7

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: '3.14'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r assets/base/scripts/requirements.txt

- name: Validate JSON files
run: python assets/base/scripts/validate-lang.py assets/base/lang/ --github
1 change: 1 addition & 0 deletions assets/base/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
click>=8.4.2
40 changes: 23 additions & 17 deletions assets/base/scripts/validate-lang.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
import json
import os
import sys
import click

def validate_json_from_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
json.load(file)
return True, "Success"
return True, None, "Success"
except json.JSONDecodeError as e:
return False, str(e)
return False, e.lineno, str(e)
except FileNotFoundError:
return False, "File not found"
return False, None, "File not found"

def validate_json_in_directory(directory):
results = {}
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
file_path = os.path.join(root, file)
success, reason = validate_json_from_file(file_path)
success, line, reason = validate_json_from_file(file_path)
if not success:
results[file_path] = reason
results[file_path] = (line, reason)
return results

@click.command()
@click.argument('directory_path', default='.')
def main(directory_path):
try:
results = validate_json_in_directory(directory_path)
if len(results) == 0:
print("Language files are valid!")
else:
for path, reason in results.items():
print(path + ": " + reason)
raise ValueError("Found syntax errors.")
except ValueError as ex:
print("Language files are NOT valid!", ex)
exit(1)
@click.option('--github', is_flag=True, default=False, help="Emit GitHub Actions error annotations")
def main(directory_path, github):
results = validate_json_in_directory(directory_path)
if len(results) == 0:
print("Language files are valid!")
else:
for path, (line, reason) in results.items():
rel_path = os.path.relpath(path)
if github:
if line:
print(f"::error file={rel_path},line={line}::{reason}")
else:
print(f"::error file={rel_path}::{reason}")
else:
print(f"{path}: {reason}")
print("Language files are NOT valid!")
sys.exit(1)

if __name__ == "__main__":
main()