-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
Hi team,
Currently, validating the size of an uploaded file (e.g., setting a max/min size for a profile picture) requires writing custom validator logic. This is a very common task in many projects that handle file uploads.
I think it would be really helpful for the framework to provide built-in validators for this. I'm proposing the addition of MaxFileSizeValidator
and MinFileSizeValidator
.
They could be used directly in a serializer like this:
from rest_framework import serializers
from rest_framework.validators import MaxFileSizeValidator, MinFileSizeValidator
class FileUploadSerializer(serializers.Serializer):
file = serializers.FileField(validators=[
MaxFileSizeValidator(1024 * 1024), # 1MB max
MinFileSizeValidator(1024), # 1KB min
])
This would save developers from writing boilerplate code and provide a standard, reliable way to handle a common validation scenario for both FileField and ImageField.
I've already opened a pull request with a full implementation, including tests and documentation:
Related PR: #9738
Would love to hear your thoughts. Thanks!