MCPcopy
hub / github.com/django/django / ValidationError

Class ValidationError

django/core/exceptions.py:144–247  ·  view source on GitHub ↗

An error while validating data.

Source from the content-addressed store, hash-verified

142
143
144class ValidationError(Exception):
145 """An error while validating data."""
146
147 def __init__(self, message, code=None, params=None):
148 """
149 The `message` argument can be a single error, a list of errors, or a
150 dictionary that maps field names to lists of errors. What we define as
151 an "error" can be either a simple string or an instance of
152 ValidationError with its message attribute set, and what we define as
153 list or dictionary can be an actual `list` or `dict` or an instance
154 of ValidationError with its `error_list` or `error_dict` attribute set.
155 """
156 super().__init__(message, code, params)
157
158 if isinstance(message, ValidationError):
159 if hasattr(message, "error_dict"):
160 message = message.error_dict
161 elif not hasattr(message, "message"):
162 message = message.error_list
163 else:
164 message, code, params = message.message, message.code, message.params
165
166 if isinstance(message, dict):
167 self.error_dict = {}
168 for field, messages in message.items():
169 if not isinstance(messages, ValidationError):
170 messages = ValidationError(messages)
171 self.error_dict[field] = messages.error_list
172
173 elif isinstance(message, list):
174 self.error_list = []
175 for message in message:
176 # Normalize plain strings to instances of ValidationError.
177 if not isinstance(message, ValidationError):
178 message = ValidationError(message)
179 if hasattr(message, "error_dict"):
180 self.error_list.extend(sum(message.error_dict.values(), []))
181 else:
182 self.error_list.extend(message.error_list)
183
184 else:
185 self.message = message
186 self.code = code
187 self.params = params
188 self.error_list = [self]
189
190 @property
191 def message_dict(self):
192 # Trigger an AttributeError if this ValidationError
193 # doesn't have an error_dict.
194 getattr(self, "error_dict")
195
196 return dict(self)
197
198 @property
199 def messages(self):
200 if hasattr(self, "error_dict"):
201 return sum(dict(self).values(), [])

Callers 15

validate_passwordsMethod · 0.90
validate_passwordsMethod · 0.90
clean_usernameMethod · 0.90
confirm_login_allowedMethod · 0.90
clean_old_passwordMethod · 0.90
validate_passwordFunction · 0.90
validateMethod · 0.90
validateMethod · 0.90
validateMethod · 0.90
validateMethod · 0.90

Calls

no outgoing calls

Tested by 15

validateMethod · 0.72
test_eqMethod · 0.72
test_eq_nestedMethod · 0.72
test_hashMethod · 0.72
test_hash_nestedMethod · 0.72
test_single_messageMethod · 0.72
test_message_listMethod · 0.72
test_message_dictMethod · 0.72
cleanMethod · 0.72
cleanMethod · 0.72