52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Simple JSON validator CLI - validates JSON files and reports errors."""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def validate_json_file(filepath: str) -> tuple[bool, str]:
|
|
"""Validate a JSON file.
|
|
|
|
Returns:
|
|
(True, "") if valid
|
|
(False, error_message) if invalid
|
|
"""
|
|
try:
|
|
with open(filepath, 'r') as f:
|
|
json.load(f)
|
|
return True, ""
|
|
except FileNotFoundError:
|
|
return False, f"Error: File not found: {filepath}"
|
|
except json.JSONDecodeError as e:
|
|
return False, f"Error: Invalid JSON at line {e.lineno}, column {e.colno}: {e.msg}"
|
|
except Exception as e:
|
|
return False, f"Error reading file: {e}"
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: json_validator.py <file1.json> [file2.json] ...", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
files = sys.argv[1:]
|
|
all_valid = True
|
|
results = []
|
|
|
|
for filepath in files:
|
|
valid, error = validate_json_file(filepath)
|
|
if valid:
|
|
results.append(f"✓ VALID: {filepath}")
|
|
else:
|
|
results.append(f"✗ INVALID: {filepath} - {error}")
|
|
all_valid = False
|
|
|
|
for r in results:
|
|
print(r)
|
|
|
|
sys.exit(0 if all_valid else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |