summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@arm.com>2018-03-08 21:17:05 -0800
committerRob Herring <robh@kernel.org>2018-05-30 16:03:46 -0500
commitfa39b24e89ce524aec2435be0f86a046fe641a11 (patch)
tree66d3c4a85ea1bfd08d4b110a333e9db7ea59a993
parent6e4ab6d5b63d0d86b0a11f8f92da94d08db19869 (diff)
downloadjsonschema-fa39b24e89ce524aec2435be0f86a046fe641a11.tar.gz
Handle $id property for draft6 in validates()
Fix another spot where 'id' vs. '$id' needs to be handled for draft 6+. Signed-off-by: Grant Likely <grant.likely@arm.com> [robh: add testcases, re-word commit message] Signed-off-by: Rob Herring <robh@kernel.org>
-rw-r--r--docs/spelling-wordlist.txt1
-rw-r--r--jsonschema/tests/test_validators.py33
-rw-r--r--jsonschema/validators.py6
3 files changed, 39 insertions, 1 deletions
diff --git a/docs/spelling-wordlist.txt b/docs/spelling-wordlist.txt
index 6a836fe..e43b09c 100644
--- a/docs/spelling-wordlist.txt
+++ b/docs/spelling-wordlist.txt
@@ -15,6 +15,7 @@ indices
ipv
iterable
jsonschema
+pre
programmatically
recurses
regex
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index 077d1c5..c8286b5 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -76,6 +76,28 @@ class TestCreateAndExtend(TestCase):
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()
@@ -1025,6 +1047,17 @@ class TestValidatorFor(TestCase):
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)
diff --git a/jsonschema/validators.py b/jsonschema/validators.py
index a47c3ae..a92e475 100644
--- a/jsonschema/validators.py
+++ b/jsonschema/validators.py
@@ -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:
@@ -54,6 +56,8 @@ def validates(version):
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