Exemplo n.º 1
0
def test_validation_error_to_str():
    error = ValidationError('invalid value type')
    assert str(error) == 'invalid value type'

    error = ValidationError('value should be less than {constraint}',
                            constraint=10)
    assert str(error) == 'value should be less than 10'

    error = ValidationError({'name': ValidationError('field is required')})
    assert str(error) == "{'name': ValidationError(field is required)}"
Exemplo n.º 2
0
    def _validate_items(self, value):
        errors = {}
        for index, item in enumerate(value):
            try:
                self.item_field.validate(item)
            except ValidationError as e:
                errors[index] = e

        if errors:
            raise ValidationError(errors)
Exemplo n.º 3
0
    def validate_document(cls, document):
        """Validate given document.

        Args:
            document: Document instance to validate.

        Raises:
            ValidationError: If document's data is not valid.
        """
        errors = {}
        for field_name, field in cls.meta.fields.items():
            try:
                field.validate(document._data[field_name])
            except ValidationError as e:
                errors[field_name] = e
            except KeyError:
                if field.required:
                    errors[field_name] = ValidationError('field is required')

        if errors:
            raise ValidationError(errors)
Exemplo n.º 4
0
 def _validate_regex(self, value):
     try:
         super()._validate_regex(value)
     except ValidationError:
         raise ValidationError('value is not a valid email address')
Exemplo n.º 5
0
 def _validate_max_length(self, value):
     if len(value) > self.max_length:
         raise ValidationError('list length is greater than {constraint}',
                               constraint=self.max_length)
Exemplo n.º 6
0
 def _validate_lt(self, value):
     if value >= self.lt:
         raise ValidationError('value should be less than {constraint}',
                               constraint=self.lt)
Exemplo n.º 7
0
 def _validate_min_length(self, value):
     if len(value) < self.min_length:
         raise ValidationError('list length is less than {constraint}',
                               constraint=self.min_length)
Exemplo n.º 8
0
 def _validate_lte(self, value):
     if value > self.lte:
         raise ValidationError('value is greater than {constraint}',
                               constraint=self.lte)
Exemplo n.º 9
0
 def _validate_gt(self, value):
     if value <= self.gt:
         raise ValidationError('value should be greater than {constraint}',
                               constraint=self.gt)
Exemplo n.º 10
0
 def _validate_regex(self, value):
     if not self.regex.match(value):
         raise ValidationError('value does not match pattern {constraint}',
                               constraint=self.regex.pattern)
Exemplo n.º 11
0
 def _validate_gte(self, value):
     if value < self.gte:
         raise ValidationError('value is less than {constraint}',
                               constraint=self.gte)
Exemplo n.º 12
0
 def _validate_blank(self, value):
     if value == '':
         if self.allow_blank:
             raise StopValidation()
         raise ValidationError('blank value is not allowed')
Exemplo n.º 13
0
 def _validate_choices(self, value):
     if value in self.choices:
         raise StopValidation()
     raise ValidationError("value does not match any variant")
Exemplo n.º 14
0
 def _validate_type(self, value):
     if not isinstance(value, self.field_type):
         raise ValidationError('invalid value type')
Exemplo n.º 15
0
 def _validate_none(self, value):
     if value is None:
         if self.allow_none:
             raise StopValidation()
         raise ValidationError('none value is not allowed')
Exemplo n.º 16
0
    assert excinfo.value.as_dict() == {
        'set_choices': 'value does not match any variant',
        'dict_choices': 'value does not match any variant',
    }


@pytest.mark.parametrize(
    'field, value, expected',
    [
        # AnyField
        (AnyField(), '1', None),
        (AnyField(), 1, None),
        (AnyField(), True, None),
        (AnyField(allow_none=True), None, None),
        (AnyField(allow_none=False), None,
         ValidationError('none value is not allowed')),
        (AnyField(choices={'xxx', 'yyy'}), 'xxx', None),
        (AnyField(choices={'xxx', 'yyy'}), 1,
         ValidationError('value does not match any variant')),
        # StrField
        (StrField(), 'xxx', None),
        (StrField(allow_none=True), None, None),
        (StrField(allow_blank=True), '', None),
        (StrField(choices=('xxx', 'yyy')), 'xxx', None),
        (StrField(choices=('xxx', 'yyy'), max_length=2), 'xxx', None),
        (StrField(choices=('xxx', 'yyy'), regex=r'zzz'), 'xxx', None),
        (StrField(regex=r'[abc]+'), 'aa', None),
        (StrField(regex=re.compile(r'[abc]+')), 'aa', None),
        (StrField(min_length=2, max_length=3), 'aa', None),
        (StrField(allow_none=False), None,
         ValidationError('none value is not allowed')),