JSON Schema Validator (python, written by Claude Code)
envgap__claude-code__python-t1-2
Written by a coding agent; not on GitHubWritten 2026-02-27
01 / FAILURE SIGNATURE
As the study recorded it
No identifying execution failure has been captured.
Not a benchmark task.
- The project already builds and runs before the fix, so there is nothing to repair.
02 / ENVIRONMENT RECIPE
- Base commit
Not freshly verified- Manifest
requirements.txt- Reproduce
Awaiting issue-specific recipe- Run under trace
Awaiting a meaningful runtime command
03 / TASK AND FAILURE
claude-code/python-t1 #2 · read the task the agent was given
Claude Code wrote this python project from the task below. It installed and ran on a clean Ubuntu 22.04 machine as written. Task given to the agent: TASK: JSON Schema Validator Write a program that validates JSON documents against user-defined schema rules. It should support type checking, required fields, nested object validation, array constraints, and pattern matching. It should collect all violations — not stop at the first — and produce a detailed validation report. FUNCTIONAL REQUIREMENTS: - Accept two command-line arguments: a JSON data file and a JSON schema file - Support validation rules: type checking (string, number, boolean, array, object, null), required fields, min/max for numbers, minLength/maxLength for strings, enum values, regex pattern matching - Validate nested objects and arrays recursively to any depth - Validate array items (all items must conform to a specified type or sub-schema) - Collect ALL validation errors across the entire document, reporting the full JSON path for each violation (e.g., $.users[2].email) - Print the validation report to the console with one line per violation showing path, expected rule, and actual value - Save the validation report as validation_report.json with structured error objects - If no input files are given, generate a sample schema and a sample data file with intentional violations across different rule types, then validate - Handle malformed JSON input gracefully with clear error messages distinguishing parse errors from validation errors Create a complete Python project for a clean Ubuntu 22.04 machine with only Python 3.10+ installed. Include: - Source code - requirements.txt with all dependencies (direct and transitive) pinned to exact versions - README.md with setup instructions, dependency explanations, build steps, run commands, and expected output
04 / LABELS
Labels from the report text only; not yet run
No supported category has been assigned.
Label rules and the text that matched
[]
05 / FILES
The project as the agent wrote it
3 files, exactly as written, before any repair.
README.md
# JSON Schema Validator - Python Trial 1 ## Dependencies - **jsonschema** (4.21.1): Full JSON Schema Draft 7 validation library ## Setup ```bash pip install -r requirements.txt ``` ## Usage ```bash # Validate a JSON file against a schema python validator.py data.json schema.json # Generate sample files with intentional violations python validator.py ``` ## Output - Console report with all violations and JSON paths - `validation_report.json` with structured error details
requirements.txt
jsonschema==4.21.1 attrs==23.2.0 referencing==0.33.0 jsonschema-specifications==2023.12.1 rpds-py==0.17.1
validator.py
#!/usr/bin/env python3
"""
JSON Schema Validator - Trial 1
Uses the jsonschema library for JSON Schema validation.
Validates JSON documents against user-defined schema rules and produces
a detailed validation report with JSON paths for all violations.
"""
import json
import sys
import os
import re
from datetime import datetime
from jsonschema import Draft7Validator, ValidationError
from jsonschema.exceptions import SchemaError
def generate_sample_data():
"""Generate sample JSON data with intentional violations."""
return {
"users": [
{
"id": 1,
"name": "Alice Johnson",
"email": "alice@example.com",
"age": 30,
"role": "admin",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "62701"
}
},
{
"id": "not-a-number",
"name": "",
"email": "invalid-email",
"age": -5,
"role": "superuser",
"address": {
"street": "",
"city": 12345,
"zip": "ABC"
}
},
{
"id": 3,
"age": 200,
"role": "viewer",
"address": {
"street": "789 Oak Ave",
"city": "Shelbyville"
}
}
],
"metadata": {
"version": 2.5,
"tags": ["production", 42, "stable"],
"created": "2024-01-15"
},
"active": "yes"
}
def generate_sample_schema():
"""Generate a sample JSON Schema for validation."""
return {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "User Database",
"type": "object",
"required": ["users", "metadata", "active"],
"properties": {
"users": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["id", "name", "email", "age", "role", "address"],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"email": {
"type": "string",
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"role": {
"type": "string",
"enum": ["admin", "editor", "viewer"]
},
"address": {
"type": "object",
"required": ["street", "city", "zip"],
"properties": {
"street": {
"type": "string",
"minLength": 1
},
"city": {
"type": "string",
"minLength": 1
},
"zip": {
"type": "string",
"pattern": "^[0-9]{5}(-[0-9]{4})?$"
}
}
}
}
}
},
"metadata": {
"type": "object",
"required": ["version", "tags"],
"properties": {
"version": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
},
"tags": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
},
"minItems": 1
}
}
},
"active": {
"type": "boolean"
}
}
}
def format_json_path(path):
"""Convert a deque path from jsonschema to a JSON path string."""
result = "$"
for component in path:
if isinstance(component, int):
result += f"[{component}]"
else:
result += f".{component}"
return result
def validate_json(data, schema):
"""
Validate JSON data against a schema using jsonschema.
Returns a list of all validation errors with JSON paths.
"""
errors = []
try:
Draft7Validator.check_schema(schema)
except SchemaError as e:
errors.append({
"path": "$",
"message": f"Invalid schema: {e.message}",
"validator": "schema",
"schema_path": str(e.schema_path)
})
return errors
validator = Draft7Validator(schema)
for error in sorted(validator.iter_errors(data), key=lambda e: list(e.absolute_path)):
json_path = format_json_path(error.absolute_path)
error_entry = {
"path": json_path,
"message": error.message,
"validator": error.validator,
"schema_path": list(str(p) for p in error.absolute_schema_path),
"value": repr(error.instance) if not isinstance(error.instance, (dict, list)) else f"<{type(error.instance).__name__}>"
}
errors.append(error_entry)
return errors
def print_report(errors, data_file, schema_file):
"""Print a formatted validation report to console."""
print("=" * 70)
print("JSON SCHEMA VALIDATION REPORT")
print("=" * 70)
print(f" Data file: {data_file}")
print(f" Schema file: {schema_file}")
print(f" Timestamp: {datetime.now().isoformat()}")
print("-" * 70)
if not errors:
print("\n RESULT: VALID - No violations found.\n")
else:
print(f"\n RESULT: INVALID - {len(errors)} violation(s) found.\n")
print("-" * 70)
for i, err in enumerate(errors, 1):
print(f"\n Violation #{i}")
print(f" Path: {err['path']}")
print(f" Validator: {err['validator']}")
print(f" Message: {err['message']}")
if 'value' in err:
print(f" Value: {err['value']}")
print("\n" + "=" * 70)
def save_report(errors, data_file, schema_file, output_path="validation_report.json"):
"""Save the validation report as JSON."""
report = {
"timestamp": datetime.now().isoformat(),
"data_file": data_file,
"schema_file": schema_file,
"valid": len(errors) == 0,
"error_count": len(errors),
"errors": errors
}
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)
print(f" Report saved to: {output_path}")
def load_json_file(filepath):
"""Load and parse a JSON file with error handling."""
if not os.path.exists(filepath):
print(f"Error: File not found: {filepath}")
sys.exit(1)
try:
with open(filepath, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError as e:
print(f"Error: Malformed JSON in {filepath}")
print(f" Line {e.lineno}, Column {e.colno}: {e.msg}")
sys.exit(1)
def main():
if len(sys.argv) == 3:
data_file = sys.argv[1]
schema_file = sys.argv[2]
data = load_json_file(data_file)
schema = load_json_file(schema_file)
elif len(sys.argv) == 1:
print("No input files provided. Generating sample data and schema...\n")
data = generate_sample_data()
schema = generate_sample_schema()
data_file = "sample_data.json"
schema_file = "sample_schema.json"
with open(data_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
with open(schema_file, 'w', encoding='utf-8') as f:
json.dump(schema, f, indent=2)
print(f" Generated: {data_file}")
print(f" Generated: {schema_file}\n")
else:
print("Usage: python validator.py <data.json> <schema.json>")
print(" python validator.py (generates sample files)")
sys.exit(1)
errors = validate_json(data, schema)
print_report(errors, data_file, schema_file)
save_report(errors, data_file, schema_file)
if __name__ == "__main__":
main()