← All tasks
javascriptclaude-code/javascript-t1 #2Not a task: already works

JSON Schema Validator (javascript, written by Claude Code)

envgap__claude-code__javascript-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
package.json
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / TASK AND FAILURE

claude-code/javascript-t1 #2 · read the task the agent was given
Claude Code wrote this javascript 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 JavaScript project for a clean Ubuntu 22.04 machine with only Node.js 20+ (LTS) installed. Include:
- Source code
- package.json 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.

package.json
{
  "name": "json-schema-validator-trial1",
  "version": "1.0.0",
  "description": "JSON Schema Validator using Ajv",
  "main": "validator.js",
  "scripts": {
    "start": "node validator.js",
    "validate": "node validator.js"
  },
  "dependencies": {
    "ajv": "8.12.0",
    "ajv-formats": "2.1.1"
  }
}
README.md
# JSON Schema Validator - JavaScript Trial 1

## Dependencies
- **ajv** (8.12.0): Another JSON Schema Validator - fast JSON Schema Draft 7 validation
- **ajv-formats** (2.1.1): Format validation for Ajv (email, uri, date, etc.)

## Setup
```bash
npm install
```

## Usage
```bash
# Validate a JSON file against a schema
node validator.js data.json schema.json

# Generate sample files with intentional violations
node validator.js
```

## Output
- Console report with all violations and JSON paths
- `validation_report.json` with structured error details
validator.js
#!/usr/bin/env node
/**
 * JSON Schema Validator - Trial 1
 * Uses Ajv (Another JSON Schema Validator) for JSON Schema Draft 7 validation.
 * Validates JSON documents and produces detailed reports with JSON paths.
 */

const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');

// --- Sample Data and Schema ---

function generateSampleData() {
    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"
    };
}

function generateSampleSchema() {
    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"
            }
        }
    };
}

// --- Validation ---

function formatInstancePath(instancePath) {
    if (!instancePath || instancePath === '') return '$';
    // instancePath from Ajv looks like "/users/1/email"
    const parts = instancePath.split('/').filter(p => p !== '');
    let result = '$';
    for (const part of parts) {
        if (/^\d+$/.test(part)) {
            result += `[${part}]`;
        } else {
            result += `.${part}`;
        }
    }
    return result;
}

function validateJson(data, schema) {
    const ajv = new Ajv({
        allErrors: true,
        verbose: true,
        strict: false
    });
    addFormats(ajv);

    const validate = ajv.compile(schema);
    const valid = validate(data);

    if (valid) return [];

    const errors = validate.errors.map(err => {
        const jsonPath = formatInstancePath(err.instancePath);
        let value = 'N/A';

        if (err.data !== undefined && typeof err.data !== 'object') {
            value = String(err.data);
        } else if (typeof err.data === 'object' && err.data !== null) {
            value = Array.isArray(err.data) ? '<array>' : '<object>';
        }

        return {
            path: jsonPath,
            message: err.message,
            validator: err.keyword,
            params: err.params,
            value: value
        };
    });

    return errors;
}

// --- Reporting ---

function printReport(errors, dataFile, schemaFile) {
    const sep = '='.repeat(70);
    const sep2 = '-'.repeat(70);
    const timestamp = new Date().toISOString();

    console.log(sep);
    console.log('JSON SCHEMA VALIDATION REPORT');
    console.log(sep);
    console.log(`  Data file:   ${dataFile}`);
    console.log(`  Schema file: ${schemaFile}`);
    console.log(`  Timestamp:   ${timestamp}`);
    console.log(sep2);

    if (errors.length === 0) {
        console.log('\n  RESULT: VALID - No violations found.\n');
    } else {
        console.log(`\n  RESULT: INVALID - ${errors.length} violation(s) found.\n`);
        console.log(sep2);

        errors.forEach((err, i) => {
            console.log(`\n  Violation #${i + 1}`);
            console.log(`    Path:      ${err.path}`);
            console.log(`    Validator: ${err.validator}`);
            console.log(`    Message:   ${err.message}`);
            if (err.value !== 'N/A') {
                console.log(`    Value:     ${err.value}`);
            }
            if (err.params) {
                console.log(`    Params:    ${JSON.stringify(err.params)}`);
            }
        });
    }

    console.log('\n' + sep);
}

function saveReport(errors, dataFile, schemaFile, outputPath) {
    const report = {
        timestamp: new Date().toISOString(),
        data_file: dataFile,
        schema_file: schemaFile,
        valid: errors.length === 0,
        error_count: errors.length,
        errors: errors.map(err => ({
            path: err.path,
            message: err.message,
            validator: err.validator,
            value: err.value,
            params: err.params
        }))
    };

    fs.writeFileSync(outputPath, JSON.stringify(report, null, 2), 'utf-8');
    console.log(`  Report saved to: ${outputPath}`);
}

function loadJsonFile(filePath) {
    if (!fs.existsSync(filePath)) {
        console.error(`Error: File not found: ${filePath}`);
        process.exit(1);
    }

    try {
        const content = fs.readFileSync(filePath, 'utf-8');
        return JSON.parse(content);
    } catch (e) {
        if (e instanceof SyntaxError) {
            console.error(`Error: Malformed JSON in ${filePath}`);
            console.error(`  ${e.message}`);
        } else {
            console.error(`Error reading ${filePath}: ${e.message}`);
        }
        process.exit(1);
    }
}

// --- Main ---

function main() {
    const args = process.argv.slice(2);
    let dataFile, schemaFile, data, schema;

    if (args.length === 2) {
        dataFile = args[0];
        schemaFile = args[1];
        data = loadJsonFile(dataFile);
        schema = loadJsonFile(schemaFile);
    } else if (args.length === 0) {
        console.log('No input files provided. Generating sample data and schema...\n');
        data = generateSampleData();
        schema = generateSampleSchema();
        dataFile = 'sample_data.json';
        schemaFile = 'sample_schema.json';

        fs.writeFileSync(dataFile, JSON.stringify(data, null, 2), 'utf-8');
        fs.writeFileSync(schemaFile, JSON.stringify(schema, null, 2), 'utf-8');

        console.log(`  Generated: ${dataFile}`);
        console.log(`  Generated: ${schemaFile}\n`);
    } else {
        console.log('Usage: node validator.js <data.json> <schema.json>');
        console.log('       node validator.js  (generates sample files)');
        process.exit(1);
    }

    const errors = validateJson(data, schema);
    printReport(errors, dataFile, schemaFile);
    saveReport(errors, dataFile, schemaFile, 'validation_report.json');
}

main();