def test_dict_details_to_string(self):
     assert ValidationError({
         'field': 'There has been a sad error'
     }).details == {
         'field': 'There has been a sad error'
     }
     assert str(
         ValidationError({
             'field': 'A sad error',
             'another_field': 'A number was expected',
         })) == ("field: A sad error.\n"
                 "another_field: A number was expected.")
 def test_dict_with_list_details_to_string(self):
     assert str(
         ValidationError({
             'field': ['A sad error', 'A shocking error'],
             'another_field': 'A number was expected',
         })) == ("field: A sad error; A shocking error.\n"
                 "another_field: A number was expected.")
Example #3
0
    def __call__(self, value, *, name, setting, **ignore):
        if value is Undefined:
            return

        type_hint = setting.type_hint if self.type_hint is None else self.type_hint

        try:
            check_type(name, value, type_hint)
        except TypeError as e:
            raise ValidationError(f'Expected value of type `{type_hint}` '
                                  f'got value of type `{type(value)}`') from e
Example #4
0
 def __call__(self, value, **ignore):
     try:
         UUID(value)
     except ValueError as e:
         raise ValidationError(f"Invalid value `{value}`: {str(e)}") from e
Example #5
0
 def is_less_that_10(val, **kwargs):
     if val >= 10:
         raise ValidationError('Value should be less that 10')
Example #6
0
 def is_positive(val, **kwargs):
     if val <= 0:
         raise ValidationError('Value should be positive')
 def test_list_details_to_string(self):
     assert ValidationError(['a', 'b']).details == ['a', 'b']
     assert str(ValidationError(['a', 'b'])) == "a; b"
 def test_str_details_to_string(self):
     assert ValidationError('abc').details == 'abc'
     assert str(ValidationError('abc')) == 'abc'
Example #9
0
 def validate(self):
     raise ValidationError('there was an error XXXX')
 def __call__(self, value, *, name, owner, **ignore):
     if value == Undefined:
         msg = self.message.format(name=name)
         raise ValidationError(msg)