You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
3.9 KiB

4 years ago
  1. class Error(Exception):
  2. """Base validation exception."""
  3. class SchemaError(Error):
  4. """An error was encountered in the schema."""
  5. class Invalid(Error):
  6. """The data was invalid.
  7. :attr msg: The error message.
  8. :attr path: The path to the error, as a list of keys in the source data.
  9. :attr error_message: The actual error message that was raised, as a
  10. string.
  11. """
  12. def __init__(self, message, path=None, error_message=None, error_type=None):
  13. Error.__init__(self, message)
  14. self.path = path or []
  15. self.error_message = error_message or message
  16. self.error_type = error_type
  17. @property
  18. def msg(self):
  19. return self.args[0]
  20. def __str__(self):
  21. path = ' @ data[%s]' % ']['.join(map(repr, self.path)) \
  22. if self.path else ''
  23. output = Exception.__str__(self)
  24. if self.error_type:
  25. output += ' for ' + self.error_type
  26. return output + path
  27. def prepend(self, path):
  28. self.path = path + self.path
  29. class MultipleInvalid(Invalid):
  30. def __init__(self, errors=None):
  31. self.errors = errors[:] if errors else []
  32. def __repr__(self):
  33. return 'MultipleInvalid(%r)' % self.errors
  34. @property
  35. def msg(self):
  36. return self.errors[0].msg
  37. @property
  38. def path(self):
  39. return self.errors[0].path
  40. @property
  41. def error_message(self):
  42. return self.errors[0].error_message
  43. def add(self, error):
  44. self.errors.append(error)
  45. def __str__(self):
  46. return str(self.errors[0])
  47. def prepend(self, path):
  48. for error in self.errors:
  49. error.prepend(path)
  50. class RequiredFieldInvalid(Invalid):
  51. """Required field was missing."""
  52. class ObjectInvalid(Invalid):
  53. """The value we found was not an object."""
  54. class DictInvalid(Invalid):
  55. """The value found was not a dict."""
  56. class ExclusiveInvalid(Invalid):
  57. """More than one value found in exclusion group."""
  58. class InclusiveInvalid(Invalid):
  59. """Not all values found in inclusion group."""
  60. class SequenceTypeInvalid(Invalid):
  61. """The type found is not a sequence type."""
  62. class TypeInvalid(Invalid):
  63. """The value was not of required type."""
  64. class ValueInvalid(Invalid):
  65. """The value was found invalid by evaluation function."""
  66. class ContainsInvalid(Invalid):
  67. """List does not contain item"""
  68. class ScalarInvalid(Invalid):
  69. """Scalars did not match."""
  70. class CoerceInvalid(Invalid):
  71. """Impossible to coerce value to type."""
  72. class AnyInvalid(Invalid):
  73. """The value did not pass any validator."""
  74. class AllInvalid(Invalid):
  75. """The value did not pass all validators."""
  76. class MatchInvalid(Invalid):
  77. """The value does not match the given regular expression."""
  78. class RangeInvalid(Invalid):
  79. """The value is not in given range."""
  80. class TrueInvalid(Invalid):
  81. """The value is not True."""
  82. class FalseInvalid(Invalid):
  83. """The value is not False."""
  84. class BooleanInvalid(Invalid):
  85. """The value is not a boolean."""
  86. class UrlInvalid(Invalid):
  87. """The value is not a url."""
  88. class EmailInvalid(Invalid):
  89. """The value is not a email."""
  90. class FileInvalid(Invalid):
  91. """The value is not a file."""
  92. class DirInvalid(Invalid):
  93. """The value is not a directory."""
  94. class PathInvalid(Invalid):
  95. """The value is not a path."""
  96. class LiteralInvalid(Invalid):
  97. """The literal values do not match."""
  98. class LengthInvalid(Invalid):
  99. pass
  100. class DatetimeInvalid(Invalid):
  101. """The value is not a formatted datetime string."""
  102. class DateInvalid(Invalid):
  103. """The value is not a formatted date string."""
  104. class InInvalid(Invalid):
  105. pass
  106. class NotInInvalid(Invalid):
  107. pass
  108. class ExactSequenceInvalid(Invalid):
  109. pass
  110. class NotEnoughValid(Invalid):
  111. """The value did not pass enough validations."""
  112. pass
  113. class TooManyValid(Invalid):
  114. """The value passed more than expected validations."""
  115. pass