-
Notifications
You must be signed in to change notification settings - Fork 66
add relative_link to link field #254
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,3 +110,32 @@ def __call__(self, value: str): | |
): | ||
return EmailValidator()(value[7:]) | ||
return super().__call__(value) | ||
|
||
|
||
@deconstructible | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider refactoring the validation logic to use early returns and a helper function for error handling to improve readability. Here’s one way to collapse the nested logic into a straight‐line of early checks plus a single helper for raising errors. This keeps every check, but it’s easier to scan and maintains the same behavior: @deconstructible
class RelativeURLValidator:
message = _("Enter a valid relative link")
code = "invalid"
def __init__(self, allowed_link_types: list = None, **kwargs):
self.allowed_link_types = allowed_link_types
super().__init__(**kwargs)
def __call__(self, value: str):
# simple helper to reduce repetition
def _fail():
raise ValidationError(self.message, code=self.code, params={"value": value})
if not isinstance(value, str):
_fail()
if len(value) > URLValidator.max_length:
_fail()
if URLValidator.unsafe_chars.intersection(value):
_fail()
# must start with “/”
if not value.startswith("/"):
_fail()
# if caller has restricted link types, enforce “relative_link”
if self.allowed_link_types and "relative_link" not in self.allowed_link_types:
_fail()
return value Steps to apply:
|
||
class RelativeURLValidator: | ||
message = _("Enter a valid relative link") | ||
code = "invalid" | ||
|
||
def __init__(self, allowed_link_types: list = None, **kwargs): | ||
self.allowed_link_types = allowed_link_types | ||
super().__init__(**kwargs) | ||
|
||
def __call__(self, value: str): | ||
if not isinstance(value, str) or len(value) > URLValidator.max_length: | ||
raise ValidationError(self.message, code=self.code, params={"value": value}) | ||
if URLValidator.unsafe_chars.intersection(value): | ||
raise ValidationError(self.message, code=self.code, params={"value": value}) | ||
if ( | ||
value.startswith("/") and ( | ||
self.allowed_link_types is not None | ||
and "relative_link" not in self.allowed_link_types | ||
) | ||
or not value.startswith("/") | ||
): | ||
raise ValidationError( | ||
self.message, | ||
code=self.code, | ||
params={"value": value}, | ||
) | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Relative links are now returned directly; consider normalization for leading/trailing whitespace.
Strip whitespace from relative links before returning to prevent subtle bugs.