Skip to content

Commit

Permalink
#43 Translate user serializer errors messages
Browse files Browse the repository at this point in the history
Co-authored-by: Leonardo da Silva Gomes <leonardodasigomes@gmail.com>
  • Loading branch information
KiSobral and LeoSilvaGomes committed Sep 30, 2019
1 parent a955c12 commit 84147f8
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class UserRegistrationSerializer(serializers.ModelSerializer):
confirm_password = serializers.CharField(
write_only=True,
required=True,
label="Confirm Password",
style={'input_type': 'password'}
label="Confirm Password",
style={'input_type': 'password'}
)

class Meta:
Expand All @@ -29,28 +29,29 @@ class Meta:

def validate_email(self, email):
if User.objects.filter(email=email).exists():
raise serializers.ValidationError('Email already exists.')
raise serializers.ValidationError('Email já cadastrado')
return email


def validate_password(self, password):
min_length = getattr(settings, 'PASSWORD_MIN_LENGTH', 8)
if len(password) < min_length:
raise serializers.ValidationError(
'Password should be atleast %s characters long.' % (min_length)
'A senha deve ter no mínimo %s caracteres' % (min_length)
)
return password

def validate_confirm_password(self, password_confirmation):
data = self.get_initial()
password = data.get('password')
if password != password_confirmation:
raise serializers.ValidationError('Passwords must match.')
raise serializers.ValidationError('As senhas devem corresponder')
return password_confirmation

def validate_username(self, username):
username = property(lambda self: self.name or self.email.replace(" ", "_"))
if User.objects.filter(username=username).exists():
raise serializers.ValidationError('Email already exists.')
raise serializers.ValidationError('Usuário com este nome já cadastrado')
return username

def create(self, validated_data):
Expand Down

0 comments on commit 84147f8

Please sign in to comment.