Skip to content

Commit

Permalink
fix: small fix to some serializers to improve responses (#86)
Browse files Browse the repository at this point in the history
* refactor: remove any "non_field_error" from responses.

They will always explicitly mention the field that errored validation.

* chore: updates bruno collections data
  • Loading branch information
corp-0 authored Mar 3, 2024
1 parent 126d4c8 commit ae9d0d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions api-collection/Auth/CreateAccount/Invalid identifier.bru
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ post {

body:json {
{
"unique_identifiers": "' 'DROP TABLE USERS;",
"username": "My Name",
"password": "qweasd123",
"email": "myname@email.com"
"unique_identifier": "' 'DROP TABLE USERS;",
"username": " ",
"password": "wasd123",
"email": "mynameemail.com"
}
}
10 changes: 8 additions & 2 deletions api-collection/Auth/Mail confirmation/ConfirmAccount.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ meta {
}

post {
url: {{baseUrl}}/accounts/confirm-account/<token you get via email here>
body: none
url: {{baseUrl}}/accounts/confirm-account
body: json
auth: none
}

body:json {
{
"token": "asdasdasdasdasd"
}
}
18 changes: 12 additions & 6 deletions src/accounts/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class Meta:
fields = ("unique_identifier", "username", "password", "email")
extra_kwargs = {"password": {"write_only": True}}

def validate_password(self, value):
def validate_password(self, value: str):
# Validate the password using Django's built-in validators
temp_user = Account(**self.initial_data)
temp_user = Account(
unique_identifier=self.initial_data.get("unique_identifier"),
username=self.initial_data.get("username"),
)
validate_password(value, temp_user)
return value

Expand Down Expand Up @@ -69,9 +72,12 @@ class VerifyAccountSerializer(serializers.Serializer):
class ResetPasswordSerializer(serializers.Serializer):
password = serializers.CharField(style={"input_type": "password"})

def validate_password(self, value):
def validate_password(self, value: str):
# Validate the password using Django's built-in validators
temp_user = Account(**self.initial_data)
temp_user = Account(
unique_identifier=self.initial_data.get("unique_identifier"),
username=self.initial_data.get("username"),
)
validate_password(value, temp_user)
return value

Expand All @@ -83,10 +89,10 @@ def validate(self, data):
try:
account_confirmation = AccountConfirmation.objects.get(token=data["token"])
except AccountConfirmation.DoesNotExist:
raise serializers.ValidationError("Token is invalid or expired.")
raise serializers.ValidationError({"token": "Token is invalid or expired."})

if not account_confirmation.is_token_valid():
raise serializers.ValidationError("Token is invalid or expired.")
raise serializers.ValidationError({"token": "Token is invalid or expired."})
return {"token": data["token"]}


Expand Down

0 comments on commit ae9d0d4

Please sign in to comment.