-
-
Notifications
You must be signed in to change notification settings - Fork 592
Add id with ref support, solve bug(371). #717
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import contextlib | ||
import json | ||
import numbers | ||
import copy | ||
|
||
from jsonschema import ( | ||
_legacy_validators, | ||
|
@@ -654,6 +655,7 @@ def __init__( | |
) | ||
self.store.update(store) | ||
self.store[base_uri] = referrer | ||
self.store_subschema(referrer, last_url=base_uri) | ||
|
||
self._urljoin_cache = urljoin_cache | ||
self._remote_cache = remote_cache | ||
|
@@ -862,6 +864,40 @@ def resolve_remote(self, uri): | |
self.store[uri] = result | ||
return result | ||
|
||
def store_subschema(self, schema, last_url=None): | ||
""" | ||
Using $id or id with $ref, save subschema to self.store | ||
|
||
Arguments: | ||
|
||
schema: | ||
|
||
The referring schema. | ||
|
||
last_url: | ||
|
||
The last URL. | ||
""" | ||
if not isinstance(schema, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will have a closer look, but the schema does not have to be a dict (it's an arbitrary collections.Mapping) and does not necessarily even need to be copy-able. |
||
return | ||
|
||
for k in schema.keys(): | ||
if k in [u"id", u"$id"] and isinstance(schema[k], str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
# Splicing the url in the last id with the url in this id, | ||
# and store last_url at the same time. | ||
last_url = urljoin(last_url, schema[k], allow_fragments=True) | ||
url, fragment = urldefrag(last_url) | ||
|
||
# Save the schema into self.store[url] | ||
self.store[url] = copy.deepcopy(schema) | ||
|
||
# Add fragment element in self.store[url] | ||
if fragment: | ||
self.store[url][fragment] = copy.deepcopy(schema) | ||
|
||
if isinstance(schema[k], dict): | ||
self.store_subschema(schema[k], last_url) | ||
|
||
|
||
def validate(instance, schema, cls=None, *args, **kwargs): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To start I'd not want this to be a public method / addition to the API.