Skip to main content
Version: 1.X.X

File Validator

In the 0.X.X version, each of the File Validats was a separate function but in this version they are all become a class

In the new version you can validate files based on extension, mime and size separately

How Used?​

First Imported​

You must first import FileValidator to use:

from file_validator.validators import FileValidator

Create Instance​

At this point you should make an instance from the FileValidator class:

file_validator = FileValidator(
acceptable_extensions=[".png"],
max_upload_file_size=1000000,
acceptable_types=["image", "audio"],
acceptable_mimes=["image/png"],
file_path="path/to/file",
)

Parameters explanation​

info
ParametersTypeDescription
max_upload_file_sizeint optionalIf you want the file size to be checked, the file size must be in bytes,
example: max_upload_file_size=1048576 (1MB)
defaults to None
acceptable_extensionslist optionalThe extensions you want the file to be checked based on.
example: acceptable_extensions=[".png"]
acceptable_typeslistThe types you want the file to be checked based on.
example: acceptable_types=['audio', 'video']
acceptable_mimeslistThe mimes you want the file to be checked based on.
example: acceptable_mimes=['audio/mpeg', 'video/mp4']
file_pathstringThe file path that you want to be validated

python-magic Library​

If you want to perform file validation operations by the python-magic Library, you should use the python_magic() method as follows:

file_validator.python_magic()

pure-magic Library​

If you want to perform file validation operations by the pure-magic Library, you should use the pure_magic() method as follows:

file_validator.pure_magic()

mimetypes Library​

If you want to perform file validation operations by the mimetypes Library, you should use the mimetypes() method as follows:

file_validator.mimetypes()

filetype Library​

If you want to perform file validation operations by the filetype Library, you should use the filetype() method as follows:

file_validator.filetype()

All Library​

If you want to perform file validation operations by the All libraries , you should use the validate() method as follows:

file_validator.validate()

File Validation Based On The Extension​

If you want to validate the files based on their extension, you should use the validate_extension() method:

file_validator.validate_extension()

File Validation Based On The Mime​

If you want to validate the files based on their MIME and their magic numbers, you should use the validate_mime() method:

file_validator.validate_mime()

File Validation Based On The type​

If you want to validate the files based on their type such image, audio, video and etc..., you should use the validate_type() method:

file_validator.validate_type()

File Validation Based On The Size​

If you want to validate the files based on their size, you should use the validate_size() method:

file_validator.validate_size()

Size conversion table​

note

To choose the size you want the files to be validated based on, you can take help from the table below or enter your desired size in bytes:

SizeBytes
1 MB1048576 B - 10242 B - 220 B
2.5 MB2621440 B
5 MB5242880 B
10 MB10485760 B
20 MB20971520 B
50 MB52428800 B
100 MB104857600 B
250 MB262144000 B
500 MB524288000 B
1 GB1073741824 B
2 GB2147483648 B