jaimegildesagredo / booby

Data modeling and validation Python library
https://booby.readthedocs.org
Other
177 stars 18 forks source link

Booby model does not validate properly with ListField attribute #3

Closed Rustem closed 11 years ago

Rustem commented 11 years ago

Example: class User(models.Model): letters = fields.ListField(validators.String, validators.In(choices=['a', 'b']))

, or

class User(models.Model): letters = fields.ListField(validators.List(validators.String, validators.In(choices=['a', 'b'])))

So if I create instance: u = User(phones=[12345]) u.validate()

Nothing raised, because LIstField ignores all validators that are not subclass of Model. It would be good to add that feature.

class TestListWithInners(object):

def test_when_list_field_raises_then_model_raises(self):
    with assert_raises(errors.ValidationError):
        c = Cell(m='a')
        c.validate()
    c = Cell(m=self.test_b)
    with assert_raises(errors.ValidationError):
        self.validator.validate(Cell(m=[1, 2, 3, 4]))
    c.validate()

def setup(self):
    self.test_a = ['a', 'b', 'c']
    self.test_b = [1, 2]
    self.test_c = ['a', 'ww']
    self.model = Cell(m=['a', 'b', 'c'])
    self.validator = validators.Model(Cell)
jaimegildesagredo commented 11 years ago

Hi Rustem!

I think you're wrong with your issue because Booby does not have a ListField yet (maybe you have used another fork of this repo). Anyway we're working on a ListField implementation and your comment is very useful for that.

By the way you could do what you want using a simple Field and the List validator as follows:

from booby import models, fields, validators

class User(models.Model):
    phones = fields.Field(validators.List(validators.String, validators.In(['a', 'b'])))

The point is to pass a list of validators for each list element to the List constructor and then the List validator to the Field.