Skip to content

validators

django_spire.file.validators

FileValidator dataclass

size_bytes_max = 10 * 1024 * 1024 class-attribute instance-attribute

allowed_extensions = None class-attribute instance-attribute

blocked_extensions = field(default_factory=(lambda: DEFAULT_BLOCKED_EXTENSIONS)) class-attribute instance-attribute

content_validation = True class-attribute instance-attribute

__post_init__

Source code in django_spire/file/validators.py
def __post_init__(self) -> None:
    if self.size_bytes_max <= 0:
        message = 'size_bytes_max must be positive.'
        raise ValueError(message)

    if self.allowed_extensions is not None and self.blocked_extensions:
        overlap = self.allowed_extensions & self.blocked_extensions

        if overlap:
            message = f'Extensions cannot be both allowed and blocked: {overlap}'
            raise ValueError(message)

validate

Source code in django_spire/file/validators.py
def validate(self, file: InMemoryUploadedFile) -> None:
    self._validate_not_empty(file)
    self._validate_filename(file)
    self._validate_size(file)
    self._validate_extension(file)

    if self.content_validation:
        self._validate_content(file)