Skip to content

Commit 2dbe578

Browse files
authored
fix(api): Fix MultipleChoiceField custom error message (#53184)
1 parent 1e781e9 commit 2dbe578

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from rest_framework import serializers
22

3+
ERROR_MESSAGES = {
4+
"invalid_choice": ("Select a valid choice. {value} is not one of " "the available choices.")
5+
}
36

4-
class MultipleChoiceField(serializers.Field):
5-
error_messages = {
6-
"invalid_choice": ("Select a valid choice. {value} is not one of " "the available choices.")
7-
}
87

8+
class MultipleChoiceField(serializers.Field):
99
def __init__(self, choices=None, *args, **kwargs):
1010
self.choices = set(choices or ())
1111
super().__init__(*args, **kwargs)
@@ -18,7 +18,7 @@ def to_internal_value(self, data):
1818
for item in data:
1919
if item not in self.choices:
2020
raise serializers.ValidationError(
21-
self.error_messages["invalid_choice"].format(value=item)
21+
ERROR_MESSAGES["invalid_choice"].format(value=item)
2222
)
2323
return data
2424
raise serializers.ValidationError("Please provide a valid list.")

tests/sentry/api/endpoints/test_api_tokens.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def test_never_cache(self):
6060
== "max-age=0, no-cache, no-store, must-revalidate, private"
6161
)
6262

63+
def test_invalid_choice(self):
64+
self.login_as(self.user)
65+
url = reverse("sentry-api-0-api-tokens")
66+
response = self.client.post(
67+
url,
68+
data={
69+
"scopes": [
70+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
71+
]
72+
},
73+
)
74+
assert response.status_code == 400
75+
assert not ApiToken.objects.filter(user=self.user).exists()
76+
6377

6478
@control_silo_test(stable=True)
6579
class ApiTokensDeleteTest(APITestCase):

0 commit comments

Comments
 (0)