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

Async validators? #13

Open
omarryhan opened this issue Dec 23, 2018 · 1 comment
Open

Async validators? #13

omarryhan opened this issue Dec 23, 2018 · 1 comment

Comments

@omarryhan
Copy link
Collaborator

Are there any plans for supporting them?

@omarryhan
Copy link
Collaborator Author

omarryhan commented Dec 23, 2018

I just looked at how this could be implemented.

On each of its fields, a form calls field.validate() which then calls field._run_validation_chain(). Unfortunately, fields weren't designed with async in mind.

At this point, I think our options are:

  1. replace field._run_validation_chain method with the one below (Not sure whether or not asyncio.gather will block here) maybe it won't even work!:
import asyncio
from wtforms.fields.core import Field


class AsyncField(Field):
    def _run_validation_chain(self, form, validators):
        async_validators = [validator for validator in validators if asyncio.iscoroutinefunction(validator)]
        # Probably shouldn't use create_task as it's py37 exclusive.
        tasks = [asyncio.create_task(async_validator(form, field)) for validator in validators]
        return asyncio.gather(*tasks) if tasks else False  # False as in no errors found
  1. Provide our own Fields e.g. StringField, EmailField etc. and let all custom fields inherit from AsyncField instead of Field.
  2. In SanicForm.validate_on_submit loop over each field in self._fields. If a field has any async validators, validate seperately. Then delete the async validators and continue with normal validation routine.
  3. Mehhh, async validators suck...

What do you guys think?

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

1 participant