Skip to content

Commit

Permalink
Enable format validation by default in check_schema.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Julian committed Nov 1, 2022
1 parent 2db9c58 commit 7448523
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
v4.17.0
=======

* The ``check_schema`` method on ``jsonschema.protocols.Validator`` instances
now *enables* format validation by default when run. This can catch some
additional invalid schemas (e.g. containing invalid regular expressions)
where the issue is indeed uncovered by validating against the metaschema
with format validation enabled as an assertion.
* The ``jsonschema`` CLI (along with ``jsonschema.cli`` the module) are now
deprecated. Use ``check-jsonschema`` instead, which can be installed via
``pip install check-jsonschema`` and found
Expand Down
10 changes: 10 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,16 @@ def test_enum_allows_non_unique_items(self):
else:
self.Validator.check_schema({"enum": [12, 12]})

def test_schema_with_invalid_regex(self):
with self.assertRaises(exceptions.SchemaError):
self.Validator.check_schema({"pattern": "*notaregex"})

def test_schema_with_invalid_regex_with_disabled_format_validation(self):
self.Validator.check_schema(
{"pattern": "*notaregex"},
format_checker=None,
)


class ValidatorTestMixin(MetaSchemaTestsMixin, object):
def test_it_implements_the_validator_protocol(self):
Expand Down
10 changes: 8 additions & 2 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,15 @@ def __attrs_post_init__(self):
)

@classmethod
def check_schema(cls, schema):
def check_schema(cls, schema, format_checker=_UNSET):
Validator = validator_for(cls.META_SCHEMA, default=cls)
for error in Validator(cls.META_SCHEMA).iter_errors(schema):
if format_checker is _UNSET:
format_checker = Validator.FORMAT_CHECKER
validator = Validator(
schema=cls.META_SCHEMA,
format_checker=format_checker,
)
for error in validator.iter_errors(schema):
raise exceptions.SchemaError.create_from(error)

def evolve(self, **changes):
Expand Down

0 comments on commit 7448523

Please sign in to comment.