def _validate_array_item(field, value): if value: if not len(value) == 2: raise ValidationError( '2 elements [lon, lat] required in %s' % field._field_name )
def validate_dict(field, value): if value: for key in 'lat', 'lon': if not isinstance(value.get(key), float): raise ValidationError( '%s: %s requires a float' % (field._field_name, key))
def validate_string(field, value): if value: values = [ float(item.strip()) for item in value.split(',') ] if not len(values) == 2: raise ValidationError( '2 elements "lat,lon" required in %s' % field._field_name)
def validate(self): if self._validators: for validator in self._validators: """ Functions in self._validators receives document instance should return None or raise Exception (ValidationError) or return any value """ val = validator(self) if val: raise ValidationError("Invalid: %s" % val)
def validate(self, value): if value is None: if self._required: raise RequiredField(self._field_name) else: if self._multi: if not isinstance(value, Iterable): raise InvalidMultiField(self._field_name) [self.validate_field_type(elem) for elem in value] else: self.validate_field_type(value) for validator in self._validators: """ Functions in self._validators receives field_instance, value should return None or raise Exception (ValidationError) or return any value """ val = validator(self, value) if val: raise ValidationError('Invalid %s, returned: %s' % (self._field_name, val))
def max_len_10(field_name, value): if len(value) > 10: raise ValidationError("Invalid Length")
def if_city_state_is_required(obj): if obj.city and not obj.state: raise ValidationError("If city, state is required")