1
2
3 from collections.abc import Iterable
4 from marshmallow import Schema as _Schema, fields, ValidationError, validate
5 from six import string_types
6
7
8 loads_kwargs = {}
9 try:
10 from marshmallow.utils import EXCLUDE
11 loads_kwargs = {"unknown": EXCLUDE}
12 except ImportError:
13 pass
14
15
17
18
19
21 super_object = super(Schema, self)
22 super_result = getattr(super_object, name)(*args, **kwargs)
23 if hasattr(super_result, 'data') and hasattr(super_result, 'errors'):
24
25 data = super_result.data
26 if super_result.errors:
27 exception = ValidationError(super_result.errors)
28 exception.valid_data = data
29 raise exception
30 return data
31 else:
32 return super_result
33
34 - def dump(self, *args, **kwargs):
36
37 - def loads(self, *args, **kwargs):
40
42 """
43 :param fn_list: list of callable functions, each takes one param
44 :return: None if at least one validation function exists without exceptions
45 :raises ValidationError: otherwise
46 """
47 def func(value):
48 errors = []
49 for fn in fn_list:
50 try:
51 fn(value)
52 except ValidationError as err:
53 errors.append(str(err))
54 else:
55 return
56 else:
57 errors.insert(0, u"At least one validator should accept given value:")
58 raise ValidationError(errors)
59
60 return func
61
62
65 if value is None:
66 return []
67 return value.split()
68
69 - def _deserialize(self, value, attr=None, data=None, **kwargs):
70 if value is None:
71 return ""
72 elif not isinstance(value, Iterable) or isinstance(value, string_types):
73 raise ValidationError("Value `{}` is not a list of strings"
74 .format(value))
75 else:
76 return " ".join(value)
77
78
80 """ stored in db as a string:
81 "python3-marshmallow 2.0.0b5\npython-marshmallow 2.0.0b5"
82 we would represent them as
83 { name: "pkg", version: "pkg version" }
84 we implement only the serialization, since field is read-only
85 """
87 if value is None:
88 return []
89 result = []
90 try:
91 for chunk in value.split("\n"):
92 pkg, version = chunk.split()
93 result.append({
94 "name": pkg,
95 "version": version
96 })
97 except:
98 pass
99
100 return result
101
102
108
109
114
115
138
139
141 name = fields.Str(
142 required=True,
143 validate=[
144 validate.Regexp(
145 r"^[a-zA-Z][\w.-]*$",
146 error="Name must contain only letters,"
147 "digits, underscores, dashes and dots."
148 "And starts with letter"),
149 ])
150 group = fields.Str(load_only=True, allow_none=True)
151 chroots = SpaceSeparatedList(load_only=True, default=list)
152
153
154
163
164
167
168
179
180
201
202
209
210
213