QueryString: empty string is not validated like string with length > 1
Closed this issue · 1 comments
Actual Behavior
If I have a form filled validated with query string and wrong values, such as:
Wrong string values
This will trigger the validation:
?order=fewfewfew&sort=fewfew
Wrong string values
This will not trigger the validation
?order=&sort=
Instead you'll have this 500 error:
ERROR:pcapi.routes.error_handlers.generic_error_handlers:Unexpected error on method=GET url=http://localhost:5001/backofficev3/pro/validation/user-offerer?order=&sort=: type object 'UserOfferer' has no attribute ""
Expected Behavior
I expect empty string and string with length to have the same behavior
Reproduction
Fields:
order = wtforms.HiddenField("order", default="desc", validators=(wtforms.validators.Optional(),))
sort = wtforms.HiddenField("sort", default="dateCreated", validators=(wtforms.validators.Optional(),))
Validators:
def validate_order(self, order: wtforms.HiddenField) -> wtforms.HiddenField:
if order.data:
order.data = order.data.lower()
if order.data not in ("asc", "desc"):
raise wtforms.validators.ValidationError("Order can only be asc or desc")
return order
def validate_sort(self, sort: wtforms.HiddenField) -> wtforms.HiddenField:
if sort.data:
if sort.data not in ("dateCreated"):
raise wtforms.validators.ValidationError(f"Sorting by {sort.data} is not allowed")
return sort
Environment
- Python version: 3.10
- wtforms version: 3.0.1
The 500 error you refer to does not seem related to wtforms, or at least you did not provide enough context so I can help you. Have you a traceback somewhere?
I will close the ticket for now, if you think this is actually related with wtforms, we can re-open the bug if you provide a minimal reproducible example, ideally something that one can just copy/paste in a python terminal and that demonstrates your issue. Like:
>>> import wtforms
>>> class F(wtforms.Form):
... order = wtforms.HiddenField()
... sort = wtforms.HiddenField()
>>> f = F(...)
>>> f.validate()
...