Skip to content

Commit a73d1ef

Browse files
committed
Format can apply to instances of any type.
Closes #125
1 parent 11dd8a5 commit a73d1ef

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

jsonschema/_validators.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ def pattern(validator, patrn, instance, schema):
150150

151151

152152
def format(validator, format, instance, schema):
153-
if (
154-
validator.format_checker is not None and
155-
validator.is_type(instance, "string")
156-
):
153+
if validator.format_checker is not None:
157154
try:
158155
validator.format_checker.check(instance, format)
159156
except FormatError as error:

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ def test_it_validates_formats_if_a_checker_is_provided(self):
180180
# Make sure original cause is attached
181181
self.assertIs(cm.exception.cause, cause)
182182

183+
def test_it_validates_formats_of_any_type(self):
184+
checker = mock.Mock(spec=FormatChecker)
185+
validator = self.validator_class(
186+
{"format" : "foo"}, format_checker=checker,
187+
)
188+
189+
validator.validate([1, 2, 3])
190+
191+
checker.check.assert_called_once_with([1, 2, 3], "foo")
192+
193+
cause = ValueError()
194+
checker.check.side_effect = FormatError('aoeu', cause=cause)
195+
196+
with self.assertRaises(ValidationError) as cm:
197+
validator.validate([1, 2, 3])
198+
# Make sure original cause is attached
199+
self.assertIs(cm.exception.cause, cause)
200+
183201

184202
@load_json_cases("draft3/*.json", ignore_glob="draft3/refRemote.json")
185203
@load_json_cases(

0 commit comments

Comments
 (0)