diff --git a/rest_auth_toolkit/serializers.py b/rest_auth_toolkit/serializers.py index afe537c..f827671 100644 --- a/rest_auth_toolkit/serializers.py +++ b/rest_auth_toolkit/serializers.py @@ -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 @@ -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) + + return data + def create(self, validated_data): return User.objects.create_user( email=validated_data['email'],