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)}"
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)
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)
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')),
def _validate_regex(self, value): try: super()._validate_regex(value) except ValidationError: raise ValidationError('value is not a valid email address')
def _validate_min_length(self, value): if len(value) < self.min_length: raise ValidationError('list length is less than {constraint}', constraint=self.min_length)
def _validate_max_length(self, value): if len(value) > self.max_length: raise ValidationError('list length is greater than {constraint}', constraint=self.max_length)
def _validate_gt(self, value): if value <= self.gt: raise ValidationError('value should be greater than {constraint}', constraint=self.gt)
def _validate_lt(self, value): if value >= self.lt: raise ValidationError('value should be less than {constraint}', constraint=self.lt)
def _validate_gte(self, value): if value < self.gte: raise ValidationError('value is less than {constraint}', constraint=self.gte)
def _validate_lte(self, value): if value > self.lte: raise ValidationError('value is greater than {constraint}', constraint=self.lte)
def _validate_regex(self, value): if not self.regex.match(value): raise ValidationError('value does not match pattern {constraint}', constraint=self.regex.pattern)
def _validate_blank(self, value): if value == '': if self.allow_blank: raise StopValidation() raise ValidationError('blank value is not allowed')
def _validate_choices(self, value): if value in self.choices: raise StopValidation() raise ValidationError("value does not match any variant")
def _validate_type(self, value): if not isinstance(value, self.field_type): raise ValidationError('invalid value type')
def _validate_none(self, value): if value is None: if self.allow_none: raise StopValidation() raise ValidationError('none value is not allowed')