Skip to content

Commit

Permalink
[REFACTOR] Refactor input validators to increase reusabilty
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesSetiawan committed Feb 22, 2024
1 parent 96d12b1 commit 5c49d94
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ def post(self, request):
email = data['email']
if AppUser.objects.filter(username = username).exists() or AppUser.objects.filter(email = email).exists():
return JsonResponse({'msg': 'Username and/or email already taken!'}, status=400)
if username == '' or password == '' or email == '':
return JsonResponse({'msg': 'Empty input! Make sure all the fields are filled.'}, status=400)
if re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email) is None:
return JsonResponse({'msg': 'Email format is wrong.'}, status=400)
validate_msg = Validators.validateInput(username, email, password)
if validate_msg != 'valid':
return JsonResponse({'msg': validate_msg}, status=400)
new_user = AppUser.objects.create_user(email=email,username=username,password=password)
new_user.save()
return JsonResponse({'msg': 'Success! User registered'}, status=200)
return JsonResponse({'msg': 'Success! User registered'}, status=200)

class Validators():
def validateInput(username, email, password):
if username == '' or password == '' or email == '':
return 'Empty input! Make sure all the fields are filled.'
if re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email) is None:
return 'Email format is wrong.'
return 'valid'

0 comments on commit 5c49d94

Please sign in to comment.