An open API service indexing awesome lists of open source software.

https://github.com/thiagowfx/check-json-schema-meta

A pre-commit hook that validates JSON files contain valid $schema references and validates the JSON data against the referenced schema.
https://github.com/thiagowfx/check-json-schema-meta

json-schema json-schema-validator pre-commit python

Last synced: about 1 month ago
JSON representation

A pre-commit hook that validates JSON files contain valid $schema references and validates the JSON data against the referenced schema.

Awesome Lists containing this project

README

          

# check-json-schema-meta

[![Pytest](https://github.com/thiagowfx/check-json-schema-meta/actions/workflows/pytest.yml/badge.svg)](https://github.com/thiagowfx/check-json-schema-meta/actions/workflows/pytest.yml)

A pre-commit hook that validates JSON files contain valid `$schema` references and validates the JSON data against the referenced schema.

> **Note**: This repository serves as a workaround for [python-jsonschema/check-jsonschema#310](https://github.com/python-jsonschema/check-jsonschema/issues/310) - a feature request to add validation of `$schema` references in JSON files.

## Features

- Validates that JSON files contain a `$schema` key (gracefully skips by default)
- Loads and validates the referenced JSON Schema (supports both local files and URLs)
- Validates JSON data against the schema
- `--strict` flag to make missing `$schema` fail validation
- `--expand-env-vars` flag to enable environment variable expansion in `$schema` paths (e.g. `"${SCHEMA_DIR}/my-schema.json"`)
- Exits with non-zero code on errors but checks all files before exiting
- Integrates with pre-commit hooks

## Installation

1. Install dependencies:
```bash
uv sync
```

2. Add to your `.pre-commit-config.yaml`:
```yaml
repos:
- repo: https://github.com/thiagowfx/check-json-schema-meta
rev: main
hooks:
- id: check-json-schema-meta
```

## Usage

### Pre-commit Hook Usage

One-shot preview (trial):

```shell
prek try-repo https://github.com/thiagowfx/check-json-schema-meta check-json-schema-meta --all
```

Basic configuration in `.pre-commit-config.yaml`:

```yaml
repos:
- repo: https://github.com/thiagowfx/check-json-schema-meta
rev: main replace with the latest tag
hooks:
- id: check-json-schema-meta
```

With strict mode enabled:

```yaml
repos:
- repo: https://github.com/thiagowfx/check-json-schema-meta
rev: main # replace with the latest tag
hooks:
- id: check-json-schema-meta
args: ['--strict']
```

With environment variable expansion enabled:

```yaml
repos:
- repo: https://github.com/thiagowfx/check-json-schema-meta
rev: main # replace with the latest tag
hooks:
- id: check-json-schema-meta
args: ['--expand-env-vars']
```

Run prek hooks:

```bash
# Install prek hooks
prek install

# Run only this specific hook
prek run [--all-files] check-json-schema-meta
```

### Direct Usage

```bash
# Default behavior - gracefully skip files without $schema
uv run check-json-schema-meta file1.json file2.json

# Strict mode - fail on missing $schema
uv run check-json-schema-meta --strict file1.json file2.json
```

### Options

- `--strict`: Make missing `$schema` fail validation. By default, files without `$schema` are gracefully skipped.
- `--expand-env-vars`: Expand environment variables in `$schema` paths.

## Development

Run tests:

```bash
uv run pytest
```

## Requirements

- Python 3.9+
- check-jsonschema package