Skip to content

Make sure empty fragment and no fragment are the same in $schema #77

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

Merged
Merged
Show file tree
Hide file tree
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
45 changes: 42 additions & 3 deletions jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import socket
import sys

try:
from collections import MutableMapping
except ImportError:
from collections.abc import MutableMapping

try:
import requests
except ImportError:
Expand All @@ -48,7 +53,6 @@

FLOAT_TOLERANCE = 10 ** -15
validators = {}
meta_schemas = {}


class _Error(Exception):
Expand All @@ -68,6 +72,41 @@ class RefResolutionError(Exception): pass
class UnknownType(Exception): pass


class _URIDict(MutableMapping):
"""
Dictionary which uses normalized URIs as keys.

"""

def normalize(self, uri):
return urlparse.urlsplit(uri).geturl()

def __init__(self, *args, **kwargs):
self.store = dict()
self.store.update(*args, **kwargs)

def __getitem__(self, uri):
return self.store[self.normalize(uri)]

def __setitem__(self, uri, value):
self.store[self.normalize(uri)] = value

def __delitem__(self, uri):
del self.store[self.normalize(uri)]

def __iter__(self):
return iter(self.store)

def __len__(self):
return len(self.store)

def __repr__(self):
return repr(self.store)


meta_schemas = _URIDict()


def validates(version):
"""
Register the decorated validator for a ``version`` of the specification.
Expand Down Expand Up @@ -919,7 +958,7 @@ def __init__(self, base_uri, referrer, store=(), cache_remote=True,
self.base_uri = base_uri
self.resolution_scope = base_uri
self.referrer = referrer
self.store = dict(store, **_meta_schemas())
self.store = _URIDict(store, **_meta_schemas())
self.cache_remote = cache_remote
self.handlers = dict(handlers)

Expand Down Expand Up @@ -1235,6 +1274,6 @@ def _uniq(container):

def validate(instance, schema, cls=None, *args, **kwargs):
if cls is None:
cls = meta_schemas.get(schema.get("$schema"), Draft4Validator)
cls = meta_schemas.get(schema.get("$schema", ""), Draft4Validator)
cls.check_schema(schema)
cls(schema, *args, **kwargs).validate(instance)
5 changes: 5 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ def test_draft3_validator_is_chosen(self):
with mock.patch.object(Draft3Validator, "check_schema") as chk_schema:
validate({}, schema)
chk_schema.assert_called_once_with(schema)
# Make sure it works without the empty fragment
schema = {"$schema" : "http://json-schema.org/draft-03/schema"}
with mock.patch.object(Draft3Validator, "check_schema") as chk_schema:
validate({}, schema)
chk_schema.assert_called_once_with(schema)

def test_draft4_validator_is_chosen(self):
schema = {"$schema" : "http://json-schema.org/draft-04/schema#"}
Expand Down