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

wtforms.validators.Optional fails when request value is null #851

Closed
Jishnu-Nandhiath opened this issue Aug 2, 2024 · 1 comment
Closed

Comments

@Jishnu-Nandhiath
Copy link

from wtforms import Form
from wtforms.validators import UUID, Optional
class SampleForm(Form):
   public_qr_id = StringField(validators=[Optional(), UUID()])

For a request sample below, where null is passed for the field, it returns error. Optional should also stop the validation chain from continuing for null values, as well.
{
"public_qr_id":null
}

Error:

Python312\Lib\uuid.py", line 172, in __init__
raise TypeError('one of the hex, bytes, bytes_le, fields, '
TypeError: one of the hex, bytes, bytes_le, fields, or int arguments must be given

Environment

  • Python version: 3.12.1
  • wtforms version:3.1.2
@Daverball
Copy link

Optional only applies to formdata and that is intentional, see my previous reply to a similar issue: #842 (comment)

If you find yourself running validate() without first passing formdata you are doing something inherently incorrect and unsupported. If you want to validate data or obj, then you should use something like pydantic.

If you were hell-bent on it you could get WTForms to do it if you write your own validators, but it's not designed for it, so I honestly wouldn't recommend trying it, it will not be reliable. WTForms assumes valid object data.

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

No branches or pull requests

3 participants