home / skills / dkyazzentwatwa / chatgpt-skills / json-schema-validator

json-schema-validator skill

/json-schema-validator

This skill validates JSON data against JSON Schema drafts, providing detailed errors, batch checks, and schema generation to ensure data quality.

npx playbooks add skill dkyazzentwatwa/chatgpt-skills --skill json-schema-validator

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
4.6 KB
---
name: json-schema-validator
description: Validate JSON data against JSON Schema specifications. Use for API validation, config file validation, or data quality checks.
---

# JSON Schema Validator

Validate JSON documents against JSON Schema (Draft 7) specifications.

## Features

- **Schema Validation**: Validate JSON against schema
- **Error Details**: Detailed validation error messages
- **Batch Validation**: Validate multiple files
- **Schema Generation**: Generate schema from examples
- **Multiple Drafts**: Support for Draft 4, 6, 7

## Quick Start

```python
from json_validator import JSONValidator

validator = JSONValidator()

# Validate data against schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0}
    },
    "required": ["name"]
}

data = {"name": "John", "age": 30}
result = validator.validate(data, schema)
print(f"Valid: {result['valid']}")
```

## CLI Usage

```bash
# Validate JSON file against schema
python json_validator.py --data user.json --schema user_schema.json

# Validate with inline schema
python json_validator.py --data config.json --schema '{"type": "object"}'

# Generate schema from sample
python json_validator.py --generate sample.json --output schema.json

# Batch validate directory
python json_validator.py --data-dir ./configs/ --schema config_schema.json
```

## API Reference

### JSONValidator Class

```python
class JSONValidator:
    def __init__(self, draft: str = "draft7")

    # Validation
    def validate(self, data: dict, schema: dict) -> dict
    def validate_file(self, data_path: str, schema_path: str) -> dict
    def validate_batch(self, data_files: list, schema: dict) -> list

    # Schema operations
    def generate_schema(self, data: dict) -> dict
    def check_schema(self, schema: dict) -> dict
```

## Validation Result

```python
{
    "valid": True,  # or False
    "errors": [],   # List of error details
    "path": "$",    # JSON path to data root
}

# With errors:
{
    "valid": False,
    "errors": [
        {
            "message": "'name' is a required property",
            "path": "$",
            "schema_path": "required"
        },
        {
            "message": "-5 is less than minimum 0",
            "path": "$.age",
            "schema_path": "properties.age.minimum"
        }
    ]
}
```

## Schema Examples

### Object Schema
```python
schema = {
    "type": "object",
    "properties": {
        "id": {"type": "integer"},
        "name": {"type": "string", "minLength": 1},
        "email": {"type": "string", "format": "email"},
        "active": {"type": "boolean", "default": True}
    },
    "required": ["id", "name", "email"],
    "additionalProperties": False
}
```

### Array Schema
```python
schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "id": {"type": "integer"},
            "value": {"type": "number"}
        }
    },
    "minItems": 1,
    "uniqueItems": True
}
```

### Nested Schema
```python
schema = {
    "type": "object",
    "properties": {
        "user": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "address": {
                    "type": "object",
                    "properties": {
                        "street": {"type": "string"},
                        "city": {"type": "string"}
                    }
                }
            }
        }
    }
}
```

## Generate Schema from Data

```python
validator = JSONValidator()

sample_data = {
    "name": "Product A",
    "price": 29.99,
    "tags": ["electronics", "sale"],
    "inStock": True
}

schema = validator.generate_schema(sample_data)
# Returns inferred schema based on data types
```

## Supported Keywords

### Type Validation
- `type`: string, number, integer, boolean, array, object, null
- `enum`: allowed values
- `const`: exact value

### String Validation
- `minLength`, `maxLength`
- `pattern`: regex pattern
- `format`: email, uri, date, date-time, ipv4, ipv6, uuid

### Number Validation
- `minimum`, `maximum`
- `exclusiveMinimum`, `exclusiveMaximum`
- `multipleOf`

### Array Validation
- `items`: schema for items
- `minItems`, `maxItems`
- `uniqueItems`
- `contains`

### Object Validation
- `properties`: property schemas
- `required`: required properties
- `additionalProperties`
- `minProperties`, `maxProperties`
- `patternProperties`

## Error Handling

```python
result = validator.validate(data, schema)

if not result['valid']:
    for error in result['errors']:
        print(f"Error at {error['path']}: {error['message']}")
```

## Dependencies

- jsonschema>=4.20.0

Overview

This skill validates JSON data against JSON Schema (Draft 4/6/7) specifications to ensure API payloads, configuration files, or datasets conform to expected structures. It returns clear pass/fail results and detailed, actionable error messages for quick debugging. The tool also supports batch validation and schema generation from examples to speed schema creation.

How this skill works

The validator loads a schema and input JSON, runs the schema engine, and returns a result object with a boolean valid flag and a list of error objects that include message, data path, and schema path. It supports file-based and in-memory validation, batch checks over directories, and draft selection (draft4, draft6, draft7). A schema generator infers a schema from sample data to bootstrap validations or create example schemas.

When to use it

  • Validate incoming API requests or responses during development and testing
  • Enforce structure and types for configuration or deployment files
  • Run batch data quality checks over JSON datasets or file directories
  • Generate starter schemas from sample JSON to speed schema creation
  • Validate third-party data before ingestion into pipelines

Best practices

  • Keep schemas modular and small; compose with $ref for reuse
  • Validate at boundaries: validate input when received and before persisting
  • Use detailed schemas (required, types, formats) to catch issues early
  • Run batch validation in CI to prevent regressions in data contracts
  • Prefer explicit additionalProperties:false when you want strict validation

Example use cases

  • API payload validation: validate request bodies against endpoint schemas before processing
  • Config validation: ensure deployment/config files match required keys and types
  • Data pipeline gating: reject or quarantine records that fail schema checks
  • Schema generation: create initial schemas from representative sample JSON
  • CI checks: run batch validation on example files to ensure changes don’t break contracts

FAQ

Which JSON Schema drafts are supported?

Draft 4, Draft 6, and Draft 7 are supported; you can select the draft when instantiating the validator.

What does validation output look like?

Validation returns {valid: bool, errors: [{message, path, schema_path}], path: '$'}. Errors include the JSON path and schema path for quick fixes.