17
17
from jsonschema .tests ._helpers import bug
18
18
19
19
20
- def startswith (validator , startswith , instance , schema ):
21
- if not instance .startswith (startswith ):
22
- yield exceptions .ValidationError ("Whoops!" )
20
+ def fail (validator , errors , instance , schema ):
21
+ for each in errors :
22
+ each .setdefault ("message" , "You told me to fail!" )
23
+ yield exceptions .ValidationError (** each )
23
24
24
25
25
26
class TestCreateAndExtend (SynchronousTestCase ):
@@ -31,7 +32,7 @@ def setUp(self):
31
32
)
32
33
33
34
self .meta_schema = {"$id" : "some://meta/schema" }
34
- self .validators = {"startswith " : startswith }
35
+ self .validators = {"fail " : fail }
35
36
self .type_checker = TypeChecker ()
36
37
self .Validator = validators .create (
37
38
meta_schema = self .meta_schema ,
@@ -53,29 +54,46 @@ def test_attrs(self):
53
54
)
54
55
55
56
def test_init (self ):
56
- schema = {"startswith " : "foo" }
57
+ schema = {"fail " : [] }
57
58
self .assertEqual (self .Validator (schema ).schema , schema )
58
59
59
- def test_iter_errors (self ):
60
- schema = {"startswith " : "hel" }
61
- iter_errors = self .Validator (schema ). iter_errors
60
+ def test_iter_errors_successful (self ):
61
+ schema = {"fail " : [] }
62
+ validator = self .Validator (schema )
62
63
63
- errors = list (iter_errors ("hello" ))
64
+ errors = list (validator . iter_errors ("hello" ))
64
65
self .assertEqual (errors , [])
65
66
67
+ def test_iter_errors_one_error (self ):
68
+ schema = {"fail" : [{"message" : "Whoops!" }]}
69
+ validator = self .Validator (schema )
70
+
66
71
expected_error = exceptions .ValidationError (
67
72
"Whoops!" ,
68
73
instance = "goodbye" ,
69
74
schema = schema ,
70
- validator = "startswith " ,
71
- validator_value = "hel" ,
72
- schema_path = deque (["startswith " ]),
75
+ validator = "fail " ,
76
+ validator_value = [{ "message" : "Whoops!" }] ,
77
+ schema_path = deque (["fail " ]),
73
78
)
74
79
75
- errors = list (iter_errors ("goodbye" ))
80
+ errors = list (validator . iter_errors ("goodbye" ))
76
81
self .assertEqual (len (errors ), 1 )
77
82
self .assertEqual (errors [0 ]._contents (), expected_error ._contents ())
78
83
84
+ def test_iter_errors_multiple_errors (self ):
85
+ schema = {
86
+ "fail" : [
87
+ {"message" : "First" },
88
+ {"message" : "Second!" , "validator" : "asdf" },
89
+ {"message" : "Third" },
90
+ ],
91
+ }
92
+ validator = self .Validator (schema )
93
+
94
+ errors = list (validator .iter_errors ("goodbye" ))
95
+ self .assertEqual (len (errors ), 3 )
96
+
79
97
def test_if_a_version_is_provided_it_is_registered (self ):
80
98
Validator = validators .create (
81
99
meta_schema = {"$id" : "something" },
@@ -216,40 +234,6 @@ def id_of(schema):
216
234
self .assertEqual (Derived .ID_OF (Derived .META_SCHEMA ), correct_id )
217
235
218
236
219
- class TestIterErrors (TestCase ):
220
- def setUp (self ):
221
- self .validator = validators .Draft3Validator ({})
222
-
223
- def test_iter_errors (self ):
224
- instance = [1 , 2 ]
225
- schema = {
226
- "disallow" : "array" ,
227
- "enum" : [["a" , "b" , "c" ], ["d" , "e" , "f" ]],
228
- "minItems" : 3 ,
229
- }
230
-
231
- got = (e .message for e in self .validator .iter_errors (instance , schema ))
232
- expected = [
233
- f"{ schema ['disallow' ]!r} is disallowed for [1, 2]" ,
234
- "[1, 2] is too short" ,
235
- f"[1, 2] is not one of { schema ['enum' ]} " ,
236
- ]
237
- self .assertEqual (sorted (got ), sorted (expected ))
238
-
239
- def test_iter_errors_multiple_failures_one_validator (self ):
240
- instance = {"foo" : 2 , "bar" : [1 ], "baz" : 15 , "quux" : "spam" }
241
- schema = {
242
- "properties" : {
243
- "foo" : {"type" : "string" },
244
- "bar" : {"minItems" : 2 },
245
- "baz" : {"maximum" : 10 , "enum" : [2 , 4 , 6 , 8 ]},
246
- },
247
- }
248
-
249
- errors = list (self .validator .iter_errors (instance , schema ))
250
- self .assertEqual (len (errors ), 4 )
251
-
252
-
253
237
class TestValidationErrorMessages (TestCase ):
254
238
def message_for (self , instance , schema , * args , ** kwargs ):
255
239
cls = kwargs .pop ("cls" , validators ._LATEST_VERSION )
0 commit comments