diff --git a/docs/spelling-wordlist.txt b/docs/spelling-wordlist.txt index 6a836fe9e..e43b09cef 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 077d1c536..c8286b558 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -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() @@ -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) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index a47c3aefa..a92e47548 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(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