Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jsonschema/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def is_time(instance: object) -> bool:
return is_datetime("1970-01-01T" + instance)


@_checks_drafts(name="regex", raises=re.error)
@_checks_drafts(name="regex", raises=(re.error, ValueError))
def is_regex(instance: object) -> bool:
if not isinstance(instance, str):
return True
Expand Down
18 changes: 18 additions & 0 deletions jsonschema/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ def test_it_catches_registered_errors(self):
with self.assertRaises(type(BANG)):
checker.check(instance="bang", format="boom")

def test_regex_conflicting_inline_flags_report_invalid_not_error(self):
# re.compile raises ValueError (not an re.error subclass) for
# incompatible inline flags in separate groups, which must surface
# as a FormatError rather than escape the checker (#1558).
checker = FormatChecker(formats=["regex"])

with self.assertRaises(FormatError):
checker.check("(?u)(?a)", "regex")

self.assertFalse(checker.conforms("(?u)(?a)", "regex"))
self.assertFalse(checker.conforms("(?a)(?u)", "regex"))

# The single-group form was already correctly invalid.
self.assertFalse(checker.conforms("(?ua)", "regex"))
# And a plain re.error still reports invalid rather than raising.
self.assertFalse(checker.conforms("[unterminated", "regex"))
self.assertTrue(checker.conforms("a+b?", "regex"))

def test_format_error_causes_become_validation_error_causes(self):
checker = FormatChecker()
checker.checks("boom", raises=ValueError)(boom)
Expand Down