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

UUID validator fails if field.data is a UUID object instead of str #549

Open
caumons opened this issue Apr 17, 2020 · 2 comments · May be fixed by #769
Open

UUID validator fails if field.data is a UUID object instead of str #549

caumons opened this issue Apr 17, 2020 · 2 comments · May be fixed by #769

Comments

@caumons
Copy link

caumons commented Apr 17, 2020

The current implementation of UUID validator at https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L520 assumes that field.data is of type str. However, if field.data is already an instance of uuid.UUID the validator fails with the following exception: AttributeError: 'UUID' object has no attribute 'replace'.

I've found this issue working with PostgreSQL, SQLAlchemy and using Flask-Admin with a field declared as:

    from sqlalchemy.dialects.postgresql import UUID
    import sqlalchemy as sa

    # ... More code here
    uuid = db.Column(
        UUID(as_uuid=True), nullable=False, default=uuid4,
        server_default=sa.text('uuid_generate_v4()'), index=True)

Note the use of as_uuid=True as said at pallets-eco/flask-admin#1444.

My proposal is to change this line uuid.UUID(field.data) by uuid.UUID(str(field.data)), this way it should work in both cases.

Meanwhile, I've done the following workaround implementing a new UUID validator overriding the __call__() method as follows:

    import uuid

    from wtforms.validators import UUID


    class SecureUUID(UUID):
        def __call__(self, form, field):
            if isinstance(field.data, uuid.UUID):
                field.data = str(field.data)
            super().__call__(form, field)
@azmeuk
Copy link
Member

azmeuk commented Apr 17, 2020

Thank you for reporting this issue.
The patch you propose seems pretty simple, would you submit a pull-request for that? If you do, do not forget to provide a unit test.

@TurnrDev
Copy link

Any reason this hasn't been fixed yet?

@TurnrDev TurnrDev linked a pull request Feb 9, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants