Skip to main content

A pre-commit hook for validating files against jsonschemas.

Project description

pre-commit.ci status

check-jsonschema

A pre-commit hook for checking files against a JSONSchema, built using the python jsonschema package. The schema may be specified as a local or remote (HTTP or HTTPS) file.

Remote files are automatically downloaded and cached if possible.

Supported Hooks

The most generic hook is this one:

  • check-jsonschema: Validate JSON or YAML files against a jsonschema on disk or fetched via HTTP(S)

These hooks check known files against schemas provided by Schemastore:

  • check-github-workflows: Validate GitHub Workflows in .github/workflows/

  • check-github-actions: Validate GitHub Actions in .github/actions/ or the action.yml at the repo root

  • check-travis: Validate Travis config

These hooks check known files against schemas provided by other sources:

  • check-azure-pipelines: Validate Azure Pipelines config against the schema provided by Microsoft

  • check-readthedocs: Validate ReadTheDocs yaml config against the schema provided by ReadTheDocs

  • check-renovate: Validate RenovateBot config against the schema provided by Renovate (does not support config in package.json or JSON5)

Example Usage

Validate GitHub Workflows with Schemastore

You can use the schemastore github workflow schema to lint your GitHub workflow files. All you need to add to your .pre-commit-config.yaml is this:

- repo: https://github.com/sirosen/check-jsonschema
  rev: 0.11.0
  hooks:
    - id: check-github-workflows

Applying an arbitrary schema to files

There is a more general hook available for running any jsonschema against a file or set of files. For example, to implement the GitHub workflow check manually, you could do this:

- repo: https://github.com/sirosen/check-jsonschema
  rev: 0.11.0
  hooks:
    - id: check-jsonschema
      name: "Check GitHub Workflows"
      files: ^\.github/workflows/
      types: [yaml]
      args: ["--schemafile", "https://json.schemastore.org/github-workflow"]

And to check with the builtin schema that a GitHub workflow sets timeout-minutes on all jobs:

- repo: https://github.com/sirosen/check-jsonschema
  rev: 0.11.0
  hooks:
    - id: check-jsonschema
      name: "Check GitHub Workflows set timeout-minutes"
      files: ^\.github/workflows/
      types: [yaml]
      args: ["--builtin-schema", "github-workflows-require-timeout"]

Standalone Usage

You can also pip install check-jsonschema to run the tool manually.

For full usage info:

check-jsonschema --help

CLI Options

These options apply both to standalone usage and pre-commit hook usage.

--schemafile

The path or URL for a file containing a schema to use.

This option is required unless --builtin-schema is used.

--no-cache

Do not cache HTTP(S) downloaded schemas.

--disable-format

JSON Schema defines a "format" attribute for string fields but does not require that any validation for formats be applied.

Starting in version 0.6.0, check-jsonschema will automatically check some formats by default. This flag disables these checks.

Because "format" checking is not done by all JSON Schema tools, it is possible that a file may validate under a schema with a different tool, but fail with check-jsonschema if --disable-format is not set.

--format-regex

Set a mode for handling of the "regex" value for "format". The modes are as follows:

mode description
disabled Skip checking regex, but leave other formats enabled.
default Check for known non-python regex syntaxes. If one is found, the expression always passes. Otherwise, check validity in the python engine.
python Require the regex to be valid in python regex syntax.

--cache-filename

The name to use for caching a remote (HTTP or HTTPS) schema.

Defaults to using the last slash-delimited part of the URI.

--default-filetype

The default filetype to assume on instance files when they are detected neither as JSON nor as YAML.

For example, pass --default-filetype yaml to instruct that files which have no extension should be treated as YAML.

By default, this is not set and files without a detected type of JSON or YAML will fail.

--builtin-schema

The name of a builtin schema from check-jsonschema to use. Use of this option replaces --schemafile, and the two are mutually exclusive.

The following values are valid and refer to vendored copies of schemastore schemas:

  • vendor.azure-pipelines
  • vendor.github-actions
  • vendor.github-workflows
  • vendor.travis
  • vendor.readthedocs
  • vendor.renovate

The following values are valid and refer to custom schemas:

  • github-workflows-require-timeout -- This schema checks that a GitHub workflow explicitly sets timeout-minutes on all jobs. (The default value for this is 6 hours.)

--failover-builtin-schema

Specify one of the vendor schemas which should be used if fetching --schemafile fails.

For example, to download the latest travis schema, but failover to the vendored copy, use

check-jsonschema --schemafile "https://json.schemastore.org/travis" --failover-builtin-schema vendor.travis

This is what is used by the hooks provided by check-jsonschema.

--show-all-validation-errors

On validation errors, show all of the underlying errors which occurred.

This is useful when a schema contains oneOf and anyOf clauses that make the default error message uninformative.

--data-transform

If given, --data-transform applies a transformation which should be applied to instancefiles before they are checked.

e.g. The "azure-pipelines" transform unpacks compile-time expressions for Azure Pipelines files.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

check-jsonschema-0.11.0.tar.gz (146.5 kB view details)

Uploaded Source

Built Distribution

check_jsonschema-0.11.0-py3-none-any.whl (156.7 kB view details)

Uploaded Python 3

File details

Details for the file check-jsonschema-0.11.0.tar.gz.

File metadata

  • Download URL: check-jsonschema-0.11.0.tar.gz
  • Upload date:
  • Size: 146.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for check-jsonschema-0.11.0.tar.gz
Algorithm Hash digest
SHA256 3f413d36b8d30f2802f8f1bb32000ed7a9195522fea0512d4ed95a5300dce623
MD5 7308378775128f4eab4bc9496d5c1264
BLAKE2b-256 2ef560c30d6998f4116c30a250ae81a3511b54dd28880a047bf453817656daea

See more details on using hashes here.

Provenance

File details

Details for the file check_jsonschema-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: check_jsonschema-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 156.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for check_jsonschema-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47f4993c132455c1ea73093e7ac1c8473aaca2c410d8a7e630931169b95dbccc
MD5 efeb50a4f3bd50ad5530fb7de38f164b
BLAKE2b-256 66b005f2b6cc6ef9581e77cbd890b29fc7237e9eb2826f406be56f88f7143bfc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page