def container_type(self, data: http.RequestData, coerce: ParamAnnotation) -> typing.Any: """ Handles `list` or `dict` annotations for HTTP requests. These types use the parsed request body. Args: data: The parsed request data. coerce: The type of the parameter that is being injected. Returns: The value that should be used for the handler function. """ if data is None or isinstance(data, coerce): return data try: return coerce(data) except exceptions.TypeSystemError as exc: detail = exc.detail except (TypeError, ValueError) as exc: detail = str(exc) raise exceptions.ValidationError(detail=detail)
def form_argument(self, data: http.RequestData, name: ParamName, coerce: ParamAnnotation) -> typing.Any: if not isinstance(data, dict): raise exceptions.ValidationError( detail='Request data must be an object.' ) data = data.get(name) if data is None or isinstance(data, coerce): return data try: return coerce(data) except exceptions.TypeSystemError as exc: detail = exc.detail except (TypeError, ValueError) as exc: detail = str(exc) raise exceptions.ValidationError(detail=detail)
def body_argument(self, data: http.RequestData, coerce: ParamAnnotation) -> typing.Any: if data is None or isinstance(data, coerce): return data try: return coerce(data) except exceptions.TypeSystemError as exc: detail = exc.detail except (TypeError, ValueError) as exc: detail = str(exc) raise exceptions.ValidationError(detail=detail)
def query_argument(self, name: ParamName, query_params: http.QueryParams, coerce: ParamAnnotation) -> typing.Any: value = query_params.get(name) if value is None or isinstance(value, coerce): return value try: return coerce(value) except exceptions.TypeSystemError as exc: detail = {name: exc.detail} except (TypeError, ValueError) as exc: detail = {name: str(exc)} raise exceptions.ValidationError(detail=detail)
def scalar_type(self, name: ParamName, kwargs: KeywordArgs, query_params: http.QueryParams, coerce: ParamAnnotation) -> typing.Any: """ Handles `str`, `int`, `float`, or `bool` annotations for HTTP requests. These types use either a matched URL keyword argument, or else a query parameter. Args: name: The name of the parameter. kwargs: The URL keyword arguments, as returned by the router. query_params: The query parameters of the incoming HTTP request. coerce: The type of the parameter. Returns: The value that should be used for the handler function. """ if name in kwargs: value = kwargs[name] is_url_arg = True else: value = query_params.get(name) is_url_arg = False if value is None or isinstance(value, coerce): return value try: return coerce(value) except exceptions.TypeSystemError as exc: detail = {name: exc.detail} except (TypeError, ValueError) as exc: detail = {name: str(exc)} if is_url_arg: raise exceptions.NotFound() raise exceptions.ValidationError(detail=detail)
def __init__(self, *args, **kwargs): """ additional validation rules """ super().__init__(*args, **kwargs) if not self.email and not self.phone: raise exceptions.ValidationError( 'You must supply one of Email or Phone.')
def validate(self, value): if not isinstance(value, str) or not value.startswith('bar_'): raise exceptions.ValidationError('Must start with bar_.') return Foo(value)