# Technical Documentation: python-jsonschema/jsonschema

> ℹ️ **Provenance:** Multi-Tier Ingestion · [CodeWiki Reference](https://codewiki.google/github.com/python-jsonschema/jsonschema)

### Overview (`json/README.md`)

# JSON Schema Test Suite

[](https://github.com/json-schema-org/.github/blob/main/CODE_OF_CONDUCT.md)
[](https://www.repostatus.org/#active)
[](https://opencollective.com/json-schema)

[](https://zenodo.org/badge/latestdoi/5952934)
[](https://github.com/json-schema-org/JSON-Schema-Test-Suite/actions?query=workflow%3A%22Test+Suite+Sanity+Checking%22)

This repository contains a set of JSON objects that implementers of JSON Schema validation libraries can use to test their validators.
The test suite repository exists to verify specified behavior defined by the JSON Schema specification and should not be confused with a style guide. It is not intended to demonstrate how schemas ought to be written. Tests may appear unusual or unintuitive, but they exist solely to exercise behavior prescribed by the specification.

It is meant to be language agnostic and should require only a JSON parser.
The conversion of the JSON objects into tests within a specific language and test framework of choice is left to be done by the validator implementer.

The recommended workflow of this test suite is to clone the `main` branch of this repository as a `git submodule` or `git subtree`. The `main` branch is always stable.

## Coverage

All JSON Schema specification releases should be well covered by this suite, including versions draft-2020-12, draft-2019-09, draft-07, draft-06, draft-04 and draft-03.
Versions draft-04 and draft-03 are considered "frozen" in that less effort is put in to backport new tests to these versions.

Additional coverage is always welcome, particularly for bugs encountered in real-world implementations.
If you see anything missing or incorrect, please feel free to [file an issue](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues) or [submit a PR](https://github.com/json-schema-org/JSON-Schema-Test-Suite).

@gregsdennis has also started a separate [test suite](https://github.com/gregsdennis/json-schema-vocab-test-suites) that is modelled after this suite to cover third-party vocabularies.

## Introduction to the Test Suite Structure

The tests in this suite are contained in the `tests` directory at the root of this repository.
Inside that directory is a subdirectory for each released version of the specification.

The structure and contents of each file in these directories is described below.

In addition to the version-specific subdirectories, one additional directory is present:

1. `latest/`: a symbolic link which points to the directory which is the most recent release (which may be useful for implementations providing specific entry points for validating against the latest version of the specification)

Inside each version directory there are a number of `.json` files each containing a collection of related tests.
Often the grouping is by property under test, but not always.
In addition to the `.json` files, each version directory contains one or more special subdirectories whose purpose is [described below](#subdirectories-within-each-version-directory), and which contain additional `.json` files.

Each `.json` file consists of a single JSON array of test cases.

### Terminology

For clarity, we first define this document's usage of some testing terminology:

| term            | definition                                                                                                                                                        |
|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **test suite**  | the entirety of the contents of this repository, containing tests for multiple different releases of the JSON Schema specification                                |
| **test case**   | a single schema, along with a description and an array of *test*s                                                                                                 |
| **test**        | within a *test case*, a single test example, containing a description, instance and a boolean indicating whether the instance is valid under the test case schema |
| **test runner** | a program, external to this repository and authored by a user of this suite, which is executing each of the tests in the suite                                    |

An example illustrating this structure is immediately below, and a JSON Schema containing a formal definition of the contents of test cases can be found [alongside this README](./test-schema.json).

### Sample Test Case

Here is a single *test case*, containing one or more tests:

```json
{
    "description": "The test case description",
    "schema": { "type": "string" },
    "tests": [
        {
            "description": "a test with a valid instance",
            "data": "a string",
            "valid": true
        },
        {
            "description": "a test with an invalid instance",
            "data": 15,
            "valid": false
        }
    ]
}
```

### Subdirectories Within Each Version directory

A specification version test directory may contain one or more subdirectories.

These are:

1. `optional/`: Contains tests that are considered optional. Note that this subdirectory currently conflates many reasons why a test may be optional -- it may be because tests within a particular file are indeed not required by the specification but still potentially useful to an implementer, or it may be because tests within it only apply to programming languages with particular functionality (in which case they are not truly optional in such a language). In the future this directory structure will be made richer to reflect these differences more clearly.

2. `proposals/`: Contains a subfolder for each active proposal to the specification. If the proposal is a keyword (generally the case), then the subfolder will bear the name of that keyword. Inside the proposal subfolder is a series of test files that would contain amendments to the required test suite should the proposal be incorporated into the specification. These tests should be considered volatile while the proposal is in development; however, implementations claiming to support the proposal are expected to pass its tests.

## Using the Suite to Test a Validator Implementation

The test suite structure was described [above](#introduction-to-the-test-suite-structure).

If you are authoring a new validator implementation, or adding support for an additional version of the specification, this section describes:

1. How to implement a test runner which passes tests to your validator
2. Assumptions the suite makes about how the test runner will configure your validator
3. Invariants the test suite claims to hold for its tests

### How to Implement a Test Runner

Presented here is a possible implementation of a test runner.
The precise steps described do not need to be followed exactly, but the results of your own procedure should produce the same effects.

To test a specific version:

* For 2019-09 and later published versions, implementations that are able to detect the version of each schema via `$schema` SHOULD be configured to do so
* For draft-07 and earlier, v1 (not yet released), and implementations unable to detect via `$schema`, implementations MUST be configured to expect the version matching the test directory name
* Load any remote references [described below](#additional-assumptions) and configure your implementation to retrieve them via their URIs
* Walk the filesystem tree for that version's subdirectory and for each `.json` file found:

    * if the file is located in the root of the version directory:

        * for each test case present in the file:

            * load the schema from the `"schema"` property
            * load (or log) the test case description from the `"description"` property for debugging or outputting
            * for each test in the `"tests"` property:

                * load the instance to be tested from the `"data"` property
                * load (or log) the individual test description from the `"description"` property for debugging or outputting

                * use the schema loaded above to validate whether the instance is considered valid under your implementation

                * if the result from your implementation matches the value found in the `"valid"` property, your implementation correctly implements the specific example
                * if the result does not match, or your implementation errors or crashes, your implementation does not correctly implement the specific example

    * otherwise it is located in a special subdirectory as described above.
      Follow the additional assumptions and restrictions for the containing subdirectory, then run the test case as above.

If your implementation supports multiple versions, run the above procedure for each version supported, configuring your implementation as appropriate to call each version individually.

### Additional Assumptions

1. The suite, notably in its `refRemote.json` file in each specification version directory, expects a number of remote references to be configured.
   These are JSON documents, identified by URI, which are used by the suite to test the behavior of the `$ref` keyword (and related keywords).
   Depending on your implementation, you may configure how to "register" these *either*:

    * by directly retrieving them off the filesystem from the `remotes/` directory, in which case you should load each schema with a retrieval URI of `http://localhost:1234` followed by the relative path from the remotes directory -- e.g. a `$ref` to `http://localhost:1234/foo/bar/baz.json` is expected to resolve to the contents of the file at `remotes/foo/bar/baz.json`

    * or alternatively, by executing `bin/jsonschema_suite remotes` using the executable in the `bin/` directory, which will output a JSON object containing all of the remotes combined, e.g.:

    ```

    $  bin/jsonschema_suite remotes
    ```
    ```json
    {
        "http://localhost:1234/baseUriChange/folderInteger.json": {
            "type": "integer"
        },
        "http://localhost:1234/baseUriChangeFolder/folderInteger.json": {
            "type": "integer"
        }
    }
    ```

2. Test cases found within [special subdirectories](#subdirectories-within-each-version-directory) may require additional configuration to run.
   In particular, when running tests within the `optional/format` subdirectory, test runners should configure implementations to enable format validation, where the implementation supports it.

### Invariants & Guarantees

The test suite guarantees a number of things about tests it defines.
Any deviation from the below is generally considered a bug.
If you suspect one, please [file an issue](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/new):

1. All files containing test cases are valid JSON.
2. The contents of the `"schema"` property in a test case are always valid
   JSON Schemas under the corresponding specification.

   The rationale behind this is that we are testing instances in a test's `"data"` element, and not the schema itself.
   A number of tests *do* test the validity of a schema itself, but do so by representing the schema as an instance inside a test, with the associated meta-schema in the `"schema"` property (via the `"$ref"` keyword):

   ```json
   {
       "description": "Test the \"type\" schema keyword",
       "schema": {
           "$ref": "https://json-schema.org/draft/2019-09/schema"
        },
       "tests": [
           {
               "description": "Valid: string",
               "data": {
                   "type": "string"
               },
               "valid": true
           },
           {
               "description": "Invalid: null",
               "data": {
                   "type": null
               },
               "valid": false
           }
       ]
   }
   ```
   See below for some [known limitations](#known-limitations).

## Known Limitations

This suite expresses its assertions about the behavior of an implementation *within* JSON Schema itself.
Each test is the application of a schema to a particular instance.
This means that the suite of tests can test against any behavior a schema can describe, and conversely cannot test against any behavior which a schema is incapable of representing, even if the behavior is mandated by the specification.

For example, a schema can require that a string is a _URI-reference_ and even that it matches a certain pattern, but even though the specification contains [recommendations about URIs being normalized](https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-id-keyword), a JSON schema cannot today represent this assertion within the core vocabularies of the specifications, so no test covers this behavior.

## Who Uses the Test Suite

This suite is being used by:

### Clojure

* [jinx](https://github.com/juxt/jinx)
* [json-schema](https://github.com/tatut/json-schema)
* [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, Scala, and JavaScript)

### Coffeescript

* [jsck](https://github.com/pandastrike/jsck)

### Common Lisp

* [json-schema](https://github.com/fisxoj/json-schema)

### C++

* [Blaze](https://github.com/sourcemeta/blaze)
* [Modern C++ JSON schema validator](https://github.com/pboettch/json-schema-validator)
* [Valijson](https://github.com/tristanpenman/valijson)

### Dart

* [json\_schema](https://github.com/patefacio/json_schema)

### Elixir

* [ex\_json\_schema](https://github.com/jonasschmidt/ex_json_schema)

### Erlang

* [jesse](https://github.com/for-GET/jesse)

### Go

* [gojsonschema](https://github.com/sigu-399/gojsonschema)
* [validate-json](https://github.com/cesanta/validate-json)

### Haskell

* [aeson-schema](https://github.com/timjb/aeson-schema)
* [hjsonschema](https://github.com/seagreen/hjsonschema)

### Java

* [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
* [json-schema-validator](https://github.com/daveclayton/json-schema-validator)
* [everit-org/json-schema](https://github.com/everit-org/json-schema)
* [networknt/json-schema-validator](https://github.com/networknt/json-schema-validator)
* [Justify](https://github.com/leadpony/justify)
* [Snow](https://github.com/ssilverman/snowy-json)
* [jsonschemafriend](https://github.com/jimblackler/jsonschemafriend)
* [OpenAPI JSON Schema Generator](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator)
* [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Kotlin, Scala, and JavaScript)

### JavaScript

* [json-schema-benchmark](https://github.com/Muscula/json-schema-benchmark)
* [direct-schema](https://github.com/IreneKnapp/direct-schema)
* [is-my-json-valid](https://github.com/mafintosh/is-my-json-valid)
* [jassi](https://github.com/iclanzan/jassi)
* [JaySchema](https://github.com/natesilva/jayschema)
* [json-schema-valid](https://github.com/ericgj/json-schema-valid)
* [Jsonary](https://github.com/jsonary-js/jsonary)
* [jsonschema](https://github.com/tdegrunt/jsonschema)
* [request-validator](https://github.com/bugventure/request-validator)
* [skeemas](https://github.com/Prestaul/skeemas)
* [tv4](https://github.com/geraintluff/tv4)
* [z-schema](https://github.com/zaggino/z-schema)
* [jsen](https://github.com/bugventure/jsen)
* [ajv](https://github.com/epoberezkin/ajv)
* [djv](https://github.com/korzio/djv)
* [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, and Scala)
* [TypeBox](https://github.com/sinclairzx81/typebox)

### Kotlin

* [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
* [kotlinx-schema](https://github.com/Kotlin/kotlinx-schema)
* [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Scala, and JavaScript)

### Lua

* [lua-schema](https://framagit.org/fperrad/lua-schema)

### Node.js

For node.js developers, the suite is also available as an [npm](https://www.npmjs.com/package/@json-schema-org/tests) package.

Node-specific support is maintained in a [separate repository](https://github.com/json-schema-org/json-schema-test-suite-npm) which also welcomes your contributions!

### .NET

* [JsonSchema.Net](https://github.com/json-everything/json-everything)
* [Newtonsoft.Json.Schema](https://github.com/JamesNK/Newtonsoft.Json.Schema)

### Perl

* [Test::JSON::Schema::Acceptance](https://github.com/karenetheridge/Test-JSON-Schema-Acceptance) (a wrapper of this test suite)
* [JSON::Schema::Modern](https://github.com/karenetheridge/JSON-Schema-Modern)
* [JSON::Schema::Tiny](https://github.com/karenetheridge/JSON-Schema-Tiny)

### PHP

* [opis/json-schema](https://github.com/opis/json-schema)
* [json-schema](https://github.com/justinrainbow/json-schema)
* [json-guard](https://github.com/thephpleague/json-guard)

### PostgreSQL

* [postgres-json-schema](https://github.com/gavinwahl/postgres-json-schema)
* [is\_jsonb\_valid](https://github.com/furstenheim/is_jsonb_valid)

### Python

* [jsonschema](https://github.com/Julian/jsonschema)
* [fastjsonschema](https://github.com/seznam/python-fastjsonschema)
* [hypothesis-jsonschema](https://github.com/Zac-HD/hypothesis-jsonschema)
* [jschon](https://github.com/marksparkza/jschon)
* [OpenAPI JSON Schema Generator](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator)

### Ruby

* [json-schema](https://github.com/hoxworth/json-schema)
* [json\_schemer](https://github.com/davishmcclurg/json_schemer)

### Rust

* [jsonschema](https://github.com/Stranger6667/jsonschema-rs)
* [valico](https://github.com/rustless/valico)

### Scala

* [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
* [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, and JavaScript)

### Swift

* [JSONSchema](https://github.com/kylef/JSONSchema.swift)
* [swift-json-schema](https://github.com/ajevans99/swift-json-schema)

### Unison

* [typed-json](https://share.unison-lang.org/@frawa/typed-json)

If you use it as well, please fork and send a pull request adding yourself to
the list :).

## Contributing

If you see something missing or incorrect, a pull request is most welcome!

There are some sanity checks in place for testing the test suite. You can run
them with `bin/jsonschema_suite check` or `tox`. They will be run automatically
by [GitHub Actions](https://github.com/json-schema-org/JSON-Schema-Test-Suite/actions?query=workflow%3A%22Test+Suite+Sanity+Checking%22)
as well.

This repository is maintained by the JSON Schema organization, and will be governed by the JSON Schema steering committee (once it exists).

---

### Overview (`json/annotations/README.md`)

# Annotations Tests Suite

The Annotations Test Suite tests which annotations should appear (or not appear)
on which values of an instance. These tests are agnostic of any output format.

## Supported Dialects

Although annotation terminology didn't appear in the spec until 2019-09,
the concept is compatible with every version of JSON Schema. Test Cases in this
Test Suite are designed to be compatible with as many releases of JSON Schema as
possible. They do not include `$schema` or `$id`/`id` keywords so
implementations can run the same Test Suite for each dialect they support.

Since this Test Suite can be used for a variety of dialects, there are a couple
of options that can be used by Test Runners to filter out Test Cases that don't
apply to the dialect under test.

## Test Case Components

### description

A short description of what behavior the Test Case is covering.

### compatibility

The `compatibility` option allows you to set which dialects the Test Case is
compatible with. Test Runners can use this value to filter out Test Cases that
don't apply to the dialect currently under test. The terminology for annotations
didn't appear in the spec until 2019-09, but the concept is compatible with
older releases as well. When setting `compatibility`, test authors should take
into account dialects before 2019-09 for implementations that chose to support
annotations for older dialects.

Dialects are indicated by the number corresponding to their release. Date-based
releases use just the year. If this option isn't present, it means the Test Case
is compatible with any dialect.

If this option is present with a number, the number indicates the minimum
release the Test Case is compatible with. This example indicates that the Test
Case is compatible with draft-07 and up.

**Example**: `"compatibility": "7"`

You can use a `<=` operator to indicate that the Test Case is compatible with
releases less than or equal to the given release. This example indicates that
the Test Case is compatible with 2019-09 and under.

**Example**: `"compatibility": "<=2019"`

You can use comma-separated values to indicate multiple constraints if needed.
This example indicates that the Test Case is compatible with releases between
draft-06 and 2019-09.

**Example**: `"compatibility": "6,<=2019"`

For convenience, you can use the `=` operator to indicate a Test Case is only
compatible with a single release. This example indicates that the Test Case is
compatible only with 2020-12.

**Example**: `"compatibility": "=2020"`

### schema

The schema that will serve as the subject for the tests. Whenever possible, this
schema shouldn't include `$schema` or `id`/`$id` because Test Cases should be
designed to work with as many releases as possible.

### externalSchemas

This allows you to define additional schemas that `schema` makes references to.
The value is an object where the keys are retrieval URIs and values are schemas.
Most external schemas aren't self identifying (using `id`/`$id`) and rely on the
retrieval URI for identification. This is done to increase the number of
dialects that the test is compatible with.  Because `id` changed to `$id` in
draft-06, if you use `$id`, the test becomes incompatible with draft-03/4 and in
most cases, that's not necessary.

### tests

A collection of Tests to run to verify the Test Case.

## Test Components

### instance

The JSON instance to be annotated.

### assertions

A collection of assertions that must be true for the test to pass.

## Assertions Components

### location

The instance location.

### keyword

The annotating keyword.

### expected

A collection of `keyword` annotations expected on the instance at `location`.
`expected` is an object where the keys are schema locations and the values are
the annotation that schema location contributed for the given `keyword`.

There can be more than one expected annotation because multiple schema locations
could contribute annotations for a single keyword.

An empty object is an assertion that the annotation must not appear at the
`location` for the `keyword`.

As a convention for this Test Suite, the `expected` object should be sorted such
that the most recently encountered value for an annotation given top-down
evaluation of the schema comes before previously encountered values.

---

### Overview (`json/output-tests/README.md`)

These tests are intended to validate that implementations are correctly generating output in accordance with the specification.

Output was initially specified with draft 2019-09.  It remained largely unchanged for draft 2020-12, but will receive an update with the next release.

_**NOTE** Although the formats didn't change much between 2019-09 and 2020-12, the tests are copied for 2020-12 because the `$schema` is different and implementations may (but shouldn't) produce different output._

## Organization

The tests are organized by specification release and then into two categories: content and structure.

Content tests verify that the keywords are producing the correct annotations and/or error messages.  Since there are no requirements on the content of error messages, there's not much that can be verified for them, but it is possible to identify when an error message _could_ be present.  Primarily, these tests need to extensively cover the annotation behaviors of each keyword.  The only output format needed for these tests is `basic` for 2019-09/2020-12 and `list` for later versions.

Structure tests verify that the structures of the various formats (i.e. `flag`, `basic`, `detailed`, `verbose` for 2019-09/2020-12 and `flag`, `list`, `hierarchical` for later versions) are correct.  These tests don't need to cover each keyword; rather they need to sufficiently cover the various aspects of building the output structures by using whatever keywords are necessary to do so.

In each release folder, you'll also find an _output-schema.json_ file that contains the schema from the specification repo that describes output for that release.  This schema will need to be loaded as the tests reference it.

## Test Files

The content of a test file is similar to the validation tests in `tests/`: for each test case, the `valid` property has been removed, and an `output` property has been added.

The `output` property itself has a property for each of the output formats where the value is a schema that will successfully validate for compliant output.  For the content tests, only `basic`/`list` needs to be present.

## Other notes

### Ambiguity around 2019-09/2020-12 `basic`

The 2019-09/2020-12 specs don't define the structure of `basic` very thoroughly.  Specifically there is a nuance where if the list contains a single output node, there are two possible structures, given the text:

- the output node for the root schema appears in the list with a containing node that just has a `valid` property
    ```json
    {
        "valid": false,
        "errors": [
            {
                "valid": false,
                "keywordLocation": "",
                "absoluteKeywordLocation": "https://json-schema.org/tests/content/draft2019-09/general/0",
                "instanceLocation": ""
            }
        ]
    }
    ```
- the entire structure is collapsed to just the root output node as `detailed` would do.
    ```json
    {
        "valid": false,
        "keywordLocation": "",
        "absoluteKeywordLocation": "https://json-schema.org/tests/content/draft2019-09/general/0",
        "instanceLocation": ""
    }
    ```
As the Test Suite should not prefer one interpretation over another, these cases need to be tested another way.

A simple solution (though there are likely others) is to force a second output unit by adding an `"anyOf": [ true ]`.  This has no impact on the validation result while adding superfluous structure to the output that avoids the above ambiguous scenario.  The test schema should still be targeted on what's being tested and ignore any output units generated by this extra keyword.

## Contributing

Of course, first and foremost, follow the [Contributing guide](/CONTRIBUTING.md).

When writing test cases, try to keep output validation schemas targeted to verify a single requirement.  Where possible (and where it makes sense), create multiple tests to cover multiple requirements.  This will help keep the output validation schemas small and increase readability.  (It also increases your test count. 😉)

For the content tests, there is also a _general.json_ file that contains tests that do not necessarily pertain to any single keyword.

---

### Overview (`json/tests/v1/proposals/README.md`)

# JSON Schema Proposals Tests

The subfolders herein represent tests for each active proposal to the specification. If the proposal is a keyword (generally the case), then the subfolder will bear the name of that keyword. Inside the proposal subfolder is a series of test files that would contain amendments to the required test suite should the proposal be incorporated into the specification. These tests should be considered volatile while the proposal is in development; however, implementations claiming to support the proposal are expected to pass its tests.

> [!IMPORTANT]
> This folder is NOT intended for proposed additions to the test suite or the specification.

Proposals to the test suite can be made by [opening an issue](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/new) in this repository.

Proposals to the specification should be made by following the process defined by our [Feature Life Cycle](https://github.com/json-schema-org/json-schema-spec/blob/main/PROCESS.md#feature-life-cycle).

---

### Creating (`docs/creating.rst`)

.. currentmodule:: jsonschema.validators

.. _creating-validators:

=======================================
Creating or Extending Validator Classes
=======================================

.. autofunction:: create
    :noindex:

.. autofunction:: extend
    :noindex:

.. autofunction:: validator_for
    :noindex:

.. autofunction:: validates
    :noindex:


Creating Validation Errors
--------------------------

Any validating function that validates against a subschema should call
``descend``, rather than ``iter_errors``. If it recurses into the
instance, or schema, it should pass one or both of the ``path`` or
``schema_path`` arguments to ``descend`` in order to properly maintain
where in the instance or schema respectively the error occurred.

The Validator Protocol
----------------------

``jsonschema`` defines a `protocol <typing.Protocol>`, `jsonschema.protocols.Validator` which can be used in type annotations to describe the type of a validator.

For full details, see `validator-protocol`.

---

### Errors (`docs/errors.rst`)

==========================
Handling Validation Errors
==========================

.. currentmodule:: jsonschema.exceptions

When an invalid instance is encountered, a `ValidationError` will be
raised or returned, depending on which method or function is used.

.. autoexception:: ValidationError
    :noindex:

    The information carried by an error roughly breaks down into:

    ===============  =================  ========================
     What Happened   Why Did It Happen  What Was Being Validated
    ===============  =================  ========================
    `message`        `context`          `instance`

                     `cause`            `json_path`

                                        `path`

                                        `schema`

                                        `schema_path`

                                        `validator`

                                        `validator_value`
    ===============  =================  ========================


    .. attribute:: message

        A human readable message explaining the error.

    .. attribute:: validator

        The name of the failed `keyword
        <https://json-schema.org/draft/2020-12/json-schema-validation.html#name-a-vocabulary-for-structural>`_.

    .. attribute:: validator_value

        The associated value for the failed keyword in the schema.

    .. attribute:: schema

        The full schema that this error came from. This is potentially a
        subschema from within the schema that was passed in originally,
        or even an entirely different schema if a :kw:`$ref` was
        followed.

    .. attribute:: relative_schema_path

        A `collections.deque` containing the path to the failed keyword
        within the schema.

    .. attribute:: absolute_schema_path

        A `collections.deque` containing the path to the failed
        keyword within the schema, but always relative to the
        *original* schema as opposed to any subschema (i.e. the one
        originally passed into a validator class, *not* `schema`\).

    .. attribute:: schema_path

        Same as `relative_schema_path`.

    .. attribute:: relative_path

        A `collections.deque` containing the path to the
        offending element within the instance. The deque can be empty if
        the error happened at the root of the instance.

    .. attribute:: absolute_path

        A `collections.deque` containing the path to the
        offending element within the instance. The absolute path
        is always relative to the *original* instance that was
        validated (i.e. the one passed into a validation method, *not*
        `instance`\). The deque can be empty if the error happened
        at the root of the instance.

    .. attribute:: json_path

        A `JSON path <https://goessner.net/articles/JsonPath/index.html>`_
        to the offending element within the instance.

    .. attribute:: path

        Same as `relative_path`.

    .. attribute:: instance

        The instance that was being validated. This will differ from
        the instance originally passed into ``validate`` if the
        validator object was in the process of validating a (possibly
        nested) element within the top-level instance. The path within
        the top-level instance (i.e. `ValidationError.path`) could
        be used to find this object, but it is provided for convenience.

    .. attribute:: context

        If the error was caused by errors in subschemas, the list of errors
        from the subschemas will be available on this property. The
        `schema_path` and `path` of these errors will be relative
        to the parent error.

    .. attribute:: cause

        If the error was caused by a *non*-validation error, the
        exception object will be here. Currently this is only used
        for the exception raised by a failed format checker in
        `jsonschema.FormatChecker.check`.

    .. attribute:: parent

        A validation error which this error is the `context` of.
        ``None`` if there wasn't one.


In case an invalid schema itself is encountered, a `SchemaError` is
raised.

.. autoexception:: SchemaError
    :noindex:

    The same attributes are present as for `ValidationError`\s.


These attributes can be clarified with a short example:

.. testcode::

    schema = {
        "items": {
            "anyOf": [
                {"type": "string", "maxLength": 2},
                {"type": "integer", "minimum": 5}
            ]
        }
    }
    instance = [{}, 3, "foo"]
    v = Draft202012Validator(schema)
    errors = sorted(v.iter_errors(instance), key=lambda e: e.path)

The error messages in this situation are not very helpful on their own.

.. testcode::

    for error in errors:
        print(error.message)

outputs:

.. testoutput::

    {} is not valid under any of the given schemas
    3 is not valid under any of the given schemas
    'foo' is not valid under any of the given schemas

If we look at `ValidationError.path` on each of the errors, we can find
out which elements in the instance correspond to each of the errors. In
this example, `ValidationError.path` will have only one element, which
will be the index in our list.

.. testcode::

    for error in errors:
        print(list(error.path))

.. testoutput::

    [0]
    [1]
    [2]

Since our schema contained nested subschemas, it can be helpful to look at
the specific part of the instance and subschema that caused each of the errors.
This can be seen with the `ValidationError.instance` and
`ValidationError.schema` attributes.

With keywords like :kw:`anyOf`, the `ValidationError.context`
attribute can be used to see the sub-errors which caused the failure. Since
these errors actually came from two separate subschemas, it can be helpful to
look at the `ValidationError.schema_path` attribute as well to see where
exactly in the schema each of these errors come from. In the case of sub-errors
from the `ValidationError.context` attribute, this path will be relative
to the `ValidationError.schema_path` of the parent error.

.. testcode::

    for error in errors:
        for suberror in sorted(error.context, key=lambda e: e.schema_path):
            print(list(suberror.schema_path), suberror.message, sep=", ")

.. testoutput::

    [0, 'type'], {} is not of type 'string'
    [1, 'type'], {} is not of type 'integer'
    [0, 'type'], 3 is not of type 'string'
    [1, 'minimum'], 3 is less than the minimum of 5
    [0, 'maxLength'], 'foo' is too long
    [1, 'type'], 'foo' is not of type 'integer'

The string representation of an error combines some of these attributes for
easier debugging.

.. testcode::

    print(errors[1])

.. testoutput::

    3 is not valid under any of the given schemas

    Failed validating 'anyOf' in schema['items']:
        {'anyOf': [{'type': 'string', 'maxLength': 2},
                   {'type': 'integer', 'minimum': 5}]}

    On instance[1]:
        3


ErrorTrees
----------

If you want to programmatically query which validation keywords
failed when validating a given instance, you may want to do so using
`jsonschema.exceptions.ErrorTree` objects.

.. autoclass:: jsonschema.exceptions.ErrorTree
    :noindex:
    :members:
    :special-members:
    :exclude-members: __dict__,__weakref__

    .. attribute:: errors

        The mapping of validation keywords to the error objects (usually `jsonschema.exceptions.ValidationError`\s) at this level of the tree.

Consider the following example:

.. testcode::

    schema = {
        "type" : "array",
        "items" : {"type" : "number", "enum" : [1, 2, 3]},
        "minItems" : 3,
    }
    instance = ["spam", 2]

For clarity's sake, the given instance has three errors under this schema:

.. testcode::

    v = Draft202012Validator(schema)
    for error in sorted(v.iter_errors(["spam", 2]), key=str):
        print(error.message)

.. testoutput::

    'spam' is not of type 'number'
    'spam' is not one of [1, 2, 3]
    ['spam', 2] is too short

Let's construct an `jsonschema.exceptions.ErrorTree` so that we
can query the errors a bit more easily than by just iterating over the
error objects.

.. testcode::

    from jsonschema.exceptions import ErrorTree
    tree = ErrorTree(v.iter_errors(instance))

As you can see, `jsonschema.exceptions.ErrorTree` takes an iterable of `ValidationError`\s when constructing a tree so you can directly pass it the return value of a validator's `jsonschema.protocols.Validator.iter_errors` method.

`ErrorTree`\s support a number of useful operations. The first one we
might want to perform is to check whether a given element in our instance
failed validation. We do so using the :keyword:`in` operator:

.. doctest::

    >>> 0 in tree
    True

    >>> 1 in tree
    False

The interpretation here is that the 0th index into the instance (``"spam"``)
did have an error (in fact it had 2), while the 1th index (``2``) did not (i.e.
it was valid).

If we want to see which errors a child had, we index into the tree and look at
the `ErrorTree.errors` attribute.

.. doctest::

    >>> sorted(tree[0].errors)
    ['enum', 'type']

Here we see that the :kw:`enum` and :kw:`type` keywords failed for
index ``0``. In fact `ErrorTree.errors` is a dict, whose values are the
`ValidationError`\s, so we can get at those directly if we want them.

.. doctest::

    >>> print(tree[0].errors["type"].message)
    'spam' is not of type 'number'

Of course this means that if we want to know if a given validation
keyword failed for a given index, we check for its presence in
`ErrorTree.errors`:

.. doctest::

    >>> "enum" in tree[0].errors
    True

    >>> "minimum" in tree[0].errors
    False

Finally, if you were paying close enough attention, you'll notice that
we haven't seen our :kw:`minItems` error appear anywhere yet. This is
because :kw:`minItems` is an error that applies globally to the instance
itself. So it appears in the root node of the tree.

.. doctest::

    >>> "minItems" in tree.errors
    True

That's all you need to know to use error trees.

To summarize, each tree contains child trees that can be accessed by
indexing the tree to get the corresponding child tree for a given
index into the instance. Each tree and child has a `ErrorTree.errors`
attribute, a dict, that maps the failed validation keyword to the
corresponding validation error.


best_match and relevance
------------------------

The `best_match` function is a simple but useful function for attempting
to guess the most relevant error in a given bunch.

.. doctest::

        >>> from jsonschema import Draft202012Validator
        >>> from jsonschema.exceptions import best_match

        >>> schema = {
        ...     "type": "array",
        ...     "minItems": 3,
        ... }
        >>> print(best_match(Draft202012Validator(schema).iter_errors(11)).message)
        11 is not of type 'array'


.. autofunction:: best_match
    :noindex:


.. function:: relevance(validation_error)
    :noindex:

    A key function that sorts errors based on heuristic relevance.

    If you want to sort a bunch of errors entirely, you can use
    this function to do so. Using this function as a key to e.g.
    `sorted` or `max` will cause more relevant errors to be
    considered greater than less relevant ones.

    Within the different validation keywords that can fail, this
    function considers :kw:`anyOf` and :kw:`oneOf` to be *weak*
    validation errors, and will sort them lower than other errors at the
    same level in the instance.

    If you want to change the set of weak [or strong] validation
    keywords you can create a custom version of this function with
    `by_relevance` and provide a different set of each.

.. doctest::

    >>> schema = {
    ...     "properties": {
    ...         "name": {"type": "string"},
    ...         "phones": {
    ...             "properties": {
    ...                 "home": {"type": "string"}
    ...             },
    ...         },
    ...     },
    ... }
    >>> instance = {"name": 123, "phones": {"home": [123]}}
    >>> errors = Draft202012Validator(schema).iter_errors(instance)
    >>> [
    ...     e.path[-1]
    ...     for e in sorted(errors, key=exceptions.relevance)
    ... ]
    ['home', 'name']


.. autofunction:: by_relevance
    :noindex:

---

### Faq (`docs/faq.rst`)

==========================
Frequently Asked Questions
==========================

My schema specifies format validation. Why do invalid instances seem valid?
---------------------------------------------------------------------------

The :kw:`format` keyword can be a bit of a stumbling block for new
users working with JSON Schema.

In a schema such as:

.. code-block:: json

    {"type": "string", "format": "date"}

JSON Schema specifications have historically differentiated between the
:kw:`format` keyword and other keywords. In particular, the
:kw:`format` keyword was specified to be *informational* as much
as it may be used for validation.

In other words, for many use cases, schema authors may wish to use
values for the :kw:`format` keyword but have no expectation
they be validated alongside other required assertions in a schema.

Of course this does not represent all or even most use cases -- many
schema authors *do* wish to assert that instances conform fully, even to
the specific format mentioned.

In drafts prior to ``draft2019-09``, the decision on whether to
automatically enable :kw:`format` validation was left up to
validation implementations such as this one.

This library made the choice to leave it off by default, for two reasons:

    * for forward compatibility and implementation complexity reasons
      -- if :kw:`format` validation were on by default, and a
      future draft of JSON Schema introduced a hard-to-implement format,
      either the implementation of that format would block releases of
      this library until it were implemented, or the behavior surrounding
      :kw:`format` would need to be even more complex than simply
      defaulting to be on. It therefore was safer to start with it off,
      and defend against the expectation that a given format would always
      automatically work.

    * given that a common use of JSON Schema is for portability across
      languages (and therefore implementations of JSON Schema), so that
      users be aware of this point itself regarding :kw:`format`
      validation, and therefore remember to check any *other*
      implementations they were using to ensure they too were explicitly
      enabled for :kw:`format` validation.

As of ``draft2019-09`` however, the opt-out by default behavior mentioned here is now *required* for all implementations of JSON Schema.

Difficult as this may sound for new users, at this point it at least means they should expect the same behavior that has always been implemented here, across any other implementation they encounter.

.. seealso::

    `Draft 2019-09's release notes on format <https://json-schema.org/draft/2019-09/release-notes.html#format-vocabulary>`_

        for upstream details on the behavior of format and how it has changed
        in ``draft2019-09``

    `validating formats`

        for details on how to enable format validation

    `jsonschema.FormatChecker`

        the object which implements format validation


Can jsonschema be used to validate YAML, TOML, etc.?
----------------------------------------------------

Like most JSON Schema implementations, `jsonschema` doesn't actually deal directly with JSON at all (other than in relation to the :kw:`$ref` keyword, elaborated on below).

In other words as far as this library is concerned, schemas and instances are simply runtime Python objects.
The JSON object ``{}`` is simply the Python `dict` ``{}``, and a JSON Schema like ``{"type": "object", {"properties": {}}}`` is really an assertion about particular Python objects and their keys.

.. note::

   The :kw:`$ref` keyword is a single notable exception.

   Specifically, in the case where `jsonschema` is asked to resolve a remote reference, it has no choice but to assume that the remote reference is serialized as JSON, and to deserialize it using the `json` module.

   One cannot today therefore reference some remote piece of YAML and have it deserialized into Python objects by this library without doing some additional work.
   See `Resolving References to Schemas Written in YAML <referencing:Resolving References to Schemas Written in YAML>` for details.

In practice what this means for JSON-like formats like YAML and TOML is that indeed one can generally schematize and then validate them exactly as if they were JSON by simply first deserializing them using libraries like ``PyYAML`` or the like, and passing the resulting Python objects into functions within this library.

Beware however that there are cases where the behavior of the JSON Schema specification itself is only well-defined within the data model of JSON itself, and therefore only for Python objects that could have "in theory" come from JSON.
As an example, JSON supports only string-valued keys, whereas YAML supports additional types.
The JSON Schema specification does not deal with how to apply the :kw:`patternProperties` keyword to non-string properties.
The behavior of this library is therefore similarly not defined when presented with Python objects of this form, which could never have come from JSON.
In such cases one is recommended to first pre-process the data such that the resulting behavior is well-defined.
In the previous example, if the desired behavior is to transparently coerce numeric properties to strings, as Javascript might, then do the conversion explicitly before passing data to this library.


Why doesn't my schema's default property set the default on my instance?
------------------------------------------------------------------------

The basic answer is that the specification does not require that
:kw:`default` actually do anything.

For an inkling as to *why* it doesn't actually do anything, consider
that none of the other keywords modify the instance either. More
importantly, having :kw:`default` modify the instance can produce
quite peculiar things. It's perfectly valid (and perhaps even useful)
to have a default that is not valid under the schema it lives in! So an
instance modified by the default would pass validation the first time,
but fail the second!

Still, filling in defaults is a thing that is useful. `jsonschema`
allows you to `define your own validator classes and callables
<creating>`, so you can easily create an `jsonschema.protocols.Validator`
that does do default setting. Here's some code to get you started. (In
this code, we add the default properties to each object *before* the
properties are validated, so the default values themselves will need to
be valid under the schema.)

    .. testcode::

        from jsonschema import Draft202012Validator, validators


        def extend_with_default(validator_class):
            validate_properties = validator_class.VALIDATORS["properties"]

            def set_defaults(validator, properties, instance, schema):
                for property, subschema in properties.items():
                    if "default" in subschema:
                        instance.setdefault(property, subschema["default"])

                for error in validate_properties(
                    validator, properties, instance, schema,
                ):
                    yield error

            return validators.extend(
                validator_class, {"properties" : set_defaults},
            )


        DefaultValidatingValidator = extend_with_default(Draft202012Validator)


        # Example usage:
        obj = {}
        schema = {'properties': {'foo': {'default': 'bar'}}}
        # Note jsonschema.validate(obj, schema, cls=DefaultValidatingValidator)
        # will not work because the metaschema contains `default` keywords.
        DefaultValidatingValidator(schema).validate(obj)
        assert obj == {'foo': 'bar'}


See the above-linked document for more info on how this works,
but basically, it just extends the :kw:`properties` keyword on a
`jsonschema.validators.Draft202012Validator` to then go ahead and update
all the defaults.

.. note::

    If you're interested in a more interesting solution to a larger
    class of these types of transformations, keep an eye on `Seep
    <https://github.com/Julian/Seep>`_, which is an experimental
    data transformation and extraction library written on top of
    `jsonschema`.


.. hint::

    The above code can provide default values for an entire object and
    all of its properties, but only if your schema provides a default
    value for the object itself, like so:

    .. testcode::

        schema = {
            "type": "object",
            "properties": {
                "outer-object": {
                    "type": "object",
                    "properties" : {
                        "inner-object": {
                            "type": "string",
                            "default": "INNER-DEFAULT"
                        }
                    },
                    "default": {} # <-- MUST PROVIDE DEFAULT OBJECT
                }
            }
        }

        obj = {}
        DefaultValidatingValidator(schema).validate(obj)
        assert obj == {'outer-object': {'inner-object': 'INNER-DEFAULT'}}

    ...but if you don't provide a default value for your object, then
    it won't be instantiated at all, much less populated with default
    properties.

    .. testcode::

        del schema["properties"]["outer-object"]["default"]
        obj2 = {}
        DefaultValidatingValidator(schema).validate(obj2)
        assert obj2 == {} # whoops


How do jsonschema version numbers work?
---------------------------------------

``jsonschema`` tries to follow the `Semantic Versioning
<https://semver.org/>`_ specification.

This means broadly that no backwards-incompatible changes should be made
in minor releases (and certainly not in dot releases).

The full picture requires defining what constitutes a
backwards-incompatible change.

The following are simple examples of things considered public API,
and therefore should *not* be changed without bumping a major version
number:

    * module names and contents, when not marked private by Python
      convention (a single leading underscore)

    * function and object signature (parameter order and name)

The following are *not* considered public API and may change without
notice:

    * the exact wording and contents of error messages; typical reasons
      to rely on this seem to involve downstream tests in packages using
      `jsonschema`. These use cases are encouraged to use the extensive
      introspection provided in `jsonschema.exceptions.ValidationError`\s
      instead to make meaningful assertions about what failed rather than
      relying on *how* what failed is explained to a human.

    * the order in which validation errors are returned or raised

    * the contents of the ``jsonschema.tests`` package

    * the contents of the ``jsonschema.benchmarks`` package

    * the specific non-zero error codes presented by the command line
      interface

    * the exact representation of errors presented by the command line
      interface, other than that errors represented by the plain outputter
      will be reported one per line

    * anything marked private

With the exception of the last two of those, flippant changes are
avoided, but changes can and will be made if there is improvement to be
had. Feel free to open an issue ticket if there is a specific issue or
question worth raising.

---

### Index (`docs/index.rst`)

.. module:: jsonschema
   :noindex:

.. include:: ../README.rst


Contents
--------

.. toctree::
    :maxdepth: 2

    validate
    errors
    referencing
    creating
    faq
    api/index


Indices and tables
==================

* `genindex`

---

### Referencing (`docs/referencing.rst`)

=========================
JSON (Schema) Referencing
=========================

The JSON Schema :kw:`$ref` and :kw:`$dynamicRef` keywords allow schema authors to combine multiple schemas (or subschemas) together for reuse or deduplication.

The `referencing <referencing:index>` library was written in order to provide a simple, well-behaved and well-tested implementation of this kind of reference resolution [1]_.
It has its `own documentation which is worth reviewing <referencing:intro>`, but this page serves as an introduction which is tailored specifically to JSON Schema, and even more specifically to how to configure `referencing <referencing:index>` for use with `Validator` objects in order to customize the behavior of the :kw:`$ref` keyword and friends in your schemas.

Configuring `jsonschema` for custom referencing behavior is essentially a two step process:

    * Create a `referencing.Registry` object that behaves the way you wish

    * Pass the `referencing.Registry` to your `Validator` when instantiating it

The examples below essentially follow these two steps.

.. [1] One that in fact is independent of this `jsonschema` library itself, and may some day be used by other tools or implementations.


Introduction to the `referencing <referencing:index>` API
---------------------------------------------------------

There are 3 main objects to be aware of in the `referencing` API:

    * `referencing.Registry`, which represents a specific immutable set of JSON Schemas (either in-memory or retrievable)
    * `referencing.Specification`, which represents a specific *version* of the JSON Schema specification, which can have differing referencing behavior.
      JSON Schema-specific specifications live in the `referencing.jsonschema` module and are named like `referencing.jsonschema.DRAFT202012`.
    * `referencing.Resource`, which represents a specific JSON Schema (often a Python `dict`) *along* with a specific `referencing.Specification` it is to be interpreted under.

As a concrete example, the simple schema ``{"type": "integer"}`` may be interpreted as a schema under either Draft 2020-12 or Draft 4 of the JSON Schema specification (amongst others); in draft 2020-12, the float ``2.0`` must be considered an integer, whereas in draft 4, it potentially is not.
If you mean the former (i.e. to associate this schema with draft 2020-12), you'd use ``referencing.Resource(contents={"type": "integer"}, specification=referencing.jsonschema.DRAFT202012)``, whereas for the latter you'd use `referencing.jsonschema.DRAFT4`.

.. seealso:: the JSON Schema :kw:`$schema` keyword

    Which should generally be used to remove all ambiguity and identify *internally* to the schema what version it is written for.

A schema may be identified via one or more URIs, either because they contain an :kw:`$id` keyword (in suitable versions of the JSON Schema specification) which indicates their canonical URI, or simply because you wish to externally associate a URI with the schema, regardless of whether it contains an ``$id`` keyword.
You could add the aforementioned simple schema to a `referencing.Registry` by creating an empty registry and then identifying it via some URI:

.. testcode::

    from referencing import Registry, Resource
    from referencing.jsonschema import DRAFT202012
    schema = Resource(contents={"type": "integer"}, specification=DRAFT202012)
    registry = Registry().with_resource(uri="http://example.com/my/schema", resource=schema)
    print(registry)

.. testoutput::

   <Registry (1 uncrawled resource)>

.. note::

    `referencing.Registry` is an entirely immutable object.
    All of its methods which add schemas (resources) to itself return *new* registry objects containing the added schemas.

You could also confirm your schema is in the registry if you'd like, via `referencing.Registry.contents`, which will show you the contents of a resource at a given URI:

.. testcode::

   print(registry.contents("http://example.com/my/schema"))

.. testoutput::

   {'type': 'integer'}

For further details, see the `referencing documentation <referencing:intro>`.

Common Scenarios
----------------

.. _in-memory-schemas:

Making Additional In-Memory Schemas Available
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The most common scenario one is likely to encounter is the desire to include a small number of additional in-memory schemas, making them available for use during validation.

For instance, imagine the below schema for non-negative integers:

.. code:: json

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "integer",
      "minimum": 0
    }

We may wish to have other schemas we write be able to make use of this schema, and refer to it as ``http://example.com/nonneg-int-schema`` and/or as ``urn:nonneg-integer-schema``.

To do so we make use of APIs from the referencing library to create a `referencing.Registry` which maps the URIs above to this schema:

.. testcode::

    from referencing import Registry, Resource
    schema = Resource.from_contents(
        {
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "integer",
            "minimum": 0,
        },
    )
    registry = Registry().with_resources(
        [
            ("http://example.com/nonneg-int-schema", schema),
            ("urn:nonneg-integer-schema", schema),
        ],
    )

What's above is likely mostly self-explanatory, other than the presence of the `referencing.Resource.from_contents` function.
Its purpose is to convert a piece of "opaque" JSON (or really a Python `dict` containing deserialized JSON) into an object which indicates what *version* of JSON Schema the schema is meant to be interpreted under.
Calling it will inspect a :kw:`$schema` keyword present in the given schema and use that to associate the JSON with an appropriate `specification <referencing.Specification>`.
If your schemas do not contain ``$schema`` dialect identifiers, and you intend for them to be interpreted always under a specific dialect -- say Draft 2020-12 of JSON Schema -- you may instead use e.g.:

.. testcode::

    from referencing import Registry, Resource
    from referencing.jsonschema import DRAFT202012
    schema = DRAFT202012.create_resource({"type": "integer", "minimum": 0})
    registry = Registry().with_resources(
        [
            ("http://example.com/nonneg-int-schema", schema),
            ("urn:nonneg-integer-schema", schema),
        ],
    )

which has the same functional effect.

You can now pass this registry to your `Validator`, which allows a schema passed to it to make use of the aforementioned URIs to refer to our non-negative integer schema.
Here for instance is an example which validates that instances are JSON objects with non-negative integral values:

.. testcode::

    from jsonschema import Draft202012Validator
    validator = Draft202012Validator(
        {
            "type": "object",
            "additionalProperties": {"$ref": "urn:nonneg-integer-schema"},
        },
        registry=registry,  # the critical argument, our registry from above
    )
    validator.validate({"foo": 37})
    assert not validator.is_valid({"foo": -37})  # Uh oh!

.. _ref-filesystem:

Resolving References from the File System
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another common request from schema authors is to be able to map URIs to the file system, perhaps while developing a set of schemas in different local files.
If you have a set of *fixed* or *static* schemas in a few files, you still likely will want to follow the `above in-memory instructions <in-memory-schemas>`, and simply load all of your files by reading them in-memory from your program.
If however you wish to *dynamically* read files off of the file system, perhaps because they may change during the lifetime of your process, then the referencing library supports doing so fully dynamically by configuring a callable which can be used to retrieve any schema which is *not* already pre-loaded in-memory.

Here we resolve any schema beginning with ``http://localhost`` to a directory ``/tmp/schemas`` on the local filesystem (note of course that this will not work if run directly unless you have populated that directory with some schemas):

.. testcode::

    from pathlib import Path
    import json

    from referencing import Registry, Resource
    from referencing.exceptions import NoSuchResource

    SCHEMAS = Path("/tmp/schemas")

    def retrieve_from_filesystem(uri: str):
        if not uri.startswith("http://localhost/"):
            raise NoSuchResource(ref=uri)
        path = SCHEMAS / Path(uri.removeprefix("http://localhost/"))
        contents = json.loads(path.read_text())
        return Resource.from_contents(contents)

    registry = Registry(retrieve=retrieve_from_filesystem)

Such a registry can then be used with `Validator` objects in the same way shown above, and any such references to URIs which are not already in-memory will be retrieved from the configured directory.

We can mix the two examples above if we wish for some in-memory schemas to be available in addition to the filesystem schemas, e.g.:

.. testcode::

    from referencing.jsonschema import DRAFT7
    registry = Registry(retrieve=retrieve_from_filesystem).with_resource(
        "urn:non-empty-array", DRAFT7.create_resource({"type": "array", "minItems": 1}),
    )

where we've made use of the similar `referencing.Registry.with_resource` function to add a single additional resource.

Resolving References to Schemas Written in YAML
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Generalizing slightly, the retrieval function provided need not even assume that it is retrieving JSON.
As long as you deserialize what you have retrieved into Python objects, you may equally be retrieving references to YAML documents or any other format.

Here for instance we retrieve YAML documents in a way similar to the `above <ref-filesystem>` using PyYAML:

.. testcode::

    from pathlib import Path
    import yaml

    from referencing import Registry, Resource
    from referencing.exceptions import NoSuchResource

    SCHEMAS = Path("/tmp/yaml-schemas")

    def retrieve_yaml(uri: str):
        if not uri.startswith("http://localhost/"):
            raise NoSuchResource(ref=uri)
        path = SCHEMAS / Path(uri.removeprefix("http://localhost/"))
        contents = yaml.safe_load(path.read_text())
        return Resource.from_contents(contents)

    registry = Registry(retrieve=retrieve_yaml)

.. note::

    Not all YAML fits within the JSON data model.

    JSON Schema is defined specifically for JSON, and has well-defined behavior strictly for Python objects which could have possibly existed as JSON.

    If you stick to the subset of YAML for which this is the case then you shouldn't have issue, but if you pass schemas (or instances) around whose structure could never have possibly existed as JSON (e.g. a mapping whose keys are not strings), all bets are off.
    For an example on how to handle that, have a look at the `parsers/yaml.py <https://github.com/python-jsonschema/check-jsonschema/blob/main/src/check_jsonschema/parsers/yaml.py>`_ file in the check-jsonschema repository.

One could similarly imagine a retrieval function which switches on whether to call ``yaml.safe_load`` or ``json.loads`` by file extension (or some more reliable mechanism) and thereby support retrieving references of various different file formats.

.. _http:

Automatically Retrieving Resources Over HTTP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the general case, the JSON Schema specifications tend to `discourage <https://json-schema.org/draft/2020-12/json-schema-core.html#name-loading-a-referenced-schema>`_ implementations (like this one) from automatically retrieving references over the network, or even assuming such a thing is feasible (as schemas may be identified by URIs which are strictly identifiers, and not necessarily downloadable from the URI even when such a thing is sensical).

However, if you as a schema author are in a situation where you indeed do wish to do so for convenience (and understand the implications of doing so), you may do so by making use of the ``retrieve`` argument to `referencing.Registry`.

Here is how one would configure a registry to automatically retrieve schemas from the `JSON Schema Store <https://www.schemastore.org>`_ on the fly using the `httpx <https://www.python-httpx.org/>`_:

.. testcode::

    from referencing import Registry, Resource
    import httpx

    def retrieve_via_httpx(uri: str):
        response = httpx.get(uri)
        return Resource.from_contents(response.json())

    registry = Registry(retrieve=retrieve_via_httpx)

Given such a registry, we can now, for instance, validate instances against schemas from the schema store by passing the ``registry`` we configured to our `Validator` as in previous examples:

.. testsetup:: *

    import sys

    class FakeResource:
        def json(self):
            return {
                "$schema": "https://json-schema.org/draft/2020-12/schema",
                "not": True,
            }

    class FakeHTTPX:
        def get(self, uri):
            return FakeResource()

    sys.modules["httpx"] = FakeHTTPX()

.. testcode::

    from jsonschema import Draft202012Validator
    Draft202012Validator(
        {"$ref": "https://json.schemastore.org/pyproject.json"},
        registry=registry,
    ).validate({"project": {"name": 12}})

which should in this case indicate the example data is invalid:

.. testoutput::

    Traceback (most recent call last):
        ...
    jsonschema.exceptions.ValidationError: 12 is not of type 'string'

    Failed validating 'type' in schema['properties']['project']['properties']['name']:
        {'pattern': '^([a-zA-Z\\d]|[a-zA-Z\\d][\\w.-]*[a-zA-Z\\d])$',
        'title': 'Project name',
        'type': 'string'}

    On instance['project']['name']:
        12

Retrieving resources from a SQLite database or some other network-accessible resource should be more or less similar, replacing the HTTP client with one for your database of course.

.. warning::

    Be sure you understand the security implications of the reference resolution you configure.
    And if you accept untrusted schemas, doubly sure!

    You wouldn't want a user causing your machine to go off and retrieve giant files off the network by passing it a ``$ref`` to some huge blob, or exploiting similar vulnerabilities in your setup.


Migrating From ``RefResolver``
------------------------------

Older versions of `jsonschema` used a different object -- `_RefResolver` -- for reference resolution, which you a schema author may already be configuring for your own use.

`_RefResolver` is now fully deprecated and replaced by the use of `referencing.Registry` as shown in examples above.

If you are not already constructing your own `_RefResolver`, this change should be transparent to you (or even recognizably improved, as the point of the migration was to improve the quality of the referencing implementation and enable some new functionality).

.. table:: Rough equivalence between `_RefResolver` and `referencing.Registry` APIs
   :widths: auto

   ===========================================================  =====================================================================================================================
                             Old API                                                                                      New API
   ===========================================================  =====================================================================================================================
   ``RefResolver.from_schema({"$id": "urn:example:foo", ...}``  ``Registry().with_resource(uri="urn:example:foo", resource=Resource.from_contents({"$id": "urn:example:foo", ...}))``
   Overriding ``RefResolver.resolve_from_url``                  Passing a callable to `referencing.Registry`\ 's ``retrieve`` argument
   ``DraftNValidator(..., resolver=_RefResolver(...))``  ``     DraftNValidator(..., registry=Registry().with_resources(...))``
   ===========================================================  =====================================================================================================================


Here are some more specifics on how to migrate to the newer APIs:

The ``store`` argument
~~~~~~~~~~~~~~~~~~~~~~

`_RefResolver`\ 's ``store`` argument was essentially the equivalent of `referencing.Registry`\ 's in-memory schema storage.

If you currently pass a set of schemas via e.g.:

.. code-block:: python

    from jsonschema import Draft202012Validator, RefResolver
    resolver = RefResolver.from_schema(
        schema={"title": "my schema"},
        store={"http://example.com": {"type": "integer"}},
    )
    validator = Draft202012Validator(
        {"$ref": "http://example.com"},
        resolver=resolver,
    )
    validator.validate("foo")

you should be able to simply move to something like:

.. testcode::

    from referencing import Registry
    from referencing.jsonschema import DRAFT202012

    from jsonschema import Draft202012Validator

    registry = Registry().with_resource(
        "http://example.com",
        DRAFT202012.create_resource({"type": "integer"}),
    )
    validator = Draft202012Validator(
        {"$ref": "http://example.com"},
        registry=registry,
    )
    assert not validator.is_valid("foo")

Handlers
~~~~~~~~

The ``handlers`` functionality from `_RefResolver` was a way to support additional HTTP schemes for schema retrieval.

Here you should move to a custom ``retrieve`` function which does whatever you'd like.
E.g. in pseudocode:

.. testcode::

    from urllib.parse import urlsplit

    def retrieve(uri: str):
        parsed = urlsplit(uri)
        if parsed.scheme == "file":
            ...
        elif parsed.scheme == "custom":
            ...

    registry = Registry(retrieve=retrieve)


Other Key Functional Differences
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Whilst `_RefResolver` *did* automatically retrieve remote references (against the recommendation of the spec, and in a way which therefore could lead to questionable security concerns when combined with untrusted schemas), `referencing.Registry` does *not* do so.
If you rely on this behavior, you should follow the `above example of retrieving resources over HTTP <http>`.

---

### Spelling Wordlist (`docs/spelling-wordlist.txt`)

# this appears to be misinterpreting Napoleon types as prose, sigh...
Validator
TypeChecker
UnknownType
ValidationError

# 0th, sigh...
th
amongst
callables
# non-codeblocked cls from autoapi
cls
deque
deduplication
dereferences
deserialize
deserialized
deserializing
filesystem
hostname
implementers
indices
# ipv4/6, sigh...
ipv
iterable
iteratively
Javascript
jsonschema
majorly
metaschema
online
outputter
pre
programmatically
pseudocode
recurses
regex
repr
runtime
sandboxing
sensical
subclassing
submodule
submodules
subschema
subschemas
subscopes
untrusted
uri
validator
validators
versioned
schemas

Zac
HD

Berman
Libera
GPL

---

### Validate (`docs/validate.rst`)

=================
Schema Validation
=================


.. currentmodule:: jsonschema

.. tip::

   Most of the documentation for this package assumes you're familiar with the fundamentals of writing JSON schemas themselves, and focuses on how this library helps you validate with them in Python.

   If you aren't already comfortable with writing schemas and need an introduction which teaches about JSON Schema the specification, you may find :ujs:`Understanding JSON Schema </>` to be a good read!

The Basics
----------

The simplest way to validate an instance under a given schema is to use the `validate <jsonschema.validators.validate>` function.

.. autofunction:: validate
    :noindex:

.. warning::

   Accepting untrusted schemas as input, especially when combined with untrusted data to validate, can lead to vulnerabilities even when restricting to official JSON Schema dialects and vocabularies.
   Never validate data against schemas from untrusted sources without proper sandboxing or input validation.

.. _validator-protocol:

The Validator Protocol
----------------------

`jsonschema` defines a `protocol <typing.Protocol>` that all validator classes adhere to.

.. hint::

    If you are unfamiliar with protocols, either as a general notion or as specifically implemented by `typing.Protocol`, you can think of them as a set of attributes and methods that all objects satisfying the protocol have.

    Here, in the context of `jsonschema`, the `Validator.iter_errors` method can be called on `jsonschema.validators.Draft202012Validator`, or `jsonschema.validators.Draft7Validator`, or indeed any validator class, as all of them have it, along with all of the other methods described below.

.. autoclass:: jsonschema.protocols.Validator
    :noindex:
    :members:

All of the `versioned validators <versioned-validators>` that are included with `jsonschema` adhere to the protocol, and any `extensions of these validators <jsonschema.validators.extend>` will as well.
For more information on `creating <jsonschema.validators.create>` or `extending <jsonschema.validators.extend>` validators see `creating-validators`.

Type Checking
-------------

To handle JSON Schema's :kw:`type` keyword, a `Validator` uses
an associated `TypeChecker`. The type checker provides an immutable
mapping between names of types and functions that can test if an instance is
of that type. The defaults are suitable for most users - each of the
`versioned validators <versioned-validators>` that are included with
`jsonschema` have a `TypeChecker` that can correctly handle their respective
versions.

.. seealso:: `validating-types`

    For an example of providing a custom type check.

.. autoclass:: TypeChecker
    :members:
    :noindex:

.. autoexception:: jsonschema.exceptions.UndefinedTypeCheck
    :noindex:

    Raised when trying to remove a type check that is not known to this
    TypeChecker, or when calling `jsonschema.TypeChecker.is_type`
    directly.

.. _validating-types:

Validating With Additional Types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Occasionally it can be useful to provide additional or alternate types when
validating JSON Schema's :kw:`type` keyword.

`jsonschema` tries to strike a balance between performance in the common
case and generality. For instance, JSON Schema defines a ``number`` type, which
can be validated with a schema such as ``{"type" : "number"}``. By default,
this will accept instances of Python `numbers.Number`. This includes in
particular `int`\s and `float`\s, along with
`decimal.Decimal` objects, `complex` numbers etc. For
``integer`` and ``object``, however, rather than checking for
`numbers.Integral` and `collections.abc.Mapping`,
`jsonschema` simply checks for `int` and `dict`, since the
more general instance checks can introduce significant slowdown, especially
given how common validating these types are.

If you *do* want the generality, or just want to add a few specific additional
types as being acceptable for a validator object, then you should update an
existing `jsonschema.TypeChecker` or create a new one. You may then create a new
`Validator` via `jsonschema.validators.extend`.

.. testcode::

    from jsonschema import validators

    class MyInteger:
        pass

    def is_my_int(checker, instance):
        return (
            Draft202012Validator.TYPE_CHECKER.is_type(instance, "number") or
            isinstance(instance, MyInteger)
        )

    type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
        "number", is_my_int,
    )

    CustomValidator = validators.extend(
        Draft202012Validator,
        type_checker=type_checker,
    )
    validator = CustomValidator(schema={"type" : "number"})


.. autoexception:: jsonschema.exceptions.UnknownType
    :noindex:

.. _versioned-validators:

Versioned Validators
--------------------

`jsonschema` ships with validator classes for various versions of the JSON Schema specification.
For details on the methods and attributes that each validator class provides see the `Validator` protocol, which each included validator class implements.

Each of the below cover a specific release of the JSON Schema specification.

.. autoclass:: Draft202012Validator
    :noindex:

.. autoclass:: Draft201909Validator
    :noindex:

.. autoclass:: Draft7Validator
    :noindex:

.. autoclass:: Draft6Validator
    :noindex:

.. autoclass:: Draft4Validator
    :noindex:

.. autoclass:: Draft3Validator
    :noindex:


For example, if you wanted to validate a schema you created against the
Draft 2020-12 meta-schema, you could use:

.. testcode::

    from jsonschema import Draft202012Validator

    schema = {
        "$schema": Draft202012Validator.META_SCHEMA["$id"],

        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "email": {"type": "string"},
        },
        "required": ["email"]
    }
    Draft202012Validator.check_schema(schema)


.. _validating formats:

Validating Formats
------------------

JSON Schema defines the :kw:`format` keyword which can be used to check if primitive types (``string``\s, ``number``\s, ``boolean``\s) conform to well-defined formats.
By default, as per the specification, no validation is enforced.
Optionally however, validation can be enabled by hooking a `format-checking object <jsonschema.FormatChecker>` into a `Validator`.

.. doctest::

    >>> validate("127.0.0.1", {"format" : "ipv4"})
    >>> validate(
    ...     instance="-12",
    ...     schema={"format" : "ipv4"},
    ...     format_checker=Draft202012Validator.FORMAT_CHECKER,
    ... )
    Traceback (most recent call last):
        ...
    ValidationError: "-12" is not a "ipv4"


Some formats require additional dependencies to be installed.

The easiest way to ensure you have what is needed is to install ``jsonschema`` using the ``format`` or ``format-nongpl`` extras.

For example:

.. code:: sh

    $ pip install jsonschema[format]

Or if you want to avoid GPL dependencies, a second extra is available:

.. code:: sh

    $ pip install jsonschema[format-nongpl]

.. warning::

    It is your own responsibility ultimately to ensure you are license-compliant, so you should be double checking your own dependencies if you rely on this extra.

The more specific list of formats along with any additional dependencies they have is shown below.

.. warning::

    If a dependency is not installed when using a checker that requires it, validation will succeed without throwing an error, as also specified by the specification.

=========================  ====================
Checker                    Notes
=========================  ====================
``color``                  requires webcolors_
``date``
``date-time``              requires rfc3339-validator_
``duration``               requires isoduration_
``email``
``hostname``               requires fqdn_
``idn-hostname``           requires idna_
``ipv4``
``ipv6``                   OS must have `socket.inet_pton` function
``iri``                    requires rfc3987_ or rfc3987-syntax_
``iri-reference``          requires rfc3987_ or rfc3987-syntax_
``json-pointer``           requires jsonpointer_
``regex``
``relative-json-pointer``  requires jsonpointer_
``time``                   requires rfc3339-validator_
``uri``                    requires rfc3987_ or rfc3986-validator_
``uri-reference``          requires rfc3987_ or rfc3986-validator_
``uri-template``           requires uri-template_
``uuid``
=========================  ====================


.. _fqdn: https://pypi.org/pypi/fqdn/
.. _idna: https://pypi.org/pypi/idna/
.. _isoduration: https://pypi.org/pypi/isoduration/
.. _jsonpointer: https://pypi.org/pypi/jsonpointer/
.. _rfc3339-validator: https://pypi.org/project/rfc3339-validator/
.. _rfc3986-validator: https://pypi.org/project/rfc3986-validator/
.. _rfc3987: https://pypi.org/pypi/rfc3987/
.. _rfc3987-syntax: https://pypi.org/pypi/rfc3987-syntax/
.. _uri-template: https://pypi.org/pypi/uri-template/
.. _webcolors: https://pypi.org/pypi/webcolors/

The supported mechanism for ensuring these dependencies are present is again as shown above, not by directly installing the packages.

.. autoclass:: FormatChecker
    :members:
    :noindex:
    :exclude-members: cls_checks

    .. attribute:: checkers

        A mapping of currently known formats to tuple of functions that validate them and errors that should be caught.
        New checkers can be added and removed either per-instance or globally for all checkers using the `FormatChecker.checks` decorator.

    .. classmethod:: cls_checks(format, raises=())

        Register a decorated function as *globally* validating a new format.

        Any instance created after this function is called will pick up the supplied checker.

        :argument str format: the format that the decorated function will check
        :argument Exception raises: the exception(s) raised
            by the decorated function when an invalid instance is
            found. The exception object will be accessible as the
            `jsonschema.exceptions.ValidationError.cause` attribute
            of the resulting validation error.

        .. deprecated:: v4.14.0

            Use `FormatChecker.checks` on an instance instead.

.. autoexception:: jsonschema.exceptions.FormatError
    :noindex:
    :members:


Format-Specific Notes
~~~~~~~~~~~~~~~~~~~~~

regex
^^^^^

The JSON Schema specification `recommends (but does not require) <https://json-schema.org/draft/2020-12/json-schema-core.html#name-regular-expressions>`_ that implementations use ECMA 262 regular expressions.

Given that there is no current library in Python capable of supporting the ECMA 262 dialect, the ``regex`` format will instead validate *Python* regular expressions, which are the ones used by this implementation for other keywords like :kw:`pattern` or :kw:`patternProperties`.

email
^^^^^

Since in most cases "validating" an email address is an attempt instead to confirm that mail sent to it will deliver to a recipient, and that that recipient is the correct one the email is intended for, and since many valid email addresses are in many places incorrectly rejected, and many invalid email addresses are in many places incorrectly accepted, the ``email`` format keyword only provides a sanity check, not full :RFC:`5322` validation.

The same applies to the ``idn-email`` format.

If you indeed want a particular well-specified set of emails to be considered valid, you can use `FormatChecker.checks` to provide your specific definition.

---

### Api Index (`docs/api/index.rst`)

API Reference
=============

Submodules
----------

.. toctree::
   :titlesonly:

   /api/jsonschema/validators/index
   /api/jsonschema/exceptions/index
   /api/jsonschema/protocols/index

:mod:`jsonschema`
-----------------

.. automodule:: jsonschema
   :members:
   :imported-members:
   :exclude-members: FormatError, Validator, ValidationError

.. autodata:: jsonschema._format._FormatCheckCallable

.. autodata:: jsonschema._typing.id_of

---

### Api Jsonschema Exceptions Index (`docs/api/jsonschema/exceptions/index.rst`)

:py:mod:`jsonschema.exceptions`
===============================

.. automodule:: jsonschema.exceptions
   :members:
   :undoc-members:

---

### Api Jsonschema Protocols Index (`docs/api/jsonschema/protocols/index.rst`)

:py:mod:`jsonschema.protocols`
==============================

.. automodule:: jsonschema.protocols
   :members:
   :undoc-members:

---

### Api Jsonschema Validators Index (`docs/api/jsonschema/validators/index.rst`)

:py:mod:`jsonschema.validators`
===============================

.. automodule:: jsonschema.validators
   :members:
   :undoc-members:
   :private-members: _RefResolver

---

--- METRICS ---
- Files Extracted: 15
- Estimated Token Budget: ~21528 tokens
- Canonical Reference: https://codewiki.google/github.com/python-jsonschema/jsonschema
