@@ -112,11 +112,15 @@ def validate(self):
112
112
# If we let items pass through that we don't validate, this
113
113
# would be a pointless exercise
114
114
115
- for key , validator in self .schema .items ():
115
+ for key , ( required , validator ) in self .schema .items ():
116
116
if key not in self .params :
117
117
continue
118
118
value = self .params [key ]
119
119
if not validator .is_valid (value ):
120
+ if required :
121
+ raise ValueError (
122
+ 'Key "{0}" is required but is invalid.' .format (key )
123
+ )
120
124
self .remove_key (key )
121
125
else :
122
126
self .set_key (key , validator .convert (value ))
@@ -194,8 +198,7 @@ def is_valid(self, string):
194
198
195
199
return True
196
200
197
- @staticmethod
198
- def convert (string ):
201
+ def convert (self , string ):
199
202
return string if isinstance (string , basestring ) else str (string )
200
203
201
204
@@ -206,5 +209,29 @@ def is_valid(self, dictionary):
206
209
except ValueError :
207
210
return False
208
211
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