Skip to main content

Python dict-like object which abstracts resolution of JSONSchema references

Project description

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.

Project details


Download files

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

Source Distribution

json-ref-dict-0.6.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_ref_dict-0.6.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file json-ref-dict-0.6.0.tar.gz.

File metadata

  • Download URL: json-ref-dict-0.6.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for json-ref-dict-0.6.0.tar.gz
Algorithm Hash digest
SHA256 dec26cfd524d7facb02341ad6d69118e9f7bcf1d3224d454f52d9a0011fb675b
MD5 33da192c2e701e0797c4947afb6a4d68
BLAKE2b-256 903f33365ac60c2d1d31fda288125d5f27bad7a23ce5bf68c4ba872d60e86fe2

See more details on using hashes here.

File details

Details for the file json_ref_dict-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: json_ref_dict-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for json_ref_dict-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d89fbcc99abf46f81300bbb781d035cbed59a7cb40019a2fc0b1782698ac7833
MD5 d1ce8bde051ad35f7c89c1cbdb765507
BLAKE2b-256 99b4e70a19b1802ee6c608175649b81519e074881e55a9a8d846512a8f52e684

See more details on using hashes here.

Supported by

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