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.
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 thatpatch_datauses. Entering white space for a field will matchvalidators.Optional(), so validations will be skipped, but the resultingpatch_dictwill contain the field still.For example:
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
optionalflag, yea?For my use case, I'm using a custom validator in place of
validators.Optionalthat doesn't consider' 'falsy.