Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add MaxFileSizeValidator and MinFileSizeValidator for file size constraints#9738

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mahdirahimi1999 wants to merge2 commits intoencode:master
base:master
Choose a base branch
Loading
frommahdirahimi1999:feature/file-size-validators

Conversation

mahdirahimi1999
Copy link
Contributor

@mahdirahimi1999mahdirahimi1999 commentedJul 12, 2025
edited
Loading

Add file size validators for FileField and ImageField

Adds two new validators to enforce file size constraints on uploaded files:

Background

In many of our DRF and Django projects, we work extensively with FileField and ImageField and need to validate file sizes. I've been creating these validators locally, and other developers have been implementing similar solutions. I thought it would be beneficial to create a standardized class that can be added to DRF and used across our projects.

New Validators

  • MaxFileSizeValidator: Ensures files don't exceed a maximum size (in bytes)
  • MinFileSizeValidator: Ensures files meet a minimum size (in bytes)

Features

  • Customizable error messages and codes
  • Deconstructible for migrations
  • Works with FileField and ImageField
  • Gracefully handles objects without .size attribute

Usage

fromrest_framework.validatorsimportMaxFileSizeValidator,MinFileSizeValidatorclassFileUploadSerializer(serializers.Serializer):file=serializers.FileField(validators=[MaxFileSizeValidator(1024*1024),# 1MB maxMinFileSizeValidator(1024),# 1KB min    ])

Implementation

  • Added torest_framework/validators.py
  • 77 new tests covering unit and integration scenarios
  • Documentation added todocs/api-guide/validators.md
  • All existing tests pass (1577 total)

Error Codes

  • max_file_size - Default for MaxFileSizeValidator
  • min_file_size - Default for MinFileSizeValidator

Add two new validators for enforcing file size constraints on uploaded files:- MaxFileSizeValidator: Ensures files do not exceed a maximum size (in bytes)- MinFileSizeValidator: Ensures files meet a minimum size (in bytes)Both validators:- Support custom error messages and codes- Are deconstructible for migrations- Include comprehensive unit and integration tests- Work with FileField and ImageField- Follow DRF conventions and patterns
- Remove top-level PIL import from tests/test_validators.py- Conditionally import PIL inside image-related tests and skip them if Pillow is unavailable
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@mahdirahimi1999

[8]ページ先頭

©2009-2025 Movatter.jp