File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -67,16 +67,18 @@ def __unicode__(self):
6767 pinstance = pprint .pformat (self .instance , width = 72 )
6868 return self .message + textwrap .dedent ("""
6969
70- Failed validating %r in schema %s:
70+ Failed validating %r in %s %s:
7171 %s
7272
73- On instance %s:
73+ On %s %s:
7474 %s
7575 """ .rstrip ()
7676 ) % (
7777 self .validator ,
78+ self ._word_for_schema_in_error_message ,
7879 _utils .format_as_index (list (self .relative_schema_path )[:- 1 ]),
7980 _utils .indent (pschema ),
81+ self ._word_for_instance_in_error_message ,
8082 _utils .format_as_index (self .relative_path ),
8183 _utils .indent (pinstance ),
8284 )
@@ -125,11 +127,13 @@ def _contents(self):
125127
126128
127129class ValidationError (_Error ):
128- pass
130+ _word_for_schema_in_error_message = "schema"
131+ _word_for_instance_in_error_message = "instance"
129132
130133
131134class SchemaError (_Error ):
132- pass
135+ _word_for_schema_in_error_message = "metaschema"
136+ _word_for_instance_in_error_message = "schema"
133137
134138
135139class RefResolutionError (Exception ):
Original file line number Diff line number Diff line change 22from contextlib import contextmanager
33import json
44
5- from jsonschema import FormatChecker , ValidationError
5+ from jsonschema import FormatChecker , SchemaError , ValidationError
66from jsonschema .tests .compat import mock , unittest
77from jsonschema .validators import (
88 RefResolutionError , UnknownType , Draft3Validator ,
@@ -782,6 +782,22 @@ def test_draft4_validator_is_the_default(self):
782782 validate ({}, {})
783783 chk_schema .assert_called_once_with ({})
784784
785+ def test_validation_error_message (self ):
786+ with self .assertRaises (ValidationError ) as e :
787+ validate (12 , {"type" : "string" })
788+ self .assertRegexpMatches (
789+ str (e .exception ),
790+ "(?s)Failed validating u?'.*' in schema.*On instance" ,
791+ )
792+
793+ def test_schema_error_message (self ):
794+ with self .assertRaises (SchemaError ) as e :
795+ validate (12 , {"type" : 12 })
796+ self .assertRegexpMatches (
797+ str (e .exception ),
798+ "(?s)Failed validating u?'.*' in metaschema.*On schema" ,
799+ )
800+
785801
786802class TestRefResolver (unittest .TestCase ):
787803
You can’t perform that action at this time.
0 commit comments