48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Quick JSON validator - validates all .json files in a directory.
|
|
Usage: python3 json-validate.py <directory>
|
|
Exit 0 if all valid, exit 1 if any invalid.
|
|
"""
|
|
import sys
|
|
import json
|
|
from pathlib import Path
|
|
|
|
def validate_json_files(directory: str) -> bool:
|
|
"""Validate all JSON files in directory. Returns True if all valid."""
|
|
dir_path = Path(directory)
|
|
if not dir_path.exists():
|
|
print(f"Error: Directory {directory} does not exist")
|
|
return False
|
|
|
|
json_files = list(dir_path.glob("*.json"))
|
|
if not json_files:
|
|
print(f"No .json files found in {directory}")
|
|
return True
|
|
|
|
all_valid = True
|
|
for json_file in json_files:
|
|
try:
|
|
with open(json_file, 'r') as f:
|
|
json.load(f)
|
|
print(f"✓ {json_file.name}")
|
|
except json.JSONDecodeError as e:
|
|
print(f"✗ {json_file.name}: {e}")
|
|
all_valid = False
|
|
except Exception as e:
|
|
print(f"✗ {json_file.name}: {type(e).__name__}: {e}")
|
|
all_valid = False
|
|
|
|
return all_valid
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print("Usage: json-validate.py <directory>")
|
|
sys.exit(1)
|
|
|
|
if validate_json_files(sys.argv[1]):
|
|
print("\nAll JSON files are valid")
|
|
sys.exit(0)
|
|
else:
|
|
print("\nSome JSON files are invalid")
|
|
sys.exit(1) |