Skip to content

Instantly share code, notes, and snippets.

@vinodkv2511
Created November 22, 2018 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vinodkv2511/3b8fcd871be3cbf321710836497b54c3 to your computer and use it in GitHub Desktop.
Save vinodkv2511/3b8fcd871be3cbf321710836497b54c3 to your computer and use it in GitHub Desktop.
@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