fastjsonschema_exceptions.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import re
  2. SPLIT_RE = re.compile(r'[\.\[\]]+')
  3. class JsonSchemaException(ValueError):
  4. """
  5. Base exception of ``fastjsonschema`` library.
  6. """
  7. class JsonSchemaValueException(JsonSchemaException):
  8. """
  9. Exception raised by validation function. Available properties:
  10. * ``message`` containing human-readable information what is wrong (e.g. ``data.property[index] must be smaller than or equal to 42``),
  11. * invalid ``value`` (e.g. ``60``),
  12. * ``name`` of a path in the data structure (e.g. ``data.property[index]``),
  13. * ``path`` as an array in the data structure (e.g. ``['data', 'property', 'index']``),
  14. * the whole ``definition`` which the ``value`` has to fulfil (e.g. ``{'type': 'number', 'maximum': 42}``),
  15. * ``rule`` which the ``value`` is breaking (e.g. ``maximum``)
  16. * and ``rule_definition`` (e.g. ``42``).
  17. .. versionchanged:: 2.14.0
  18. Added all extra properties.
  19. """
  20. def __init__(self, message, value=None, name=None, definition=None, rule=None):
  21. super().__init__(message)
  22. self.message = message
  23. self.value = value
  24. self.name = name
  25. self.definition = definition
  26. self.rule = rule
  27. @property
  28. def path(self):
  29. return [item for item in SPLIT_RE.split(self.name) if item != '']
  30. @property
  31. def rule_definition(self):
  32. if not self.rule or not self.definition:
  33. return None
  34. return self.definition.get(self.rule)
  35. class JsonSchemaDefinitionException(JsonSchemaException):
  36. """
  37. Exception raised by generator of validation function.
  38. """