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

Add validate function for passwords in SignupDeserializer #39

Merged
merged 10 commits into from
Oct 16, 2018
19 changes: 19 additions & 0 deletions rest_auth_toolkit/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth import password_validation
from django.core import exceptions
from django.utils.translation import gettext as _

from rest_framework import serializers
Expand All @@ -24,6 +26,23 @@ class Meta:
'password': {'style': {'input_type': 'password'}},
}

def validate(self, data):
password = data['password']

# Create user object without saving it to get extra checks by validators
user = User(**data)

errors = {}
try:
password_validation.validate_password(password=password, user=user)
except exceptions.ValidationError as e:
errors['password'] = list(e.messages)

if errors:
raise serializers.ValidationError(errors)
JulienLabonte marked this conversation as resolved.
Show resolved Hide resolved

return data

def create(self, validated_data):
return User.objects.create_user(
email=validated_data['email'],
Expand Down