Skip to content

Commit

Permalink
Manually validate length in S3UploadURLField
Browse files Browse the repository at this point in the history
This is need so that in api specs there would be no length limit
  • Loading branch information
TheSuperiorStanislav committed Dec 9, 2024
1 parent 9b7e2c0 commit 53de692
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ We follow [Semantic Versions](https://semver.org/).

## Unreleased

- Manually validate length in `S3UploadURLField`
This is need so that in api specs there would be no length limit

## 0.3.0

- Confirm support for python 3.13
Expand Down
20 changes: 10 additions & 10 deletions saritasa_s3_tools/django/drf_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ def __init__(self, **kwargs) -> None:
# a limit specified in model field(which is by default 100), it causes
# confusion for openapi specs validators.
max_length = kwargs.pop("max_length", None)
self.max_length_validator = None
super().__init__(**kwargs)
if max_length is not None:
# Append this validator to enable max length validation after
# converting input url
self.validators.append(
validators.MaxLengthValidator(
limit_value=max_length,
message=lazy_format(
self.error_messages["max_length"],
max_length=max_length,
),
# Manually validate max length so that api specs would generate
# without limit
self.max_length_validator = validators.MaxLengthValidator(
limit_value=max_length,
message=lazy_format(
self.error_messages["max_length"],
max_length=max_length,
),
)
# Append this validator to enable invalid code for spec
Expand Down Expand Up @@ -132,7 +131,8 @@ def to_internal_value(self, data: typing.Any) -> str:
aws_location = default_storage.location # type: ignore
if aws_location and file_url.startswith(aws_location):
file_url = file_url.split(f"{aws_location}/")[-1]

if self.max_length_validator:
self.max_length_validator(file_url)
return file_url

def to_representation(self, value: typing.Any) -> str | None:
Expand Down

0 comments on commit 53de692

Please sign in to comment.