Request and response data validation process refac

This commit is contained in:
Artur Maciag 2020-02-03 11:13:37 +00:00
parent 7da1ea6b77
commit faa8164f9a
2 changed files with 52 additions and 58 deletions

View file

@ -114,12 +114,12 @@ class RequestValidator(object):
except OpenAPIParameterError as exc: except OpenAPIParameterError as exc:
errors.append(exc) errors.append(exc)
continue continue
else:
try: try:
casted = self._cast(param, deserialised) casted = self._cast(param, deserialised)
except CastError as exc: except CastError as exc:
errors.append(exc) errors.append(exc)
continue continue
try: try:
unmarshalled = self._unmarshal(param, casted) unmarshalled = self._unmarshal(param, casted)
@ -132,38 +132,35 @@ class RequestValidator(object):
return RequestParameters(**locations), errors return RequestParameters(**locations), errors
def _get_body(self, request, operation): def _get_body(self, request, operation):
errors = []
if operation.request_body is None: if operation.request_body is None:
return None, errors return None, []
body = None
try: try:
media_type = operation.request_body[request.mimetype] media_type = operation.request_body[request.mimetype]
except InvalidContentType as exc: except InvalidContentType as exc:
errors.append(exc) return None, [exc, ]
else:
try:
raw_body = operation.request_body.get_value(request)
except MissingRequestBody as exc:
errors.append(exc)
else:
try:
deserialised = self._deserialise(media_type, raw_body)
except InvalidMediaTypeValue as exc:
errors.append(exc)
else:
try:
casted = self._cast(media_type, deserialised)
except CastError as exc:
errors.append(exc)
else:
try:
body = self._unmarshal(media_type, casted)
except (ValidateError, UnmarshalError) as exc:
errors.append(exc)
return body, errors try:
raw_body = operation.request_body.get_value(request)
except MissingRequestBody as exc:
return None, [exc, ]
try:
deserialised = self._deserialise(media_type, raw_body)
except InvalidMediaTypeValue as exc:
return None, [exc, ]
try:
casted = self._cast(media_type, deserialised)
except CastError as exc:
return None, [exc, ]
try:
body = self._unmarshal(media_type, casted)
except (ValidateError, UnmarshalError) as exc:
return None, [exc, ]
return body, []
def _deserialise(self, param_or_media_type, value): def _deserialise(self, param_or_media_type, value):
return param_or_media_type.deserialise(value) return param_or_media_type.deserialise(value)

View file

@ -65,38 +65,35 @@ class ResponseValidator(object):
return ResponseValidationResult(data_errors, data, None) return ResponseValidationResult(data_errors, data, None)
def _get_data(self, response, operation_response): def _get_data(self, response, operation_response):
errors = []
if not operation_response.content: if not operation_response.content:
return None, errors return None, []
data = None
try: try:
media_type = operation_response[response.mimetype] media_type = operation_response[response.mimetype]
except InvalidContentType as exc: except InvalidContentType as exc:
errors.append(exc) return None, [exc, ]
else:
try:
raw_data = operation_response.get_value(response)
except MissingResponseContent as exc:
errors.append(exc)
else:
try:
deserialised = self._deserialise(media_type, raw_data)
except InvalidMediaTypeValue as exc:
errors.append(exc)
else:
try:
casted = self._cast(media_type, deserialised)
except CastError as exc:
errors.append(exc)
else:
try:
data = self._unmarshal(media_type, casted)
except (ValidateError, UnmarshalError) as exc:
errors.append(exc)
return data, errors try:
raw_data = operation_response.get_value(response)
except MissingResponseContent as exc:
return None, [exc, ]
try:
deserialised = self._deserialise(media_type, raw_data)
except InvalidMediaTypeValue as exc:
return None, [exc, ]
try:
casted = self._cast(media_type, deserialised)
except CastError as exc:
return None, [exc, ]
try:
data = self._unmarshal(media_type, casted)
except (ValidateError, UnmarshalError) as exc:
return None, [exc, ]
return data, []
def _get_headers(self, response, operation_response): def _get_headers(self, response, operation_response):
errors = [] errors = []