"""Model validators.""" from sukimu import exceptions def enum(*values): """Enum validator. Ensure the value passed is part of a specific list of values. Args: values (list): list of all the authorized values. Returns: callable: the validate method. """ def validate(value): """Validator. Args: value (mixed): the value to validate. Returns: mixed: the validated value. """ if value not in values: raise exceptions.FieldException( 'The value selected is not part of the choices available.') return value return validate def only_contains(*values): """Validator that checks a list only contains specific values. The validator goes through all the items passed in and check they are part of the group. Args: values (list): the values to validate. Returns: mixed: the validated values. """ def validate(items): """Validator. Args: value (mixed): the value to validate. Returns: mixed: the validated value. """ for item in items: if item not in values: raise exceptions.FieldException( 'The value {} is not part of the choices ' 'available.'.format(item)) return items return validate