Skip to content

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

Description

@larkin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions