Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve error messages for URL validation #2324

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/marshmallow/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from abc import ABC, abstractmethod
from itertools import zip_longest
from operator import attrgetter
from urllib.parse import urlparse

from marshmallow import types
from marshmallow.exceptions import ValidationError
Expand Down Expand Up @@ -210,11 +211,32 @@ def __call__(self, value: str) -> str:
if "://" in value:
scheme = value.split("://")[0].lower()
if scheme not in self.schemes:
raise ValidationError(message)
raise ValidationError(
f"Invalid URL scheme '{scheme}'. "
f"Allowed schemes are: {', '.join(self.schemes)}."
)

regex = self._regex(self.relative, self.absolute, self.require_tld)

if not regex.search(value):
if self.require_tld:
try:
# Extract the netloc (hostname and port)
parsed_url = urlparse(value)
hostname = parsed_url.hostname
except (ValueError, TypeError, AttributeError):
hostname = None

if hostname:
# Check if hostname is an IP address
is_ip = re.match(r"\d+\.\d+\.\d+\.\d+", hostname)
# Check if hostname contains a dot (.)
has_tld = "." in hostname
if not is_ip and not has_tld:
raise ValidationError(
"URL must include a top-level domain (e.g., '.com', '.org')."
)
# Default error message for other failures
raise ValidationError(message)

return value
Expand Down