コード例 #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)}"
コード例 #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)
コード例 #3
0
ファイル: document.py プロジェクト: devtud/aiomongodel
    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)
コード例 #4
0
 def _validate_regex(self, value):
     try:
         super()._validate_regex(value)
     except ValidationError:
         raise ValidationError('value is not a valid email address')
コード例 #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)
コード例 #6
0
 def _validate_lt(self, value):
     if value >= self.lt:
         raise ValidationError('value should be less than {constraint}',
                               constraint=self.lt)
コード例 #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)
コード例 #8
0
 def _validate_lte(self, value):
     if value > self.lte:
         raise ValidationError('value is greater than {constraint}',
                               constraint=self.lte)
コード例 #9
0
 def _validate_gt(self, value):
     if value <= self.gt:
         raise ValidationError('value should be greater than {constraint}',
                               constraint=self.gt)
コード例 #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)
コード例 #11
0
 def _validate_gte(self, value):
     if value < self.gte:
         raise ValidationError('value is less than {constraint}',
                               constraint=self.gte)
コード例 #12
0
 def _validate_blank(self, value):
     if value == '':
         if self.allow_blank:
             raise StopValidation()
         raise ValidationError('blank value is not allowed')
コード例 #13
0
 def _validate_choices(self, value):
     if value in self.choices:
         raise StopValidation()
     raise ValidationError("value does not match any variant")
コード例 #14
0
 def _validate_type(self, value):
     if not isinstance(value, self.field_type):
         raise ValidationError('invalid value type')
コード例 #15
0
 def _validate_none(self, value):
     if value is None:
         if self.allow_none:
             raise StopValidation()
         raise ValidationError('none value is not allowed')
コード例 #16
0
ファイル: test_fields.py プロジェクト: ilex/aiomongodel
    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')),