Skip to content

[RefResolver]: updated docs on ref resolving to local files #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
Resolving JSON References
=========================

How to use the RefResolver
--------------------------

This section is intended for those who are having problems with using $ref to
load other schemas from within a given schema.

Loading schemas from within the filesystem requires setting a base uri for
the RefResolver. Otherwise, finding another schema relative to a first schema
will not work.

.. code-block:: python

import os
from jsonschema import Draft4Validator, RefResolver

def get_draft4_validator(schema_absolute_path):
"""
Return a draft4 validator with the base_uri
set to the schema path so that file refs are resolved
"""
with open(schema_absolute_path) as schema_file:
schema = json.load(schema_file)
resolver = RefResolver("file://" + schema_absolute_path, schema)
return Draft4Validator(schema, resolver=resolver)





.. currentmodule:: jsonschema

Expand Down