"""Helper functions to use with handlers.""" from functools import wraps from artwork.utils import exception from flask import request from marshmallow import ValidationError def validate_request_data(schema, partial=False): """Decorate requests' input data validation. Args: schema (marshmallow.Schema()): schema instance to apply partial (bool): validate only derived fields """ def validator(func): @wraps(func) def wrapper(*args, **kwargs): data = request.get_json() try: schema.load(data, partial=partial) except ValidationError as err: # bubble the error back up # to be handled by the @app.errorhandler exception.reraise_exception(err) else: return func(*args, **kwargs) return wrapper return validator