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

patch_data should have same truthy test as validators.Optional()? #22

Open
larkin opened this issue May 15, 2014 · 1 comment
Open

patch_data should have same truthy test as validators.Optional()? #22

larkin opened this issue May 15, 2014 · 1 comment

Comments

@larkin
Copy link

larkin commented May 15, 2014

If you're using forms for handling patch requests, you have to use validators.Optional() at the end of your validation chain for all fields.

That said, the test that validators.Optional() uses to determine if a field is blank is different than the test that patch_data uses. Entering white space for a field will match validators.Optional(), so validations will be skipped, but the resulting patch_dict will contain the field still.

For example:

from wtforms import Form, fields, validators

import wtforms_json

wtforms_json.init()

class PatchUserForm(Form):
    email = fields.TextField(
        'email',
        [validators.Email('Invalid email address.'), validators.Optional()]
    )

form = PatchUserForm.from_json({'email': ' '})
assert form.validate()
assert 'email' not in form.patch_data # will fail here.

The argument against this is that people should be able to set fields to null or blank via patch, but in that case wtforms-json could check the field's optional flag, yea?

For my use case, I'm using a custom validator in place of validators.Optional that doesn't consider ' ' falsy.

@alanhamlett
Copy link
Contributor

This is related to Issue #20.

Possible fix would be to monkey-patch validators.Optional to NOT stop validating when the field is blank. That way you could use validators.Optional with validators.DataRequired. When the field is blank, the DataRequired validator raises an error if it was used. When the field is missing, the Optional validator stops validating like normal and patch_data will NOT include that field.

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

2 participants