-
-
Save vinodkv2511/3b8fcd871be3cbf321710836497b54c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@staticmethod | |
def validate_required_input(param, value): | |
""" | |
Function to validate the required input of post method | |
:param param: It can take one of the values from required param of post method | |
:param value: Value of the passed param | |
:return: value if value passes the validation criteria for the given param | |
:raises: ValidationError: if value doesn't pass the validation criteria for the given param | |
""" | |
if param == 'username': | |
if value is not None and type(value) == str and len(value) > 0: | |
if User.objects.filter(username=value).exists(): | |
raise ValidationError('Username already taken, please try with a different username') | |
return value | |
else: | |
raise ValidationError('Invalid username, it can\'t be empty') | |
elif param == 'password': | |
if value is not None and type(value) == str and len(value) >= 8: | |
return value | |
else: | |
raise ValidationError('Invalid Password, password should be at least 8 characters long') | |
elif param == 'email': | |
if value is not None and type(value) == str and len(value) > 0: | |
try: | |
validate_email(value) | |
except ValidationError: | |
raise ValidationError('Invalid Email') | |
else: | |
if User.objects.filter(email=value).exists(): | |
raise ValidationError('E-mail already in use, please try logging in instead') | |
return value | |
else: | |
raise ValidationError('Invalid Email') | |
else: | |
raise ValidationError('Invalid Input Param Passed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment