-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
90 lines (73 loc) · 2.59 KB
/
Copy pathvalidation.py
File metadata and controls
90 lines (73 loc) · 2.59 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
86
87
88
89
90
from collections import Counter
from django.contrib.messages import constants
from django.utils.translation import gettext as _
class Message:
def __init__(self, level, message):
self.level = level
self.message = message
def __str__(self):
return str(self.message)
def __repr__(self):
return f"<{self.__class__.__name__}: message={self.message!r}>"
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.level == other.level
and self.message == other.message
)
def __hash__(self):
return hash((self.level, self.message))
class Warning(Message):
def __init__(self, *args, **kwargs):
super().__init__(constants.WARNING, *args, **kwargs)
class Error(Message):
def __init__(self, *args, **kwargs):
super().__init__(constants.ERROR, *args, **kwargs)
def validate_uniqueness(fields):
counts = Counter(field[0] for field in fields)
if repeated := [pair for pair in counts.items() if pair[1] > 1]:
return [
Warning(
_("Fields exist more than once: {fields}.").format(
fields=", ".join(
f"'{name}' ({count})" for name, count in sorted(repeated)
)
)
)
]
return []
def validate_required_fields(fields, required):
if missing := set(required) - {field[0] for field in fields}:
return [
Error(
_("Required fields are missing: {fields}.").format(
fields=", ".join(f"'{name}'" for name in sorted(missing))
)
)
]
return []
def validate_fields(fields, schema):
errors = []
field_dict = dict(fields)
for field, field_schema in schema.items():
if field not in field_dict:
errors.append(
Warning(
_("Expected field '{field}' doesn't exist.").format(field=field)
)
)
continue
for attribute, value in field_schema.items():
if field_dict[field][attribute] != value:
errors.append(
Error(
_(
"The '{attribute}' attribute of the field '{field}' doesn't have the expected value '{value}'."
).format(
field=field,
attribute=attribute,
value=value,
)
)
)
return errors