57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""quick-json-validator.py - Validate JSON files in a directory.
|
|
|
|
Usage: python quick-json-validator.py <directory>
|
|
Exit 0 if all files valid, 1 if any invalid.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def validate_json_files(directory: str):
|
|
"""Validate all .json files in the given directory."""
|
|
dir_path = Path(directory)
|
|
|
|
if not dir_path.exists():
|
|
print(f"Error: Directory '{directory}' does not exist")
|
|
return 1
|
|
|
|
if not dir_path.is_dir():
|
|
print(f"Error: '{directory}' is not a directory")
|
|
return 1
|
|
|
|
json_files = list(dir_path.glob('*.json'))
|
|
|
|
if not json_files:
|
|
print(f"No .json files found in '{directory}'")
|
|
return 0
|
|
|
|
errors = []
|
|
for json_file in json_files:
|
|
try:
|
|
with open(json_file, 'r') as f:
|
|
json.load(f)
|
|
print(f"[OK] {json_file.name}")
|
|
except json.JSONDecodeError as e:
|
|
errors.append((json_file, e))
|
|
print(f"[FAIL] {json_file.name}: {e}")
|
|
except Exception as e:
|
|
errors.append((json_file, e))
|
|
print(f"[ERROR] {json_file.name}: {e}")
|
|
|
|
if errors:
|
|
print(f"\nFound {len(errors)} invalid file(s)")
|
|
return 1
|
|
|
|
print(f"\nAll {len(json_files)} file(s) valid")
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 2:
|
|
print(__doc__)
|
|
sys.exit(1)
|
|
|
|
sys.exit(validate_json_files(sys.argv[1])) |