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

Length validation fails on UUID fields #54

Closed
wkevina opened this issue Jan 18, 2016 · 3 comments

Comments

@wkevina
Copy link

commented Jan 18, 2016

It appears that schemas for models with custom column types, which have a python_type of uuid.UUID, have a Length validator set on them. This fails when in validator.Length.__call__, len() is called on the UUID.

The column type is basically the example from the SQLAlchemy docs, with the addition of a python_type property that returns uuid.UUID.

I could just use the sqlalchemy.dialects.postgresql.UUID, but I don't want to tie myself to just postgresql. Is there a recommended way for handling custom columns with this library?

Stack trace:

../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow_sqlalchemy/schema.py:188: in validate
    return super(ModelSchema, self).validate(data, *args, **kwargs)
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/schema.py:574: in validate
    _, errors = self._do_load(data, many, postprocess=False)
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/schema.py:603: in _do_load
    index_errors=self.opts.index_errors,
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/marshalling.py:283: in deserialize
    index=(index if index_errors else None)
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/marshalling.py:65: in call_and_store
    value = getter_func(data)
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/marshalling.py:276: in <lambda>
    data
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/fields.py:263: in deserialize
    self._validate(output)
../../../../.virtualenvs/server/lib/python3.5/site-packages/marshmallow/fields.py:195: in _validate
    if validator(value) is False:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Length(min=None, max=None, error=None)>, value = UUID('a6f4f8ed-c20d-4f69-b335-88f2292d21e9')

    def __call__(self, value):
>       length = len(value)
E       TypeError: object of type 'UUID' has no len()```

kelvinhammond added a commit to kelvinhammond/marshmallow-sqlalchemy that referenced this issue Sep 18, 2016

@andrewmains12

This comment has been minimized.

Copy link

commented Sep 27, 2016

Thanks for opening this ticket @wkevina and @sloria for the awesome library! I've just been running into this issue myself.

Is there a recommended way for handling custom columns with this library?

I was curious about this issue as well (though it's maybe more broad than this particular ticket). It seems like there could be other instances of this same phenomenon around any sqlalchemy TypeDecorator's/custom types, where the deserialized python type doesn't fit validation extracted from the column. For instance, a datetime stored as a fixed length string would have the same issue (not that this is a good idea compared to using DATETIME etc, just by means of example). Any thoughts on how to address the more general issue where validators that make sense for the sqlalchemy implementation type (e.g. CHAR) don't make sense for the python type (e.g. UUID)? Special casing UUID solves the immediate issue, but it seems like it might pop up in other circumstances as well.

@sloria

This comment has been minimized.

Copy link
Member

commented Oct 2, 2016

Any thoughts on how to address the more general issue where validators that make sense for the sqlalchemy implementation type (e.g. CHAR) don't make sense for the python type (e.g. UUID)?

Very good point @andrewmains12 . One approach would be to remove automatic addition of validators and leave it up to the user to opt in to validators.

I prefer to keep the automatic validators because they meet the "90% use case". Special-casing UUID in this case is justified because marshmallow-sqlalchemy has built-in support for the UUID sqlalchemy type.

So I think the best approach for handling other types is to let the user opt out of the automatically-added validators, which they can do using field_for.

        full_name = field_for(models.Student, 'full_name', validate=[])

sloria added a commit that referenced this issue Oct 2, 2016

@sloria

This comment has been minimized.

Copy link
Member

commented Oct 8, 2016

Fixed by #80

@sloria sloria closed this Oct 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.