Files
troupe-standup/wizard-work/spark/json-validator.py
T

48 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Quick JSON validator - validates JSON files and reports errors."""
import json
import sys
from pathlib import Path
def validate_json_file(filepath: str) -> bool:
"""Validate a JSON file. Returns True if valid, False otherwise."""
try:
with open(filepath, 'r') as f:
json.load(f)
print(f"VALID: {filepath}")
return True
except json.JSONDecodeError as e:
print(f"INVALID: {filepath} - {e}")
return False
except FileNotFoundError:
print(f"NOT FOUND: {filepath}")
return False
def main():
if len(sys.argv) < 2:
print("Usage: json-validator.py <file-or-directory>")
sys.exit(1)
paths = sys.argv[1:]
all_valid = True
for path in paths:
p = Path(path)
if p.is_dir():
for json_file in p.rglob("*.json"):
if not validate_json_file(str(json_file)):
all_valid = False
elif p.is_file():
if not validate_json_file(str(p)):
all_valid = False
else:
print(f"NOT FOUND: {path}")
all_valid = False
sys.exit(0 if all_valid else 1)
if __name__ == "__main__":
main()