fix regex validation for url

This commit is contained in:
italojohnny 2024-06-23 19:22:43 -03:00 • committed by Gabriel Luiz Freitas Almeida
commit 576795a471

View file

@ -44,14 +44,16 @@ class URLComponent(Component):
# Basic URL validation regex # Basic URL validation regex
url_regex = re.compile( url_regex = re.compile(
r"^(http://|https://)?" # http:// or https:// r"^(https?:\/\/)?" # optional protocol
r"(([a-zA-Z0-9\.-]+)" # domain r"(www\.)?" # optional www
r"(\.[a-zA-Z]{2,}))" # top-level domain r"([a-zA-Z0-9.-]+)" # domain
r"(:[0-9]{1,5})?" # optional port r"(\.[a-zA-Z]{2,})?" # top-level domain
r"(\/.*)?$" # optional path r"(:\d+)?" # optional port
r"(\/[^\s]*)?$", # optional path
re.IGNORECASE
) )
if not re.match(url_regex, string): if not url_regex.match(string):
raise ValueError(f"Invalid URL: {string}") raise ValueError(f"Invalid URL: {string}")
return string return string