|
3 | 3 | import numbers |
4 | 4 | from voluptuous import Invalid |
5 | 5 | from django.utils.dateparse import parse_datetime, parse_date |
| 6 | +from django.core.exceptions import ImproperlyConfigured |
6 | 7 |
|
7 | 8 | # Forward compatibility with Python 3.x |
8 | 9 | if sys.version_info.major == 3: |
@@ -118,3 +119,68 @@ def fn(value): |
118 | 119 | '<{0}> is not a valid csv of integers'.format(value) |
119 | 120 | ) |
120 | 121 | return fn |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +class GenericSeparatedValidator(object): |
| 126 | + ''' |
| 127 | + Creates list like validator for any voluptuous validation function |
| 128 | + and any custom string separator. |
| 129 | + Instance of the class should be passed to the schema like this: |
| 130 | + >>> CSVofIntegers = GenericSeparatedValidator(int, ',') |
| 131 | + >>> Schema({ "field": CSVofIntegers() }) |
| 132 | +
|
| 133 | + >>> CSVofIntegers = GenericSeparatedValidator(int, ',') |
| 134 | + >>> CSVofIntegers('1,2,3') |
| 135 | + [1,2,3] |
| 136 | +
|
| 137 | + >>> WeirdSeparatedValidation = GenericSeparatedValidator(int, '^^') |
| 138 | + >>> WeirdSeparatedValidation('1^^2^^3') |
| 139 | + [1, 2, 3] |
| 140 | +
|
| 141 | + >>> CSVofIntegerLike = GenericSeparatedValidator(IntegerLike(), ',') |
| 142 | + >>> CSVofIntegerLike('a,b,c') |
| 143 | + Traceback (most recent call last): |
| 144 | + ... |
| 145 | + voluptuous.error.Invalid: <a,b,c> is not valid set of <IntegerLike>,\ |
| 146 | + Invalid input <a>; expected an integer |
| 147 | + ''' |
| 148 | + |
| 149 | + def __init__(self, validation_function, separator=',', msg=None): |
| 150 | + if not isinstance(separator, basestring): |
| 151 | + raise ImproperlyConfigured( |
| 152 | + 'GenericSeparatedValidator separator \ |
| 153 | + must be of type basestring' |
| 154 | + ) |
| 155 | + self.separator = separator |
| 156 | + self.validation_function = validation_function |
| 157 | + self.msg = msg |
| 158 | + if sys.version_info.major == 3: |
| 159 | + self.value_type = validation_function.__qualname__.split('.')[0] |
| 160 | + else: |
| 161 | + self.value_type = validation_function.__name__ |
| 162 | + |
| 163 | + def __call__(self, value): |
| 164 | + ''' |
| 165 | + Checks whether a value is list of given validation_function. |
| 166 | + Returns list of validated values or just one valid value in |
| 167 | + list if there is only one element in given CSV string. |
| 168 | + ''' |
| 169 | + try: |
| 170 | + if isinstance(value, basestring): |
| 171 | + if self.separator in value: |
| 172 | + seperated_string_values =[item.strip() for item |
| 173 | + in value.split(self.separator)] |
| 174 | + values = [self.validation_function(item) for item |
| 175 | + in seperated_string_values] |
| 176 | + else: |
| 177 | + values = [self.validation_function(value)] |
| 178 | + return values |
| 179 | + else: |
| 180 | + raise ValueError |
| 181 | + except (Invalid, ValueError) as e: |
| 182 | + raise Invalid(self.msg or |
| 183 | + ('<{0}> is not valid set of <{1}>, {2}'.format( |
| 184 | + value, |
| 185 | + self.value_type, |
| 186 | + e))) |
0 commit comments