예제 #1
0
    def validate(self, value, instance=None):
        if not self.required and value is None:
            return

        if self.required and value is None:
            raise RequiredFieldError('Field \'{}\' is required'.format(
                self.name))

        if self._type is not None and not isinstance(value, self._type):
            raise FieldTypeError(
                '{} value is not of type {}, given "{}"'.format(
                    self.name, self._type.__name__, value))
        if self._validator:
            try:
                res = self._validator(value)
            except ValidationError as e:
                if e.field_name is None:
                    raise ValidationError(
                        '{} did not pass the validation: {}'.format(
                            self.name, e.message),
                        field_name=self.name)
                else:
                    raise

            if res is False:
                raise ValidationError('{} did not pass the validation'.format(
                    self.name))
        return
예제 #2
0
 def validator(values):
     for i, x in enumerate(values):
         if not isinstance(x, str) or not re.match('foo[0-9]', x):
             raise ValidationError(
                 'element at index {} does not match the regex'.format(
                     i))
예제 #3
0
 def validate(self):
     if self.foo is None and self.bar is None:
         raise ValidationError(
             'one between foo and bar must be set')
예제 #4
0
 def validator(x):
     if not bool(re.match(regex, x)):
         raise ValidationError('value does not match regex'.format())