def parse_iso8601_interval(value): try: return aniso8601.parse_interval(value) except Exception: try: return aniso8601.parse_repeating_interval(value) except Exception: raise FormatError('{} is not an iso8601 interval'.format(value))
def test_it_validates_formats_of_any_type(self): checker = mock.Mock(spec=FormatChecker) validator = self.validator_class( {"format": "foo"}, format_checker=checker, ) validator.validate([1, 2, 3]) checker.check.assert_called_once_with([1, 2, 3], "foo") cause = ValueError() checker.check.side_effect = FormatError('aoeu', cause=cause) with self.assertRaises(ValidationError) as cm: validator.validate([1, 2, 3]) # Make sure original cause is attached self.assertIs(cm.exception.cause, cause)
def test_it_validates_formats_if_a_checker_is_provided(self): checker = mock.Mock(spec=FormatChecker) validator = self.validator_class( {"format": "foo"}, format_checker=checker, ) validator.validate("bar") checker.check.assert_called_once_with("bar", "foo") cause = ValueError() checker.check.side_effect = FormatError("aoeu", cause=cause) with self.assertRaises(ValidationError) as cm: validator.validate("bar") # Make sure original cause is attached self.assertIs(cm.exception.cause, cause)
def check(self, instance, format): if instance is not None and format == "date-time": if not self.check_datetime(instance): raise FormatError(f"{instance} has invalid datetime format") else: return super().check(instance, format)