"""Custom validations for use in Marshmallow schemas.""" from marshmallow import validate, ValidationError from royalty_common.constants import error from royalty_common.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)