-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Fix boolean validation when string is passed as value #1866
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
Conversation
@@ -454,7 +454,7 @@ def from_native(self, value): | |||
return True | |||
if value in ('false', 'f', 'False', '0'): | |||
return False | |||
return bool(value) | |||
raise ValidationError(self.error_messages['invalid'] % _(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced it's a good idea to raise a ValidationError within the from_native. Shouldn't it be in the validate ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect behaviour if value is True
, False
, 1 and 0.
Closing this off. On my checklist for #1800. |
Any remote chance to have it in 2.x? |
Nope. Cracking on with 3.0, rather focus on that. The behaviour as it currently is reasonable enough in any case. If you want alternate behavior, use a custom field class. |
@tomchristie Regarding this being "reasonable" -- part of the huge amount of hate against PHP is the fact that it has automatic conversions such as this. Do you want to make DRF work like PHP? |
@AeroNotix Please avoid such negative statements. |
@xordoquy I don't see the negativity. I'm genuinely surprised at the behaviour introduced in the 3.0 branch. The allowed values don't even make sense for a boolean field for the serialized types available. TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True))
FALSE_VALUES = set(('f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False)) Which formats allow '0' or 0 or 0.0 to represent false? DRF supports JSON (which does not support anything other than true/false), YAML (which supports a couple of things like NO and 'N', yet you do not support them here). Ideally the allowed values would take into account the format being parsed and validate based on those values. |
@AeroNotix the "reasonable" part applies to the current branch. |
the following example:
does behave in such case: