summaryrefslogtreecommitdiff
path: root/voluptuous/error.py
blob: 35d392e59edbd45e6007142f5fcd9a13c3b5c70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import typing


class Error(Exception):
    """Base validation exception."""


class SchemaError(Error):
    """An error was encountered in the schema."""


class Invalid(Error):
    """The data was invalid.

    :attr msg: The error message.
    :attr path: The path to the error, as a list of keys in the source data.
    :attr error_message: The actual error message that was raised, as a
        string.

    """

    def __init__(self, message: str, path: typing.Optional[typing.List[str]] = None, error_message: typing.Optional[str] = None, error_type: typing.Optional[str] = None) -> None:
        Error.__init__(self, message)
        self.path = path or []
        self.error_message = error_message or message
        self.error_type = error_type

    @property
    def msg(self) -> str:
        return self.args[0]

    def __str__(self) -> str:
        path = ' @ data[%s]' % ']['.join(map(repr, self.path)) \
            if self.path else ''
        output = Exception.__str__(self)
        if self.error_type:
            output += ' for ' + self.error_type
        return output + path

    def prepend(self, path: typing.List[str]) -> None:
        self.path = path + self.path


class MultipleInvalid(Invalid):
    def __init__(self, errors: typing.Optional[typing.List[Invalid]] = None) -> None:
        self.errors = errors[:] if errors else []

    def __repr__(self) -> str:
        return 'MultipleInvalid(%r)' % self.errors

    @property
    def msg(self) -> str:
        return self.errors[0].msg

    @property
    def path(self) -> typing.List[str]:
        return self.errors[0].path

    @property
    def error_message(self) -> str:
        return self.errors[0].error_message

    def add(self, error: Invalid) -> None:
        self.errors.append(error)

    def __str__(self) -> str:
        return str(self.errors[0])

    def prepend(self, path: typing.List[str]) -> None:
        for error in self.errors:
            error.prepend(path)


class RequiredFieldInvalid(Invalid):
    """Required field was missing."""


class ObjectInvalid(Invalid):
    """The value we found was not an object."""


class DictInvalid(Invalid):
    """The value found was not a dict."""


class ExclusiveInvalid(Invalid):
    """More than one value found in exclusion group."""


class InclusiveInvalid(Invalid):
    """Not all values found in inclusion group."""


class SequenceTypeInvalid(Invalid):
    """The type found is not a sequence type."""


class TypeInvalid(Invalid):
    """The value was not of required type."""


class ValueInvalid(Invalid):
    """The value was found invalid by evaluation function."""


class ContainsInvalid(Invalid):
    """List does not contain item"""


class ScalarInvalid(Invalid):
    """Scalars did not match."""


class CoerceInvalid(Invalid):
    """Impossible to coerce value to type."""


class AnyInvalid(Invalid):
    """The value did not pass any validator."""


class AllInvalid(Invalid):
    """The value did not pass all validators."""


class MatchInvalid(Invalid):
    """The value does not match the given regular expression."""


class RangeInvalid(Invalid):
    """The value is not in given range."""


class TrueInvalid(Invalid):
    """The value is not True."""


class FalseInvalid(Invalid):
    """The value is not False."""


class BooleanInvalid(Invalid):
    """The value is not a boolean."""


class UrlInvalid(Invalid):
    """The value is not a URL."""


class EmailInvalid(Invalid):
    """The value is not an email address."""


class FileInvalid(Invalid):
    """The value is not a file."""


class DirInvalid(Invalid):
    """The value is not a directory."""


class PathInvalid(Invalid):
    """The value is not a path."""


class LiteralInvalid(Invalid):
    """The literal values do not match."""


class LengthInvalid(Invalid):
    pass


class DatetimeInvalid(Invalid):
    """The value is not a formatted datetime string."""


class DateInvalid(Invalid):
    """The value is not a formatted date string."""


class InInvalid(Invalid):
    pass


class NotInInvalid(Invalid):
    pass


class ExactSequenceInvalid(Invalid):
    pass


class NotEnoughValid(Invalid):
    """The value did not pass enough validations."""
    pass


class TooManyValid(Invalid):
    """The value passed more than expected validations."""
    pass