diff options
-rw-r--r-- | docs/spelling-wordlist.txt | 1 | ||||
-rw-r--r-- | jsonschema/tests/test_validators.py | 33 | ||||
-rw-r--r-- | jsonschema/validators.py | 6 |
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 |