Skip to content

Commit 8023267

Browse files
committed
Merge remote-tracking branch 'agoose77/fix_schema_id'
* agoose77/fix_schema_id: Fix bug in current registration of validators
2 parents a410674 + 7a8634e commit 8023267

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

jsonschema/tests/test_validators.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,19 @@ def test_if_a_version_is_not_provided_it_is_not_registered(self):
7676
validators.create(meta_schema={u"id": "id"})
7777
self.assertFalse(validates.called)
7878

79-
def test_if_validates_registers_meta_schema_id(self):
79+
def test_validates_registers_meta_schema_id(self):
8080
meta_schema_key = "meta schema id"
8181
my_meta_schema = {u"id": meta_schema_key}
8282

8383
validators.create(
8484
meta_schema=my_meta_schema,
8585
version="my version",
86+
id_of=lambda s: s.get("id", ""),
8687
)
8788

8889
self.assertIn(meta_schema_key, validators.meta_schemas)
8990

90-
def test_if_validates_registers_meta_schema_draft6_id(self):
91+
def test_validates_registers_meta_schema_draft6_id(self):
9192
meta_schema_key = "meta schema $id"
9293
my_meta_schema = {u"$id": meta_schema_key}
9394

@@ -1052,6 +1053,7 @@ def test_custom_validator(self):
10521053
Validator = validators.create(
10531054
meta_schema={"id": "meta schema id"},
10541055
version="12",
1056+
id_of=lambda s: s.get("id", ""),
10551057
)
10561058
schema = {"$schema": "meta schema id"}
10571059
self.assertIs(

jsonschema/validators.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ def validates(version):
3636
Register the decorated validator for a ``version`` of the specification.
3737
3838
Registered validators and their meta schemas will be considered when
39-
parsing ``$schema`` properties' URIs. Meta schemas can use either
40-
``id`` or ``$id`` depending on whether they follow pre-draft6 or draft6
41-
and later, respectively.
39+
parsing ``$schema`` properties' URIs.
4240
4341
Arguments:
4442
@@ -54,10 +52,9 @@ def validates(version):
5452

5553
def _validates(cls):
5654
validators[version] = cls
57-
if u"id" in cls.META_SCHEMA:
58-
meta_schemas[cls.META_SCHEMA[u"id"]] = cls
59-
elif u"$id" in cls.META_SCHEMA:
60-
meta_schemas[cls.META_SCHEMA[u"$id"]] = cls
55+
meta_schema_id = cls.ID_OF(cls.META_SCHEMA)
56+
if meta_schema_id:
57+
meta_schemas[meta_schema_id] = cls
6158
return cls
6259
return _validates
6360

@@ -208,6 +205,7 @@ class Validator(object):
208205
VALIDATORS = dict(validators)
209206
META_SCHEMA = dict(meta_schema)
210207
TYPE_CHECKER = type_checker
208+
ID_OF = staticmethod(id_of)
211209

212210
_DEFAULT_TYPES = dict(default_types)
213211

0 commit comments

Comments
 (0)