-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
CharField should not accept collections as valid input #3394
Conversation
c88514f
to
d345256
Compare
I'm not quite sure why the py3x-django18 builds are failing; I can look into it further if you decide this change is a good idea. |
I don't think I mind us coercing numbers to string representations. Collection types should prob fail tho. |
I second @tomchristie |
What is the rationale for allowing numbers? Any time a client passes a number to a string field, it is most likely due to user or client error and should be treated as such. The client should be responsible for "stringifying" any numerical input. |
^ Same goes for booleans passed to a string field. |
I agree with triggering an error when a collection is passed in as a string.
I'm -0 on not allowing numbers mostly because that's expected functionality at the moment and changing this would break backwards compatibility. We even had a test to ensure that numbers were coerced properly. It's also useful to note that there may be renders out there which try their best to coerce string input to the right type, so numbers would become a numeric type (decimal, probably) instead of staying as strings. We allow for the reverse case (numbers passed in as strings to number-type fields), so I don't understand why we would want to disallow this. |
Indeed, most of simple fields accept strings as input. The fields' deserialization logic, however, is responsible for deciding which string inputs are valid (e.g. I lean slightly toward disallowing numbers as inputs to |
At least they should be treated as individually. The case for one pull request is unambiguous, the case for the other less so. |
I'd like to see this be configurable at least. I would imagine passing anything other than a string to a CharField is usually indicative of user error. |
Possibly, tho "Generous on input, strict on output" could also be a valid rule of thumb for us to use here. |
Retitling to reflect the resolution. |
This issue came up on the marshmallow issue tracker (marshmallow-code/marshmallow#231), so I thought I'd pass it along here.
It seems incorrect for
CharField
to take numbers and collections as valid input.Code to reproduce
Expected
Validation fails with messages like
"{} is not a valid string."
.Actual
Numbers and collections pass validation.