"""Custom validations for use in Marshmallow schemas.""" import re from marshmallow import ValidationError, validate from abacus_common_logic.constants import error from abacus_common_logic.constants.constants import DECIMAL_PATTERN not_blank = validate.Length(min=1, error=error.ERROR_FIELD_MISSING) def check_decimal_precision(param): """Check the input is a float with no more than 2 decimal places of precision. :param param: a float :return: True if the float has no more than 2 digits after the decimal point, raises a ValidationError otherwise. """ if DECIMAL_PATTERN.match(str(param)): return True else: raise ValidationError(error.ERROR_INVALID_DECIMAL) def check_file_path(param): """Check the input is a valid file path. :param param: a string :return: True if the string is a valid file path, raises a ValidationError otherwise. """ if re.fullmatch(r'.*\:\/\/[A-z0-9-_\/]+\.[A-z]+', param): return True raise ValidationError(error.ERROR_INVALID_FILE_PATH)