8000 Last few validators I should need. · Web5design/github3.py@7a1d5f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a1d5f6

Browse files
committed
Last few validators I should need.
1 parent e9b29d0 commit 7a1d5f6

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

github3/structs.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ def validate(self):
112112
# If we let items pass through that we don't validate, this
113113
# would be a pointless exercise
114114

115-
for key, validator in self.schema.items():
115+
for key, (required, validator) in self.schema.items():
116116
if key not in self.params:
117117
continue
118118
value = self.params[key]
119119
if not validator.is_valid(value):
120+
if required:
121+
raise ValueError(
122+
'Key "{0}" is required but is invalid.'.format(key)
123+
)
120124
self.remove_key(key)
121125
else:
122126
self.set_key(key, validator.convert(value))
@@ -194,8 +198,7 @@ def is_valid(self, string):
194198

195199
return True
196200

197-
@staticmethod
198-
def convert(string):
201+
def convert(self, string):
199202
return string if isinstance(string, basestring) else str(string)
200203

201204

@@ -206,5 +209,29 @@ def is_valid(self, dictionary):
206209
except ValueError:
207210
return False
208211

209-
if self.sub_schema:
210-
for
212+
schema_items = self.sub_schema.items()
213+
return any(
214+
[v.is_valid(dictionary[k]) for (k, v) in schema_items]
215+
)
216+
217+
def convert(self, dictionary):
218+
dictionary = dict(dictionary)
219+
for k, v in self.sub_schema.items():
220+
if dictionary.get(k):
221+
dictionary[k] = v.convert(dictionary[k])
222+
return dictionary
223+
224+
225+
class ListValidator(BaseValidator):
226+
def is_valid(self, lyst):
227+
try:
228+
lyst = list(lyst)
229+
except TypeError:
230+
return False
231+
232+
is_valid = self.sub_schema.is_valid
233+
return all([is_valid(i) for i in lyst])
234+
235+
def convert(self, lyst):
236+
convert = self.sub_schema.convert
237+
return [convert(i) for i in lyst]

0 commit comments

Comments
 (0)
0