-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
095eadb
commit 33ea79b
Showing
2 changed files
with
40 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import re | ||
|
||
from rest_framework.serializers import ValidationError | ||
|
||
pattern = re.compile( | ||
r"^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|" | ||
r"([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|" | ||
r"([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\." | ||
r"([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$" | ||
) | ||
|
||
|
||
def service(value): | ||
if value not in ["web", "email"]: | ||
raise ValidationError("Service must be 'web' or 'email'.") | ||
return value | ||
|
||
|
||
def file_size(file): | ||
if file.size > 5000000: | ||
raise ValidationError("The file size can not be more than 5000000 bytes.") | ||
return file | ||
|
||
|
||
def domain_name(value): | ||
""" | ||
Return whether or not given value is a valid domain. | ||
See: | ||
https://validators.readthedocs.io/en/latest/_modules/validators/domain.html#domain | ||
""" | ||
res = pattern.match(value) | ||
if res: | ||
return value | ||
else: | ||
raise ValidationError("This field must be a domain name.") |