Skip to content

Handle $id property for draft6 in validates() #417

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 1 commit into from
Jul 8, 2018
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
1 change: 1 addition & 0 deletions docs/spelling-wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ indices
ipv
iterable
jsonschema
pre
programmatically
recurses
regex
Expand Down
33 changes: 33 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ def test_if_a_version_is_not_provided_it_is_not_registered(self):
validators.create(meta_schema={u"id": "id"})
self.assertFalse(validates.called)

def test_if_validates_registers_meta_schema_id(self):
meta_schema_key = "meta schema id"
my_meta_schema = {u"id": meta_schema_key}

validators.create(
meta_schema=my_meta_schema,
version="my version",
)

self.assertIn(meta_schema_key, validators.meta_schemas)

def test_if_validates_registers_meta_schema_draft6_id(self):
meta_schema_key = "meta schema $id"
my_meta_schema = {u"$id": meta_schema_key}

validators.create(
meta_schema=my_meta_schema,
version="my version",
)

self.assertIn(meta_schema_key, validators.meta_schemas)

def test_extend(self):
original_validators = dict(self.Validator.VALIDATORS)
new = mock.Mock()
Expand Down Expand Up @@ -1025,6 +1047,17 @@ def test_custom_validator(self):
Validator,
)

def test_custom_validator_draft6(self):
Validator = validators.create(
meta_schema={"$id": "meta schema $id"},
version="13",
)
schema = {"$schema": "meta schema $id"}
self.assertIs(
validators.validator_for(schema),
Validator,
)

def test_validator_for_jsonschema_default(self):
self.assertIs(validators.validator_for({}), validators._LATEST_VERSION)

Expand Down
6 changes: 5 additions & 1 deletion jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def validates(version):
Register the decorated validator for a ``version`` of the specification.

Registered validators and their meta schemas will be considered when
parsing ``$schema`` properties' URIs.
parsing ``$schema`` properties' URIs. Meta schemas can use either
``id`` or ``$id`` depending on whether they follow pre-draft6 or draft6
and later, respectively.

Arguments:

Expand All @@ -54,6 +56,8 @@ def _validates(cls):
validators[version] = cls
if u"id" in cls.META_SCHEMA:
meta_schemas[cls.META_SCHEMA[u"id"]] = cls
elif u"$id" in cls.META_SCHEMA:
meta_schemas[cls.META_SCHEMA[u"$id"]] = cls
return cls
return _validates

Expand Down