Beispiel #1
0
 def _check(self, value):
   if len(value) != len(self._fields):
     msg = "expected {} elements, got {}".format(len(self._fields), len(value))
     raise ValidationFailed(msg, value=value)
   for i, (t, el) in enumerate(zip(self._fields, value)):
     with context.index(i):
       run_check(t.check, el)
Beispiel #2
0
 def _check(self, value):
   if value is None:
     raise ValidationFailed("expected Object")
   reasons = []
   for name, t in self._fields.items():
     subvalue = value.get(name)
     try:
       with context.member(name):
         run_check(t.check, subvalue)
     except ValidationFailed as e:
       if subvalue is None:
         reasons.append(ValidationFailed("missing field '{}'".format(name)))
       else:
         reasons.append(e)
   for name in value.__dict__:
     if name not in self._fields:
       reasons.append(ValidationFailed("unexpected field '{}'".format(name)))
   if reasons:
     if len(reasons) == 1:
       raise reasons[0]
     raise ValidationFailed(", ".join(r.format() for r in reasons))
Beispiel #3
0
 def _check(self, value):
   for i, el in enumerate(value):
     with context.index(i):
       run_check(self._t.check, el)