forked from braintree/braintree_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation_error_collection.py
More file actions
86 lines (65 loc) · 2.86 KB
/
validation_error_collection.py
File metadata and controls
86 lines (65 loc) · 2.86 KB
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
from braintree.validation_error import ValidationError
class ValidationErrorCollection(object):
"""
A class representing a collection of validation errors.
For more information on ValidationErrors, see http://www.braintreepaymentsolutions.com/gateway/validation-errors
"""
def __init__(self, data={"errors": []}):
self.data = data
@property
def deep_errors(self):
"""
Return all :class:`ValidationErrors <braintree.validation_error.ValidationError>`, including nested errors.
"""
result = []
result.extend(self.errors)
for nested_error in self.__nested_errors.values():
result.extend(nested_error.deep_errors)
return result
def for_index(self, index):
return self.for_object("index_%s" % index)
def for_object(self, nested_key):
"""
Returns a :class:`ValidationErrorCollection <braintree.validation_error_collection.ValidationErrorCollection>` representing the errors at the nested level:::
error_result = Transaction.sale({"credit_card": {"number": "invalid"}})
print error_result.errors.for_object("transaction").for_object("credit_card").on("number")[0].code
"""
return self.__get_nested_errrors(nested_key)
def on(self, attribute):
"""
Returns the list of errors for a given attribute::
error_result = Transaction.sale({"credit_card": {"number": "invalid"}})
print [ error.code for error in error_result.errors.for_object("transaction").for_object("credit_card").on("number") ]
"""
return [error for error in self.errors if error.attribute == attribute]
@property
def deep_size(self):
"""Returns the number of errors on this object and any nested objects."""
size = len(self.errors)
for error in self.__nested_errors.values():
size += error.deep_size
return size
@property
def errors(self):
"""Returns a list of :class:`ValidationError <braintree.validation_error.ValidationError>` objects."""
return [ValidationError(error) for error in self.data["errors"]]
@property
def size(self):
"""Returns the number of errors on this object, without counting nested errors."""
return len(self.errors)
def __get_nested_errrors(self, nested_key):
if nested_key in self.__nested_errors:
return self.__nested_errors[nested_key]
else:
return ValidationErrorCollection()
def __getitem__(self, index):
return self.errors[index]
def __len__(self):
return self.size
@property
def __nested_errors(self):
nested_errors = {}
for key in self.data.keys():
if key == "errors": continue
nested_errors[key] = ValidationErrorCollection(self.data[key])
return nested_errors