def test_first_list_in_dict(): e = ValidationError(OrderedDict([ ('foo', ['error @ foo.0', 'error @ foo.1', 'error @ foo.2']), ('bar', 'error @ bar'), ('baz', 'error @ baz') ])) assert e.first() == (('foo',), 'error @ foo.0')
def test_first_dict(): e = ValidationError(OrderedDict([ ('foo', 'error @ foo'), ('bar', 'error @ bar'), ('baz', 'error @ baz') ])) assert e.first() == (('foo',), 'error @ foo')
def test_first_nested_dict(): e = ValidationError( OrderedDict([('foo', OrderedDict([ ('hello', 'error @ foo.hello'), ('world', 'error @ foo.world'), ])), ('bar', 'error @ bar'), ('baz', 'error @ baz')])) assert e.first() == (('foo', 'hello'), 'error @ foo.hello')
def test_first_dict_in_list(): e = ValidationError([ OrderedDict([('foo', 'error @ 0.foo'), ('bar', 'error @ 0.bar'), ('baz', 'error @ 0.baz')]), OrderedDict([('foo', 'error @ 1.foo'), ('bar', 'error @ 1.bar'), ('baz', 'error @ 1.baz')]) ]) assert e.first() == (('foo', ), 'error @ 0.foo')
def test_first_nested_dict(): e = ValidationError(OrderedDict([ ('foo', OrderedDict([ ('hello', 'error @ foo.hello'), ('world', 'error @ foo.world'), ])), ('bar', 'error @ bar'), ('baz', 'error @ baz') ])) assert e.first() == (('foo', 'hello'), 'error @ foo.hello')
def test_first_dict_in_list(): e = ValidationError([ OrderedDict([ ('foo', 'error @ 0.foo'), ('bar', 'error @ 0.bar'), ('baz', 'error @ 0.baz') ]), OrderedDict([ ('foo', 'error @ 1.foo'), ('bar', 'error @ 1.bar'), ('baz', 'error @ 1.baz') ]) ]) assert e.first() == (('foo',), 'error @ 0.foo')
def to_internal_value(self, data): if not isinstance(data, dict): self.fail('not_a_dict') errors = {} pre_value = {} for field in self.writable_fields: pre_value[field.source] = field.get_value(data) pre_value = self.pre_validate(pre_value) value = {} for field in self.writable_fields: field_value = pre_value[field.source] validate_method = getattr(self, 'validate_' + field.field_name, None) try: field_value = field.run_validation(field_value) if validate_method is not None: field_value = validate_method(field_value) except ValidationError as e: errors[field.field_name] = e.errors else: value[field.source] = field_value if errors: raise ValidationError(errors) return value
def test_normalise_list_complex(): errors = [{ 'foo': ['1', '2', '3'], 'bar': '4', }, ['foo', 'bar', 'baz'], 'foo'] e = ValidationError(errors) assert e.errors == [{ 'foo': ['1', '2', '3'], 'bar': ['4'], }, ['foo', 'bar', 'baz'], 'foo']
def run_validators_on_field(self, data, field_name, validators): field = self.fields[field_name] value = field.get_attribute(data) try: for validator in validators: if hasattr(validator, 'set_context'): validator.set_context(self) value = validator(value) except ValidationError as e: raise ValidationError({field.field_name: e.errors}) data[field.source] = value
def run_validators_on_serializer(self, data, validators): try: for validator in validators: if hasattr(validator, 'set_context'): validator.set_context(self) data = validator(data) except ValidationError as e: if isinstance(e.errors, dict): raise else: raise ValidationError({'_': e.errors}) return data
def run_validation(self, data): data = self.validate_empty_values(data) if data is None: return data value = self.to_internal_value(data) try: value = self.run_validators(value) value = self.validate(value) except ValidationError as e: if isinstance(e.errors, dict): raise else: raise ValidationError({'_': e.errors}) return value
def is_valid(self, raise_exception=False): if self.partial: data = self.get_partial() else: data = self.initial_data try: self.validated_data = self.run_validation(data) except ValidationError as e: self.validated_data = {} self.errors = e.errors else: self.errors = {} if self.errors and raise_exception: raise ValidationError(self.errors) return not bool(self.errors)
def to_internal_value(self, data): if not isinstance(data, list): self.fail('not_a_list') values = [] errors = {} for i, x in enumerate(data): try: value = self.child.run_validation(x) except ValidationError as e: errors[i] = e.errors else: values.append(value) if errors: raise ValidationError(errors) return values
def test_normalise_dict_complex(): errors = { 'foo': [{ 'foo': '1', 'bar': '2', }], 'bar': '1', 'baz': ['1', '2', '3'], } e = ValidationError(errors) assert e.errors == { 'foo': [{ 'foo': ['1'], 'bar': ['2'], }], 'bar': ['1'], 'baz': ['1', '2', '3'], }
def test_normalise_empty_list(): e = ValidationError([]) assert e.errors == []
def test_first_empty_list(): e = ValidationError([]) assert e.first() is None
def test_first_list(): e = ValidationError(['error @ 0', 'error @ 1', 'error @ 2']) assert e.first() == (None, 'error @ 0')
def fail(self, key): raise ValidationError(self.error_messages[key])
def validate(self, data): raise ValidationError(self.message)
def test_first_dict(): e = ValidationError( OrderedDict([('foo', 'error @ foo'), ('bar', 'error @ bar'), ('baz', 'error @ baz')])) assert e.first() == (('foo', ), 'error @ foo')
def test_str(): e = ValidationError(['foo', 'bar', 'baz']) assert str(e) == "['foo', 'bar', 'baz']"
def test_normalise_string(): e = ValidationError('hello') assert e.errors == ['hello']
def error(value): raise ValidationError('Uh oh!')
def test_normalise_list(): errors = ['foo', 'bar', 'baz'] e = ValidationError(errors) assert e.errors == errors
def test_normalise_dict(): errors = {'foo': ['1', '2', '3'], 'bar': ['4', '5', '6']} e = ValidationError(errors) assert e.errors == errors
def test_first_list_in_dict(): e = ValidationError( OrderedDict([('foo', ['error @ foo.0', 'error @ foo.1', 'error @ foo.2']), ('bar', 'error @ bar'), ('baz', 'error @ baz')])) assert e.first() == (('foo', ), 'error @ foo.0')
def test_normalise_empty_dict(): e = ValidationError({}) assert e.errors == {}
def validate_foo(self, foo): raise ValidationError('Uh oh!')