File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change 1
1
from __future__ import absolute_import
2
2
import abc
3
3
import jsonschema
4
+ from jsonschema .validators import validator_for
4
5
from pyramid .httpexceptions import HTTPBadRequest
5
6
from ..compat import add_metaclass
6
- from ..utils import add_missing
7
7
8
8
9
9
class JSONValidationError (HTTPBadRequest ):
10
10
"""HTTP response for JSON validation errors.
11
11
"""
12
12
13
+
13
14
def validate (data , schema ):
14
15
"""Validate data against a JSON schema.
15
16
@@ -21,15 +22,12 @@ def validate(data, schema):
21
22
:raises pyramid.httpexceptions.HTTPBadRequest: if validation fails this
22
23
exception is raised to abort any further processing.
23
24
"""
24
- try :
25
- jsonschema .validate (data , schema ,
26
- format_checker = jsonschema .draft4_format_checker )
27
- except jsonschema .ValidationError as e :
28
- error = {
29
- '.' .join (str (p ) for p in e .path ): e .message
30
- }
31
- response = JSONValidationError (json = error )
32
- response .validation_error = e
25
+ validator = validator_for (schema )
26
+ errors = {}
27
+ for e in validator (schema ).iter_errors (data ):
28
+ errors ['.' .join (str (p ) for p in e .path )] = e .message
29
+ if errors :
30
+ response = JSONValidationError (json = errors )
33
31
raise response
34
32
35
33
Original file line number Diff line number Diff line change 6
6
7
7
class DummyResource (JsonSchemaValidationMixin , EditableResource ):
8
8
schema = {
9
- '$schema' : 'http://json-schema.org/draft-04/schema' ,
10
9
'type' : 'object' ,
11
10
'properties' : {
12
11
'email' : {
@@ -57,6 +56,15 @@ def test_array_validation_error():
57
56
}, partial = False )
58
57
59
58
59
+ def test_multiple_errors ():
60
+ resource = DummyResource ()
61
+ with pytest .raises (HTTPBadRequest ) as exc_info :
62
+ import pdb ; pdb .set_trace ()
63
+ resource .validate ({})
64
+ assert 'email' in exc_info .value .json
65
+ assert 'password' in exc_info .value .json
66
+
67
+
60
68
def test_partial_data ():
61
69
resource = DummyResource ()
62
70
resource .to_dict = lambda : {'password' : 'Jane' }
You can’t perform that action at this time.
0 commit comments