Skip to content

Commit faa8164

Browse files
committed
Request and response data validation process refac
1 parent 7da1ea6 commit faa8164

File tree

2 files changed

+54
-60
lines changed

2 files changed

+54
-60
lines changed

openapi_core/validation/request/validators.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ def _get_parameters(self, request, params):
114114
except OpenAPIParameterError as exc:
115115
errors.append(exc)
116116
continue
117-
else:
118-
try:
119-
casted = self._cast(param, deserialised)
120-
except CastError as exc:
121-
errors.append(exc)
122-
continue
117+
118+
try:
119+
casted = self._cast(param, deserialised)
120+
except CastError as exc:
121+
errors.append(exc)
122+
continue
123123

124124
try:
125125
unmarshalled = self._unmarshal(param, casted)
@@ -132,38 +132,35 @@ def _get_parameters(self, request, params):
132132
return RequestParameters(**locations), errors
133133

134134
def _get_body(self, request, operation):
135-
errors = []
136-
137135
if operation.request_body is None:
138-
return None, errors
136+
return None, []
139137

140-
body = None
141138
try:
142139
media_type = operation.request_body[request.mimetype]
143140
except InvalidContentType as exc:
144-
errors.append(exc)
145-
else:
146-
try:
147-
raw_body = operation.request_body.get_value(request)
148-
except MissingRequestBody as exc:
149-
errors.append(exc)
150-
else:
151-
try:
152-
deserialised = self._deserialise(media_type, raw_body)
153-
except InvalidMediaTypeValue as exc:
154-
errors.append(exc)
155-
else:
156-
try:
157-
casted = self._cast(media_type, deserialised)
158-
except CastError as exc:
159-
errors.append(exc)
160-
else:
161-
try:
162-
body = self._unmarshal(media_type, casted)
163-
except (ValidateError, UnmarshalError) as exc:
164-
errors.append(exc)
165-
166-
return body, errors
141+
return None, [exc, ]
142+
143+
try:
144+
raw_body = operation.request_body.get_value(request)
145+
except MissingRequestBody as exc:
146+
return None, [exc, ]
147+
148+
try:
149+
deserialised = self._deserialise(media_type, raw_body)
150+
except InvalidMediaTypeValue as exc:
151+
return None, [exc, ]
152+
153+
try:
154+
casted = self._cast(media_type, deserialised)
155+
except CastError as exc:
156+
return None, [exc, ]
157+
158+
try:
159+
body = self._unmarshal(media_type, casted)
160+
except (ValidateError, UnmarshalError) as exc:
161+
return None, [exc, ]
162+
163+
return body, []
167164

168165
def _deserialise(self, param_or_media_type, value):
169166
return param_or_media_type.deserialise(value)

openapi_core/validation/response/validators.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,35 @@ def _validate_data(self, request, response):
6565
return ResponseValidationResult(data_errors, data, None)
6666

6767
def _get_data(self, response, operation_response):
68-
errors = []
69-
7068
if not operation_response.content:
71-
return None, errors
69+
return None, []
7270

73-
data = None
7471
try:
7572
media_type = operation_response[response.mimetype]
7673
except InvalidContentType as exc:
77-
errors.append(exc)
78-
else:
79-
try:
80-
raw_data = operation_response.get_value(response)
81-
except MissingResponseContent as exc:
82-
errors.append(exc)
83-
else:
84-
try:
85-
deserialised = self._deserialise(media_type, raw_data)
86-
except InvalidMediaTypeValue as exc:
87-
errors.append(exc)
88-
else:
89-
try:
90-
casted = self._cast(media_type, deserialised)
91-
except CastError as exc:
92-
errors.append(exc)
93-
else:
94-
try:
95-
data = self._unmarshal(media_type, casted)
96-
except (ValidateError, UnmarshalError) as exc:
97-
errors.append(exc)
98-
99-
return data, errors
74+
return None, [exc, ]
75+
76+
try:
77+
raw_data = operation_response.get_value(response)
78+
except MissingResponseContent as exc:
79+
return None, [exc, ]
80+
81+
try:
82+
deserialised = self._deserialise(media_type, raw_data)
83+
except InvalidMediaTypeValue as exc:
84+
return None, [exc, ]
85+
86+
try:
87+
casted = self._cast(media_type, deserialised)
88+
except CastError as exc:
89+
return None, [exc, ]
90+
91+
try:
92+
data = self._unmarshal(media_type, casted)
93+
except (ValidateError, UnmarshalError) as exc:
94+
return None, [exc, ]
95+
96+
return data, []
10097

10198
def _get_headers(self, response, operation_response):
10299
errors = []

0 commit comments

Comments
 (0)