Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get error when I set fields as tuples instead of lists #271

Closed
theosotr opened this issue Sep 27, 2016 · 3 comments
Closed

Get error when I set fields as tuples instead of lists #271

theosotr opened this issue Sep 27, 2016 · 3 comments

Comments

@theosotr
Copy link

I have the following piece of code.

from cerberus import Validator


schema = {
    'my_field': {
        'type': 'list',
        'schema': {'type': 'string'}
    }
}

validator = Validator(schema)

data = {'my_field': ('foo', 'bar')}

validator.validate(data)

I get the following exception:
TypeError: 'tuple' object does not support item assignment

When I set my_field as list works fine. However, I would like cerberus to support tuple objects as well.

@funkyfuture
Copy link
Member

funkyfuture commented Sep 27, 2016 via email

@andreymal
Copy link

andreymal commented Sep 27, 2016

I'm not author, but I could:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 739, in validate
    self.__normalize_mapping(self.document, self.schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 499, in __normalize_mapping
    self.__normalize_containers(mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 564, in __normalize_containers
    self.__normalize_sequence(field, mapping, schema)
  File "/home/andreymal/.local/lib/python3.5/site-packages/cerberus/validator.py", line 620, in __normalize_sequence
    mapping[field][i] = result[i]
TypeError: 'tuple' object does not support item assignment

Generally, Cerberus modifies input, I think this is very bad:

original = [1, 2]

schema = {
    'my_field': {'type': 'list', 'schema': {'type': 'string', 'coerce': str}}
}

validator = Validator(schema)
validator.validate({'my_field': original})

print(original == [1, 2])  # False — WAT?
print(original == ['1', '2'])  # True — WAAT??!1

@funkyfuture
Copy link
Member

@theosotr support for normalizing tuples would have to be amended. it's certainly welcome. in your case you could also avoid hitting the normalization code by calling validate with normalize=False.

@andreymal this behavior has recently been amended to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants