summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2022-08-19 13:27:22 +0300
committerJulian Berman <Julian@GrayVines.com>2022-08-19 13:32:12 +0300
commita60a087884e63a6673bebbd403588958c5b047b3 (patch)
treece45f21a714176f745c97f84c29b8b8b4c39331f /jsonschema
parentedaf2a19c4a4915928cfe4e5073abb0f10321c59 (diff)
downloadjsonschema-a60a087884e63a6673bebbd403588958c5b047b3.tar.gz
Support validator classes whose metaschema uses a different dialect.v4.13.0
In other words, one may author validator classes (via jsonschema.validators.create or extend) whose meta schema defines schema behavior using JSON Schema draft2020-12 but whose schemas are in its own dialect. Said again differently, the set of valid schemas for a validator class may be governed by one draft, while the schema behavior itself is governed by another.
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/tests/test_validators.py32
-rw-r--r--jsonschema/validators.py3
2 files changed, 34 insertions, 1 deletions
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index d49dba0..ea7d3e4 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -197,6 +197,38 @@ class TestCreateAndExtend(TestCase):
),
)
+ def test_check_schema_with_different_metaschema(self):
+ """
+ One can create a validator class whose metaschema uses a different
+ dialect than itself.
+ """
+
+ NoEmptySchemasValidator = validators.create(
+ meta_schema={
+ "$schema": validators.Draft202012Validator.META_SCHEMA["$id"],
+ "not": {"const": {}},
+ },
+ )
+ NoEmptySchemasValidator.check_schema({"foo": "bar"})
+
+ with self.assertRaises(exceptions.SchemaError):
+ NoEmptySchemasValidator.check_schema({})
+
+ NoEmptySchemasValidator({"foo": "bar"}).validate("foo")
+
+ def test_check_schema_with_different_metaschema_defaults_to_self(self):
+ """
+ A validator whose metaschema doesn't declare $schema defaults to its
+ own validation behavior, not the latest "normal" specification.
+ """
+
+ NoEmptySchemasValidator = validators.create(
+ meta_schema={"fail": [{"message": "Meta schema whoops!"}]},
+ validators={"fail": fail},
+ )
+ with self.assertRaises(exceptions.SchemaError):
+ NoEmptySchemasValidator.check_schema({})
+
def test_extend(self):
original = dict(self.Validator.VALIDATORS)
new = object()
diff --git a/jsonschema/validators.py b/jsonschema/validators.py
index abdcb4c..39e2990 100644
--- a/jsonschema/validators.py
+++ b/jsonschema/validators.py
@@ -215,7 +215,8 @@ def create(
@classmethod
def check_schema(cls, schema):
- for error in cls(cls.META_SCHEMA).iter_errors(schema):
+ Validator = validator_for(cls.META_SCHEMA, default=cls)
+ for error in Validator(cls.META_SCHEMA).iter_errors(schema):
raise exceptions.SchemaError.create_from(error)
def evolve(self, **changes):