def validate(self, val): super().validate(val) if val is None: return val else: val = self.type(val) if not isinstance(val, six.string_types): raise ValidationError(u'{}: {} is not string type'.format(self.name or 'value', val)) if len(val) > self.length: raise ValidationError(u'{}: {} is not over size: {}'.format(self.name or 'value', val, self.length)) return val
def __setitem__(self, field_name: str, field_val): if self.__META__.has_field(field_name): self._data[field_name] = self.__META__.get_field_type( field_name).validate(field_val) self._actived_fields[field_name] = True else: raise ValidationError( u'Unkown Field: {} In Valid Fields: {}'.format( field_name, self.__META__.fields))
def validate(self, val): super().validate(val) if isinstance(val, str): val = self.type(val) elif isinstance(val, dict): val = val else: raise ValidationError(u'{}: {} is not string type or {} type'.format(self.name or 'value', val, self.type)) return val
def validate(self, val): super().validate(val) val = self.type(val) if val is not None: if val > self.max or val < self.min: raise ValidationError(u'{}: {} not in [{}, {}]'.format(self.name, val, self.min, self.max)) else: pass return val
def wrapped_func(*args, **kwargs): args_type_count = len(args_type) + len(kwargs_type) args_count = len(args) + len(kwargs) if args_type_count > args_count: raise ValidationError(u'Param Types Count: {} != Params Count: {}'.format(args_type_count, args_count)) for idx, type_arg in enumerate(zip(args_type, args)): arg_type, arg = type_arg if not isinstance(arg, arg_type): raise ValidationError( u'Param Type: {} != Given Param Type: {}, Arg Index: {}'.format( arg_type, type(arg), format(idx))) for kwarg_name, kwarg_type in kwargs_type.items(): kwarg = kwargs.get(kwarg_name) if not isinstance(kwarg, kwarg_type): raise ValidationError( u'Param Type: {} != Given Param Type: {}, Key Arg Name: {}'.format( kwarg_type, type(kwarg), kwarg_name)) return func(*args, **kwargs)
def __getitem__(self, field_name: str): if self.__META__.has_field( field_name) and field_name in self._actived_fields: if field_name in self._data: return self._data[field_name] else: raise EmptyError(u'Empty Value: {}'.format(field_name)) else: raise ValidationError( u'Unkown Field: {} In Valid Fields: {}'.format( field_name, self.__META__.fields))
def validate(self, val): super().validate(val) if isinstance(val, six.string_types): val = datetime.datetime.fromtimestamp(float(val)) elif isinstance(val, self.type): pass elif isinstance(val, datetime.datetime): val = val.time() else: raise ValidationError(u'{}: {} is not valid'.format(self.name or 'value', val, self.type)) return val
def validate(self, val): super().validate(val) if isinstance(val, six.string_types): try: val = parser.isoparser().parse_isotime(val) except Exception: val = parser.parse(val) elif isinstance(val, self.type): pass else: raise ValidationError(u'{}: {} is not string type or {} type'.format(self.name or 'value', val, self.type)) return val
def validate(self, val: object) -> object: if self.required and val is None: raise ValidationError(u'{}: is not null but got null'.format(self.name or 'value')) return val