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,6 +1,8 @@
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.utils.translation import gettext as _
from django.core import exceptions
import django.contrib.auth.password_validation as validators

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

def validate(self, data):
user = User(**data)

password = data.get('password')

errors = dict()
try:
validators.validate_password(password=password, user=user)

except exceptions.ValidationError as e:
errors = list(e.messages)

if errors:
raise serializers.ValidationError(errors)

return data

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