sphinx-jsonschema
This package contains sphinx-jsonschema, an extension to Sphinx to allow authors to display a JSON Schema in their documentation.
It arose out of a personal itch and implements what I needed. Some features of JSON Schema are (not yet) implemented. Also I can imagine that other display layouts are desired.
Let me know in comments and perhaps pull requests.
Features
Near complete support for all features of JSON Schema Draft 4, and support for the keywords of the later drafts up to Draft 2020-12 (see docs/schemaversions.rst for the list of keywords and the draft that introduced them).
Supports inline schemas as well as external schemas loaded from a file or URL.
Supports JSON Pointer notation on external resources to select a subschema.
Supports cross references between schemas.
Allows reStructuredText markup in title and description fields.
Allows JSON Schema definitions in both JSON and YAML format.
Supports the examples keyword from Draft 6.
Installation
Install the package using pip:
pip install sphinx-jsonschema
and add it to the extensions list in your conf.py:
extensions = [
'sphinx-jsonschema'
]
Usage
The extension adds a single directive to Sphinx: jsonschema. You provide it with either an http URL to a schema or you may embed the schema inline.
Example
Display a schema fetched from a website:
.. jsonschema:: http://some.domain/with/a/path/spec.json
Display a schema located in a file with an absolute path:
.. jsonschema:: /home/leo/src/jsonschema/sample.json
A path relative to the referencing document:
.. jsonschema:: jsonschema/sample.json
Or a schema defined in a Python object:
.. jsonschema:: mod.pkg.SCHEMA
With all three of the above you may add JSON Pointer notation to display a subschema:
.. jsonschema:: http://some.domain/with/a/path/spec.json#/path/to/schema .. jsonschema:: /home/leo/src/jsonschema/sample.json#/path/to/schema .. jsonschema:: jsonschema/sample.json#/path/to/schema .. jsonschema:: mod.pkg.SCHEMA#/path/to/schema
Alternatively you can embed the schema:
.. jsonschema::
{
"$schema": "This field is ignored for now. Perhaps use it to indicate schema version in display?",
"title": "Test data set 1: **Simple type**",
"id": "http://this.better.be.a.regular.domain",
"description": "These data sets exercise `JSON Schema <http://json-schema.org>`_ constructions and show how they are rendered.\n\nNote that it is possible to embed reStructuredText elements in strings.",
"type": "string",
"minLength": 10,
"maxLength": 100,
"pattern": "^[A-Z]+$"
}
This notation does not support JSON Pointer.
JSON Schema extension
- $$target
sphinx-jsonschema extends JSON Schema with the $$target key.
This key is only recognized at the outermost object of the schema.
JSON Schema uses the $ref key in combination with the $id key to cross-reference between schemas.
Sphinx-jsonschema shows the $id of the outermost schema as the first row of the table but does not use it to resolve references. It uses the value of $ref to create a reStructuredText :ref: role.
For this to work you need to mark the target schema with the $$target key, the value of which must be identical to the value of the corresponding $ref key.
So a schema:
{
"title": "Schema 1",
"$ref": "#/definitions/schema2"
}
will have its $ref replaced by a link pointing to:
{
"title": "Schema 2",
"$$target": "#/definitions/schema2"
...
}
Occasionally a schema will be addressed from several other schemas using different $ref values. In that case the value of $$target should be a list enumerating all different references to the schema.
- $$description
sphinx-jsonschema extends JSON Schema with the $$description key.
This key serves the same purpose as the description key and can be used in the same way. It differs from description in that it allows an array of strings as value instead of a single string.
This allows you to write:
{
...
"description": "+------------+------------+-----------+ \n| Header 1 | Header 2 | Header 3 | \n+============+============+===========+ \n| body row 1 | column 2 | column 3 | \n+------------+------------+-----------+ \n| body row 2 | Cells may span columns.| \n+------------+------------+-----------+ \n| body row 3 | Cells may | - Cells | \n+------------+ span rows. | - contain | \n| body row 4 | | - blocks. | \n+------------+------------+-----------+",
...
}
as:
{
...
"$$description": [
"+------------+------------+-----------+",
"| Header 1 | Header 2 | Header 3 |",
"+============+============+===========+",
"| body row 1 | column 2 | column 3 |",
"+------------+------------+-----------+",
"| body row 2 | Cells may span columns.|",
"+------------+------------+-----------+",
"| body row 3 | Cells may | - Cells |",
"+------------+ span rows. | - contain |",
"| body row 4 | | - blocks. |",
"+------------+------------+-----------+"
],
...
}
Which clearly is much more readable and maintainable.
Licence
Copyright Leo Noordergraaf, All rights reserved.
This software is made available under the GPL v3.
Changelog
see docs/changelog.rst
Metadata
Release files for sphinx-jsonschema 1.20.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sphinx_jsonschema-1.20.1.tar.gz | 29.7 kB | Without provenance | Details for sphinx_jsonschema-1.20.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sphinx_jsonschema-1.20.1-py3-none-any.whl | Python 3 | none | any | Details for sphinx_jsonschema-1.20.1-py3-none-any.whl |
Total release size: 58.3 kB
Release files / sphinx_jsonschema-1.20.1.tar.gz
| Download URL | sphinx_jsonschema-1.20.1.tar.gz |
|---|---|
| Size | 29.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
56d84e2051d7221d9089f11d113abe45581aa5e1a3e9c2bf99f96f5e5696b4f2
|
|
BLAKE2b-256 checksum How to use checksums |
667ec6632d91066e2d68ae12f5ef78fa369a8ad77c6498f2ceed8aa766616758
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.7
|
Release files / sphinx_jsonschema-1.20.1-py3-none-any.whl
| Download URL | sphinx_jsonschema-1.20.1-py3-none-any.whl |
|---|---|
| Size | 28.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc78a68213be2e13c7d4c4bff3231c6389c8f53e3fedd0016d047d4f41821e5c
|
|
BLAKE2b-256 checksum How to use checksums |
6ff0deaa6acea49c5faefd7ffea0cde0eeebf3635d5393d3aaecc7b9baacb5a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.7
|
Security for sphinx-jsonschema 1.20.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.