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
| Attribute | Type | Description |
|---|---|---|
| message | string = 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. |
| code | string = invalid_extension | The 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
| Name | Type | Description |
|---|---|---|
| allowed_extensions | list = None | A list of allowed file extensions. Extensions are converted to lowercase. |
| message | string | Custom error message to be used when validation fails. Defaults to a predefined message. |
| code | string | Custom error code to be used when validation fails. Defaults to 'invalid_extension'. |