check-jsonschema
check-jsonschema is a command-line interface (CLI) tool and pre-commit hook for validating JSON/YAML files against JSON schemas. It provides a convenient way to integrate schema validation into development workflows, supporting a wide range of common configuration schemas. The current version is 0.37.1, and it releases frequently to update vendored schemas and add new features.
Common errors
-
Schema validation errors were encountered. <file_path>::<json_path>: '<value>' is not valid under any of the given schemas
cause An instance file contains data that does not conform to the rules defined in the provided JSON schema.fixReview the error message to identify the specific part of the instance data that is invalid (e.g., incorrect type, missing required field, value outside allowed range) and modify the instance file to adhere to the schema. You may also need to adjust the schema if the current validation rule is unintended. -
FileNotFoundError: [Errno 2] No such file or directory: '<file_path>'
cause The command-line tool cannot find the specified schema file or one of the instance files because the path provided is incorrect or the file does not exist.fixVerify that the file path provided for `--schemafile` or any instance file is correct and that the file exists in the specified location. Use absolute paths or ensure the relative path is correct from the directory where `check-jsonschema` is executed. -
ModuleNotFoundError: No module named 'jsonschema.compat'
cause This error typically occurs when a dependency, often an older version of the `jsonschema` library itself, tries to import a module (`jsonschema.compat`) that was removed in `jsonschema` version 4.0 and later.fixDowngrade the `jsonschema` package to a version below 4.0 using `pip install 'jsonschema<4.0'` or ensure all installed packages that depend on `jsonschema` are compatible with the latest version. -
<file_path>::<json_path>: '<value>' is not a 'date'
cause The data in the instance file that is expected to be a `date` (as per the schema's `format: date` keyword) does not adhere to the RFC3339 date format.fixEnsure the value in the instance file matches the `YYYY-MM-DD` format for dates as required by the JSON Schema `date` format. If `date-time` validation is failing unexpectedly, consider that `format` validation is often optional and can be disabled if not strictly required, or ensure your `jsonschema` library version correctly implements it for `date-time`. -
check-jsonschema hook not running for .meta files
cause When `check-jsonschema` is used as a pre-commit hook, it might not validate files with custom extensions (like `.meta`) because pre-commit's default `types` filter doesn't recognize them as JSON or YAML.fixModify your `.pre-commit-config.yaml` to explicitly include the custom file type. You can do this by overriding the `types` or `files` arguments for the `check-jsonschema` hook, for example, by adding `types_or: []` and `files: ^data/.*\.meta$` to match files by name regardless of their detected type.
Warnings
- breaking Python 3.9 support has been dropped.
- breaking Verbose output levels for the CLI have changed. Previously, `-v` showed checked filenames; now, `-v` shows all errors without filenames, and `-vv` (or higher) is required to display filenames checked.
- gotcha When using check-jsonschema as a pre-commit hook with custom schemas, ensure the schema paths are correct relative to the repository root or accessible via defined `$schema` IDs. Incorrect paths or unreachable references will lead to validation errors.
- gotcha check-jsonschema defaults to validating against vendored schemas for common config types (e.g., GitHub Workflows, Dependabot). If you have a custom schema with the same filename or an unconventional path, it might be ignored or validated against the wrong schema.
Install
-
pip install check-jsonschema -
pip install pre-commit && pre-commit install
Imports
- main
from check_jsonschema.main import main
Quickstart
# 1. Create a schema file (e.g., 'my_schema.json')
# echo '{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}' > my_schema.json
# 2. Create a data file to validate (e.g., 'my_data.json')
# echo '{"name": "John Doe"}' > my_data.json
# 3. Run validation via CLI
# check-jsonschema --schema my_schema.json my_data.json
# 4. Integrate with pre-commit (add to .pre-commit-config.yaml)
# - repo: https://github.com/python-jsonschema/check-jsonschema
# rev: '0.37.1'
# hooks:
# - id: check-dependabot
# - id: check-github-workflows