Skip to main content

Build Status

JSONSchema Ref Dict

Python dict-like object which abstracts resolution of JSONSchema references.

from json_ref_dict import RefDict

schema = RefDict("https://json-schema.org/draft-07/schema#/")

Nested items containing "$ref" will be resolved lazily when accessed, meaning the dictionary can be treating as a single, continuous (and possibly infinite) document.

Remote references are supported, and will be resolved relative to the current document.

If no scheme is provided, it is assumed that the document is present on the local filesystem (see Example below).

If PyYAML is installed, then loading of YAML documents will be supported, otherwise only JSON documents may be loaded.

Example

Given the following related schemas:

master.yaml

definitions:
  foo:
    type: string
  local_ref:
    $ref: '#/definitions/foo'
  remote_ref:
    $ref: 'other.yaml#/definitions/bar'
  backref:
    $ref: 'other.yaml#/definitions/baz'

other.yaml

definitions:
  bar:
    type: integer
  baz:
    $ref: 'master.yaml#/definitions/foo'

We can parse these as a single object as follows:

from json_ref_dict import RefDict

schema = RefDict("master.yaml#/definitions")
print(schema)
>>> {'foo': {'type': 'string'}, 'local_ref': {'$ref': '#/definitions/foo'}, 'remote_ref': {'$ref': 'other.yaml#/definitions/bar'}, 'backref': {'$ref': 'other.yaml#/definitions/baz'}}

print(schema["local_ref"])
>>> {'type': 'string'}

print(schema["remote_ref"])
>>> {'type': 'integer'}

print(schema["backref"])
>>> {'type': 'string'}

Materializing documents

If you don't want the lazy behaviour, and want to get all of the IO out of the way up front, then you can "materialize" the dictionary:

from json_ref_dict import materialize, RefDict

schema = materialize(RefDict("https://json-schema.org/draft-04/schema#/"))
assert isinstance(schema, dict)

A materialized RefDict is just a regular dict, containing a document with all references resolved. This is useful if, for example, you want to cache/persist the entire schema. Be aware that if there are cyclical references in the schema, these will be present on the materialized dictionary.

The materialize helper also supports some basic transformation options, as performing global transformations on infinite documents is non-trivial:

  • include_keys - an iterable of keys to include in the materialized document.
  • exclude_keys - an iterable of keys to exclude from the materialized document.
  • value_map - an operation to apply to the values of the document (not lists or dictionaries).

Requirements

This package is currently tested for Python 3.6.

Installation

This project may be installed using pip:

pip install json-ref-dict

Development

  1. Clone the repository: git clone git@github.com:jacksmith15/json-ref-dict.git && cd json-ref-dict
  2. Install the requirements: pip install -r requirements.txt -r requirements-test.txt
  3. Run pre-commit install
  4. Run the tests: bash run_test.sh -c -a

This project uses the following QA tools:

  • PyTest - for running unit tests.
  • PyLint - for enforcing code style.
  • MyPy - for static type checking.
  • Travis CI - for continuous integration.
  • Black - for uniform code formatting.

License

This project is distributed under the MIT license.

Metadata

Release files for json-ref-dict 0.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-ref-dict 0.7.2
File Size Uploaded
json-ref-dict-0.7.2.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-ref-dict 0.7.2
File Interpreter ABI Platform
json_ref_dict-0.7.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.0 kB

Release files / json-ref-dict-0.7.2.tar.gz

Download URL json-ref-dict-0.7.2.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
33d327c90a348838b94903402fa452905c284c64033b17d7a94cb5bc767a7518
BLAKE2b-256 checksum
How to use checksums
d823ee62caa62e1d7cfbfda5a00ba7766d95324136c1cb20988ea0b2d47779b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / json_ref_dict-0.7.2-py3-none-any.whl

Download URL json_ref_dict-0.7.2-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6024c659d999fae5efdc37719e91c5c0c3695fee16c44c2a736fd97f1a4ce4c2
BLAKE2b-256 checksum
How to use checksums
ccd4bfe0831f412a262a3a921dbdd610cd939af2062ee07f8d5dbb393622ae19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page