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