Skip to content

Commit 7448523

Browse files
committed
Enable format validation by default in check_schema.
This catches some additional schema problems (ones caught by format validation in the metaschema, which was previously treated as annotation-only in check_schema). Closes: #904
1 parent 2db9c58 commit 7448523

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
v4.17.0
22
=======
33

4+
* The ``check_schema`` method on ``jsonschema.protocols.Validator`` instances
5+
now *enables* format validation by default when run. This can catch some
6+
additional invalid schemas (e.g. containing invalid regular expressions)
7+
where the issue is indeed uncovered by validating against the metaschema
8+
with format validation enabled as an assertion.
49
* The ``jsonschema`` CLI (along with ``jsonschema.cli`` the module) are now
510
deprecated. Use ``check-jsonschema`` instead, which can be installed via
611
``pip install check-jsonschema`` and found

jsonschema/tests/test_validators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,16 @@ def test_enum_allows_non_unique_items(self):
14831483
else:
14841484
self.Validator.check_schema({"enum": [12, 12]})
14851485

1486+
def test_schema_with_invalid_regex(self):
1487+
with self.assertRaises(exceptions.SchemaError):
1488+
self.Validator.check_schema({"pattern": "*notaregex"})
1489+
1490+
def test_schema_with_invalid_regex_with_disabled_format_validation(self):
1491+
self.Validator.check_schema(
1492+
{"pattern": "*notaregex"},
1493+
format_checker=None,
1494+
)
1495+
14861496

14871497
class ValidatorTestMixin(MetaSchemaTestsMixin, object):
14881498
def test_it_implements_the_validator_protocol(self):

jsonschema/validators.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,15 @@ def __attrs_post_init__(self):
215215
)
216216

217217
@classmethod
218-
def check_schema(cls, schema):
218+
def check_schema(cls, schema, format_checker=_UNSET):
219219
Validator = validator_for(cls.META_SCHEMA, default=cls)
220-
for error in Validator(cls.META_SCHEMA).iter_errors(schema):
220+
if format_checker is _UNSET:
221+
format_checker = Validator.FORMAT_CHECKER
222+
validator = Validator(
223+
schema=cls.META_SCHEMA,
224+
format_checker=format_checker,
225+
)
226+
for error in validator.iter_errors(schema):
221227
raise exceptions.SchemaError.create_from(error)
222228

223229
def evolve(self, **changes):

0 commit comments

Comments
 (0)