"""Custom validators for marshmallow.""" from marshmallow import fields from marshmallow import ValidationError class IsNotBlankOrWhitespace(fields.Validator): """Validate string has non-whitespace characters.""" default_message = 'Field is required' def __init__(self, error=None): """Constructor. Args: error (str): Override default error message (optional) """ self.error = error or self.default_message def __call__(self, value): """Validator. Args: value (str): String to check. Returns: value """ if not value.strip(): raise ValidationError(self.error) return value class IsAllPositiveNumbers(fields.Validator): """Validate string has positive integers.""" default_message = 'Track Ids should be positive' def __init__(self, error=None): """Constructor. Args: error (str): Override default error message (optional) """ self.error = error or self.default_message def __call__(self, value): """Validator. Args: value (str): String to check. Returns: value """ try: tuids = list(map(int, value.split(','))) except ValueError as e: raise ValidationError(str(e)) for tuid in tuids: if tuid <= 0: raise ValidationError(self.error) return value