def dumps(self, d: dict) -> dict: result = {} errors = [] for k, field in self.fields.items(): v = d.get(k) # resolve value is empty if v is None: if field.required: errors.append( ValidationException( "the field {k} is required".format(k=k))) continue if field.default is None: continue if isinstance(field.default, typing.Callable): v = field.default() else: v = field.default try: serialized = field.dumps(v) except ValidationException as e: errors.append(e) continue result[field.dump_to or k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def dumps(self, d, name=None, **kwargs): result = {} errors = [] for k, field in self.fields.items(): v = d.get(k) if v is None: if field.required: errors.append( ValidationException("the field {k} is required".format(k=k)) ) continue if field.default is None: continue if isinstance(field.default, compact.Callable): v = field.default() else: v = field.default try: serialized = field.dumps(v, name=k) except ValidationException as e: errors.extend(e.errors) continue result[field.dump_to or k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def dumps(self, d: dict) -> dict: result = {} errors = [] for k, field in self.fields.items(): v = d.get(k) # resolve value is empty if v is None: if field.required: errors.append( ValidationException( "the field {k} is required".format(k=k))) continue if field.default is None: continue if isinstance(field.default, typing.Callable): v = field.default() else: v = field.default try: serialized = field.dumps(v) except ValidationException as e: errors.append(e) continue k = field.dump_to or k if isinstance(field, fields.List): for i, item in enumerate(serialized): if not isinstance(field.item, RequestSchema): result["{k}.{i}".format(k=k, i=i)] = item continue for item_k, item_v in item.items(): result["{k}.{i}.{item_k}".format( k=k, i=i, item_k=item_k)] = item_v elif isinstance(field, RequestSchema): for dk, dv in serialized.items(): result["{k}.{dk}".format(k=k, dk=dk)] = dv else: result[k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def dumps(self, d: dict, name=None, **kwargs) -> dict: if not isinstance(d, dict): raise ValidationException( "invalid field {}, expect dict, got {}".format(name, type(d))) result = super(RequestSchema, self).dumps(d, name=name, **kwargs) return result
def loads(self, d: dict, name=None, **kwargs) -> dict: result = {} errors = [] if not self.case_sensitive: d = {k.lower(): v for k, v in d.items()} for k, field in self.fields.items(): load_key = field.load_from or k v = d.get(load_key if self.case_sensitive else load_key.lower()) if v is None: if field.default is None: continue if isinstance(field.default, compat.Callable): v = field.default() else: v = field.default try: serialized = field.loads(v, name=k) except ValidationException as e: errors.extend(e.errors) continue result[k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def loads(self, value, name=None, **kwargs): if not isinstance(value, collections.Iterable): raise ValidationException( "invalid field {}, expect list, got {}".format(name, type(value)) ) errors = [] values = [] for each in value: try: v = self.item.loads(each) except ValidationException as e: errors.extend(e.errors) else: values.append(v) if len(errors) > 0: raise ValidationException(errors) return values
def loads(self, value, **kwargs): if not isinstance(value, collections.Iterable): raise ValidationException("invalid str") errors = [] values = [] for each in value: try: v = self.item.loads(each) except ValidationException as e: errors.append(e) else: values.append(v) if len(errors) > 0: raise ValidationException(errors) return values
def loads(self, d, name=None, **kwargs): result = {} errors = [] for k, field in self.fields.items(): v = d.get(field.load_from or k) try: serialized = field.loads(v, name=k) except ValidationException as e: errors.extend(e.errors) continue result[k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def loads(self, d: dict) -> dict: result = {} errors = [] for k, field in self.fields.items(): v = d.get(field.load_from or k) try: serialized = field.loads(v) except ValidationException as e: errors.append(e) continue result[k] = serialized if len(errors) > 0: raise ValidationException(errors) return result
def loads(self, value, name=None, **kwargs): if not isinstance(value, int): raise ValidationException( "invalid field {}, expect int, got {}".format(name, type(value)) ) return int(value)
def loads(self, value, name=None, **kwargs): if not isinstance(value, compact.string_types): raise ValidationException( "invalid field {}, expect str, got {}".format(name, type(value)) ) return value
def dumps(self, value, name=None, **kwargs): if not isinstance(value, bool): raise ValidationException( "invalid field {}, expect bool, got {}".format(name, type(value)) ) return bool(value)
def dumps(self, value, **kwargs): if not isinstance(value, float): raise ValidationException("invalid float") return float(value)
def loads(self, value, **kwargs): if not isinstance(value, int): raise ValidationException("invalid int") return int(value)
def loads(self, value, **kwargs): if not isinstance(value, str): raise ValidationException("invalid str") return str(value)
def loads(self, value, **kwargs): if not isinstance(value, bool): raise ValidationException("invalid bool") return bool(value)