openapi-core/openapi_core/validation/request/models.py

42 lines
1.2 KiB
Python
Raw Normal View History

2018-04-17 12:38:23 +00:00
"""OpenAPI core validation request models module"""
2018-04-18 10:39:03 +00:00
from openapi_core.schema.exceptions import OpenAPIMappingError
2018-04-17 12:38:23 +00:00
from openapi_core.validation.models import BaseValidationResult
class RequestParameters(dict):
valid_locations = ['path', 'query', 'header', 'cookie']
2018-04-17 12:38:23 +00:00
def __getitem__(self, location):
self.validate_location(location)
return self.setdefault(location, {})
def __setitem__(self, location, value):
raise NotImplementedError
2019-06-18 15:13:44 +00:00
def __add__(self, other):
if not isinstance(other, self.__class__):
raise ValueError("Invalid type")
for location in self.valid_locations:
if location in other:
self[location].update(other[location])
return self
2018-04-17 12:38:23 +00:00
@classmethod
def validate_location(cls, location):
if location not in cls.valid_locations:
raise OpenAPIMappingError(
"Unknown parameter location: {0}".format(str(location)))
class RequestValidationResult(BaseValidationResult):
def __init__(self, errors, body=None, parameters=None):
super(RequestValidationResult, self).__init__(errors)
self.body = body
self.parameters = parameters or RequestParameters()