Skip to main content

FileExtensionValidator

This class provides a mechanism to validate that a file's extension matches a predefined list of permitted formats. It extracts the extension from a file object and raises a validation error with a customizable message and code if the extension is not allowed. The validator supports case-insensitive comparisons and can be used as a callable in validation pipelines.

Attributes

AttributeTypeDescription
messagestring = File extension “%(extension)s” is not allowed. Allowed extensions are: %(allowed_extensions)s.The error message used when the file extension is not found in the allowed list, supporting placeholders for extension and allowed_extensions.
codestring = invalid_extensionThe fallback error code used to identify the validation failure in exception handling.

Constructor

Signature

def FileExtensionValidator(
allowed_extensions: list = None,
message: string,
code: string
) - > null

Parameters

NameTypeDescription
allowed_extensionslist = NoneA list of allowed file extensions. Extensions are converted to lowercase.
messagestringCustom error message to be used when validation fails. Defaults to a predefined message.
codestringCustom error code to be used when validation fails. Defaults to 'invalid_extension'.