Skip to content

Commit a410674

Browse files
committed
Merge remote-tracking branch 'robherring/draft6-id-fix'
* robherring/draft6-id-fix: Handle $id property for draft6 in validates()
2 parents 9632422 + fa39b24 commit a410674

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/spelling-wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ indices
1515
ipv
1616
iterable
1717
jsonschema
18+
pre
1819
programmatically
1920
recurses
2021
regex

jsonschema/tests/test_validators.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ 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):
80+
meta_schema_key = "meta schema id"
81+
my_meta_schema = {u"id": meta_schema_key}
82+
83+
validators.create(
84+
meta_schema=my_meta_schema,
85+
version="my version",
86+
)
87+
88+
self.assertIn(meta_schema_key, validators.meta_schemas)
89+
90+
def test_if_validates_registers_meta_schema_draft6_id(self):
91+
meta_schema_key = "meta schema $id"
92+
my_meta_schema = {u"$id": meta_schema_key}
93+
94+
validators.create(
95+
meta_schema=my_meta_schema,
96+
version="my version",
97+
)
98+
99+
self.assertIn(meta_schema_key, validators.meta_schemas)
100+
79101
def test_extend(self):
80102
original_validators = dict(self.Validator.VALIDATORS)
81103
new = mock.Mock()
@@ -1037,6 +1059,17 @@ def test_custom_validator(self):
10371059
Validator,
10381060
)
10391061

1062+
def test_custom_validator_draft6(self):
1063+
Validator = validators.create(
1064+
meta_schema={"$id": "meta schema $id"},
1065+
version="13",
1066+
)
1067+
schema = {"$schema": "meta schema $id"}
1068+
self.assertIs(
1069+
validators.validator_for(schema),
1070+
Validator,
1071+
)
1072+
10401073
def test_validator_for_jsonschema_default(self):
10411074
self.assertIs(validators.validator_for({}), validators._LATEST_VERSION)
10421075

jsonschema/validators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ 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.
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.
4042
4143
Arguments:
4244
@@ -54,6 +56,8 @@ def _validates(cls):
5456
validators[version] = cls
5557
if u"id" in cls.META_SCHEMA:
5658
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
5761
return cls
5862
return _validates
5963

0 commit comments

Comments
 (0)