Implement regex check for short code

This commit is contained in:
Gabriel Augendre 2021-05-29 16:45:54 +02:00
parent aa75bab8cf
commit 83abf8a770

View file

@ -1,4 +1,5 @@
import os import os
import re
from typing import Dict from typing import Dict
from urllib.parse import urlparse from urllib.parse import urlparse
@ -53,6 +54,12 @@ def _validate_user_input():
raise BadRequestException( raise BadRequestException(
message="`short_code` must be longer than 4 characters." message="`short_code` must be longer than 4 characters."
) )
reg = re.compile(r"^[a-zA-Z0-9_-]+$")
if not reg.match(short_code):
raise BadRequestException(
message="`short_code` must only contain non accentuated letters, "
"digits, dashes (-) and underscores (_)."
)
if _short_code_exists(short_code): if _short_code_exists(short_code):
raise BadRequestException(message=f"`{short_code}` is already taken") raise BadRequestException(message=f"`{short_code}` is already taken")