|
@@ -44,22 +44,33 @@ class ProcessorInterface(object):
|
44
|
44
|
raise NotImplementedError
|
45
|
45
|
|
46
|
46
|
|
47
|
|
-class InputProcessor(ProcessorInterface):
|
|
47
|
+class Processor(ProcessorInterface):
|
|
48
|
+ @classmethod
|
|
49
|
+ def clean_data(cls, data: typing.Any) -> dict:
|
|
50
|
+ # Fixes #22: Schemas make not validation if None is given
|
|
51
|
+ if data is None:
|
|
52
|
+ return {}
|
|
53
|
+ return data
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+class InputProcessor(Processor):
|
48
|
57
|
pass
|
49
|
58
|
|
50
|
59
|
|
51
|
|
-class OutputProcessor(ProcessorInterface):
|
|
60
|
+class OutputProcessor(Processor):
|
52
|
61
|
pass
|
53
|
62
|
|
54
|
63
|
|
55
|
64
|
class MarshmallowOutputProcessor(OutputProcessor):
|
56
|
65
|
def process(self, data: typing.Any):
|
57
|
|
- data = self.schema.dump(data).data
|
58
|
|
- self.validate(data)
|
59
|
|
- return data
|
|
66
|
+ clean_data = self.clean_data(data)
|
|
67
|
+ dump_data = self.schema.dump(clean_data).data
|
|
68
|
+ self.validate(dump_data)
|
|
69
|
+ return dump_data
|
60
|
70
|
|
61
|
71
|
def validate(self, data: typing.Any) -> None:
|
62
|
|
- errors = self.schema.load(data).errors
|
|
72
|
+ clean_data = self.clean_data(data)
|
|
73
|
+ errors = self.schema.load(clean_data).errors
|
63
|
74
|
if errors:
|
64
|
75
|
raise OutputValidationException(
|
65
|
76
|
'Error when validate input: {}'.format(
|
|
@@ -68,8 +79,9 @@ class MarshmallowOutputProcessor(OutputProcessor):
|
68
|
79
|
)
|
69
|
80
|
|
70
|
81
|
def get_validation_error(self, data: dict) -> ProcessValidationError:
|
71
|
|
- data = self.schema.dump(data).data
|
72
|
|
- errors = self.schema.load(data).errors
|
|
82
|
+ clean_data = self.clean_data(data)
|
|
83
|
+ dump_data = self.schema.dump(clean_data).data
|
|
84
|
+ errors = self.schema.load(dump_data).errors
|
73
|
85
|
return ProcessValidationError(
|
74
|
86
|
message='Validation error of output data',
|
75
|
87
|
details=errors,
|
|
@@ -78,7 +90,8 @@ class MarshmallowOutputProcessor(OutputProcessor):
|
78
|
90
|
|
79
|
91
|
class MarshmallowInputProcessor(InputProcessor):
|
80
|
92
|
def process(self, data: dict):
|
81
|
|
- unmarshall = self.schema.load(data)
|
|
93
|
+ clean_data = self.clean_data(data)
|
|
94
|
+ unmarshall = self.schema.load(clean_data)
|
82
|
95
|
if unmarshall.errors:
|
83
|
96
|
raise OutputValidationException(
|
84
|
97
|
'Error when validate ouput: {}'.format(
|
|
@@ -89,9 +102,9 @@ class MarshmallowInputProcessor(InputProcessor):
|
89
|
102
|
return unmarshall.data
|
90
|
103
|
|
91
|
104
|
def get_validation_error(self, data: dict) -> ProcessValidationError:
|
92
|
|
- marshmallow_errors = self.schema.load(data).errors
|
|
105
|
+ clean_data = self.clean_data(data)
|
|
106
|
+ marshmallow_errors = self.schema.load(clean_data).errors
|
93
|
107
|
return ProcessValidationError(
|
94
|
108
|
message='Validation error of input data',
|
95
|
109
|
details=marshmallow_errors,
|
96
|
110
|
)
|
97
|
|
-
|