kiddouk / redisco

A Python Library for Simple Models and Containers Persisted in Redis
MIT License
438 stars 78 forks source link

Apply type validation to falsy values as well #9

Closed jkbrzt closed 12 years ago

jkbrzt commented 12 years ago

For example, this instance would previously get saved:

class Person(models.Model):
    age = models.IntegerField()

>>> p = Person(age=[])
>>> p.save()
True

…and fail with a value error when loaded:

>>> Person.objects.get_by_id(p.id)
Traceback (most recent call last):
  File "<ipython-input-5-623349df4911>", line 1, in <module>
    Person.objects.get_by_id(p.id)
  File "redisco/models/managers.py", line 40, in get_by_id
    return self.get_model_set().get_by_id(id)
  File "redisco/models/modelset.py", line 63, in get_by_id
    return self._get_item_with_id(id)
  File "redisco/models/modelset.py", line 276, in _get_item_with_id
    instance.id = str(id)
  File "redisco/models/base.py", line 328, in id
    att.__set__(self, att.typecast_for_read(stored_attrs[att.name]))
  File "redisco/models/attributes.py", line 153, in typecast_for_read
    return int(value)
ValueError: invalid literal for int() with base 10: '[]'

With this patch:

>>> p = Person(age=[])
>>> p.save()
[('age', 'bad type')]
travisbot commented 12 years ago

This pull request passes (merged 33556ef8 into 486a1cbf).

kiddouk commented 12 years ago

Thanks a bunch for that @jkbr .