105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""JSON Schema Validator Utility - Validates JSON files and produces verification reports."""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
def validate_json_file(file_path: str) -> dict[str, Any]:
|
|
"""Validate a JSON file and return results."""
|
|
result = {
|
|
"file": file_path,
|
|
"exists": False,
|
|
"valid_json": False,
|
|
"structure_valid": False,
|
|
"errors": [],
|
|
"summary": {},
|
|
"lines": 0,
|
|
"char_count": 0
|
|
}
|
|
|
|
path = Path(file_path)
|
|
|
|
# Check file exists
|
|
if not path.exists():
|
|
result["errors"].append(f"File does not exist: {file_path}")
|
|
return result
|
|
|
|
result["exists"] = True
|
|
|
|
# Get file stats
|
|
try:
|
|
result["size_bytes"] = path.stat().st_size
|
|
result["char_count"] = len(path.read_text())
|
|
result["lines"] = len(path.read_text().splitlines())
|
|
except Exception as e:
|
|
result["errors"].append(f"Error reading file stats: {e}")
|
|
|
|
# Parse JSON
|
|
try:
|
|
with open(path, 'r') as f:
|
|
data = json.load(f)
|
|
result["valid_json"] = True
|
|
result["data_type"] = type(data).__name__
|
|
|
|
# Validate structure for common types
|
|
if isinstance(data, dict):
|
|
result["structure_valid"] = True
|
|
result["summary"]["keys"] = list(data.keys())
|
|
result["summary"]["key_count"] = len(data)
|
|
elif isinstance(data, list):
|
|
result["structure_valid"] = True
|
|
result["summary"]["item_count"] = len(data)
|
|
result["summary"]["first_item_type"] = type(data[0]).__name__ if data else "empty"
|
|
elif isinstance(data, (str, int, float, bool, type(None))):
|
|
result["structure_valid"] = True
|
|
result["summary"]["value"] = str(data)[:100]
|
|
else:
|
|
result["errors"].append(f"Unexpected data type: {type(data).__name__}")
|
|
|
|
except json.JSONDecodeError as e:
|
|
result["errors"].append(f"JSON parse error: {e}")
|
|
except Exception as e:
|
|
result["errors"].append(f"Unexpected error: {e}")
|
|
|
|
return result
|
|
|
|
|
|
def main():
|
|
"""Main entry point."""
|
|
if len(sys.argv) < 2:
|
|
print("Usage: json_validator_utility.py <json_file1> [json_file2] ...")
|
|
sys.exit(1)
|
|
|
|
files = sys.argv[1:]
|
|
results = []
|
|
|
|
for file_path in files:
|
|
result = validate_json_file(file_path)
|
|
results.append(result)
|
|
|
|
# Output verification report
|
|
report = {
|
|
"validation_results": results,
|
|
"summary": {
|
|
"total_files": len(results),
|
|
"existing_files": sum(1 for r in results if r["exists"]),
|
|
"valid_json_files": sum(1 for r in results if r["valid_json"]),
|
|
"structure_valid_files": sum(1 for r in results if r["structure_valid"]),
|
|
"files_with_errors": sum(1 for r in results if r["errors"])
|
|
}
|
|
}
|
|
|
|
# Write report
|
|
output_path = "/root/.hermes/wizard-outputs/spark/validation_report.json"
|
|
with open(output_path, 'w') as f:
|
|
json.dump(report, f, indent=2)
|
|
|
|
print(f"Validation complete. Report written to: {output_path}")
|
|
print(f"Summary: {report['summary']}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |