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

Unexpected validation behaviour with empty strings in data #53

Open
kshade opened this issue Oct 17, 2017 · 2 comments
Open

Unexpected validation behaviour with empty strings in data #53

kshade opened this issue Oct 17, 2017 · 2 comments

Comments

@kshade
Copy link

kshade commented Oct 17, 2017

I've found some unexpected (to me) behaviour in WTForms-JSON where an empty string in an optional string field with a minimum length did not result in a validation error nor exclusion of the field from patch_data. I've written a small test script, here is the output I'm getting from it:

WTForms-JSON version 0.3.3

json {}
errors {}
form.data {'name': None, 'potato': None}
form.patch_data {}

json {'name': ''}
errors {}
form.data {'name': None, 'potato': None}
form.patch_data {'name': None}

json {'name': 'a'}
errors {'name': ['Field must be between 2 and 128 characters long.']}
form.data {'name': 'a', 'potato': None}
form.patch_data {'name': 'a'}

json {'name': 'ab'}
errors {}
form.data {'name': 'ab', 'potato': None}
form.patch_data {'name': 'ab'}

json {'potato': None}
errors {}
form.data {'name': None, 'potato': None}
form.patch_data {'potato': None}

json {'potato': ''}
errors {}
form.data {'name': None, 'potato': None}
form.patch_data {'potato': None}

json {'potato': 'minimum'}
errors {'potato': ['Invalid value, must be one of: maximum.']}
form.data {'name': None, 'potato': 'minimum'}
form.patch_data {'potato': 'minimum'}

json {'potato': 'maximum'}
errors {}
form.data {'name': None, 'potato': 'maximum'}
form.patch_data {'potato': 'maximum'}

Length validation doesn't happen with the second test case, but it also isn't removed from patch_data like in the first. Potato intuitively should only ever be "maximum" or not present, but both an empty string and None are accepted as None.

@kshade
Copy link
Author

kshade commented Oct 17, 2017

I've tried to debug the problem, it looks like the validation chain is stopped because the Optional validator treats the empty string as the field not being present, but patch_data() still tries to read the field's data value which is set to None.

debug

@kshade
Copy link
Author

kshade commented Dec 7, 2017

Hello?

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