async def resolve(self, request: http.Request, route: Route, path_params: http.PathParams) -> ValidatedPathParams: validator = type("Validator", (marshmallow.Schema, ), { f.name: f.schema for f in route.path_fields[request.method].values() }) try: path_params = validator().load(path_params) except marshmallow.ValidationError as exc: raise exceptions.ValidationError(detail=exc.normalized_messages()) return ValidatedPathParams(path_params)
def resolve(self, request: http.Request, route: Route, data: http.RequestData): if not route.body_field[request.method] or not route.body_field[ request.method].schema: return data validator = route.body_field[request.method].schema try: return validator.load(data) except marshmallow.ValidationError as exc: raise exceptions.ValidationError(detail=exc.normalized_messages())
def resolve(self, request: http.Request, route: Route, query_params: http.QueryParams) -> ValidatedQueryParams: validator = type("Validator", (marshmallow.Schema, ), { f.name: f.schema for f in route.query_fields[request.method].values() }) try: query_params = validator().load(dict(query_params), unknown=marshmallow.EXCLUDE) except marshmallow.ValidationError as exc: raise exceptions.ValidationError(detail=exc.normalized_messages()) return ValidatedQueryParams(query_params)
def resolve(self, parameter: inspect.Parameter, path_params: ValidatedPathParams, query_params: ValidatedQueryParams): params = path_params if (parameter.name in path_params) else query_params if parameter.annotation in ( OptInt, OptFloat, OptBool, OptStr) or parameter.default is not parameter.empty: kwargs = { "missing": parameter.default if parameter.default is not parameter.empty else None } else: kwargs = {"required": True} param_validator = { inspect.Signature.empty: marshmallow.fields.Field, int: marshmallow.fields.Integer, float: marshmallow.fields.Number, bool: marshmallow.fields.Boolean, str: marshmallow.fields.String, uuid.UUID: marshmallow.fields.UUID, datetime.date: marshmallow.fields.Date, datetime.datetime: marshmallow.fields.DateTime, OptInt: marshmallow.fields.Integer, OptFloat: marshmallow.fields.Number, OptBool: marshmallow.fields.Boolean, OptStr: marshmallow.fields.String, http.QueryParam: marshmallow.fields.String, http.PathParam: marshmallow.fields.String, OptUUID: marshmallow.fields.UUID, OptDate: marshmallow.fields.Date, OptDateTime: marshmallow.fields.DateTime, }[parameter.annotation](**kwargs) validator = type("Validator", (marshmallow.Schema, ), {parameter.name: param_validator}) try: params = validator().load(params, unknown=marshmallow.EXCLUDE) except marshmallow.ValidationError as exc: raise exceptions.ValidationError(detail=exc.normalized_messages()) return params.get(parameter.name, parameter.default)