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
18 (e.g. ``data.property[index] must be smaller than or equal to 42``),
19 * invalid ``value`` (e.g. ``60``),
20 * ``name`` of a path in the data structure (e.g. ``data.property[index]``),
21 * ``path`` as an array in the data structure (e.g. ``['data', 'property', 'index']``),
22 * the whole ``definition`` which the ``value`` has to fulfil (e.g. ``{'type': 'number', 'maximum': 42}``),
23 * ``rule`` which the ``value`` is breaking (e.g. ``maximum``)
24 * and ``rule_definition`` (e.g. ``42``).
25
26 .. versionchanged:: 2.14.0
27 Added all extra properties.
28 """
29
30 def __init__(self, message, value=None, name=None, definition=None, rule=None):
31 super().__init__(message)
32 self.message = message
33 self.value = value
34 self.name = name
35 self.definition = definition
36 self.rule = rule
37
38 @property
39 def path(self):
40 return [item for item in SPLIT_RE.split(self.name) if item != '']
41
42 @property
43 def rule_definition(self):
44 if not self.rule or not self.definition:
45 return None
46 return self.definition.get(self.rule)
47
48
49class JsonSchemaValuesException(JsonSchemaException):
50 """
51 Exception raised by validation function. It is a collection of all errors.
52 """
53
54 def __init__(self, errors):
55 super().__init__()
56 self.errors = errors
57
58
59class JsonSchemaDefinitionException(JsonSchemaException):
60 """
61 Exception raised by generator of validation function.
62 """