Skip to main content

File Validation Decorator

A decorator for validating file types and sizes. If the validation fails, an Exception will be raised and the code will stop before reaching the function you are decorating. A successful validation will execute the function being decorated - business as usual.

Alt_Text


Example Of A Successful File Validation

Successful file validations allow the function that is being decorated to execute. The first parameter defined in your function needs to be the file object.

from file_validation_decorator.file_validation import file_validation

@file_validation(accepted_file_extensions=['txt'], accepted_mime_types=['text/plain'])
def hello(file):
    print('Hello World!')

with open('test.txt', 'r') as file:
    hello(file)

Example Of A File That Is Too Large In Size

The decorator is provided a file size of .000001 and will raise a FileSizeExceeded error.

from file_validation_decorator.file_validation import file_validation

@file_validation(accepted_file_extensions=['txt'], max_file_size=.000001)
def hello(file):
    print('Hello World!')

with open('test.txt', 'r') as file:
    hello(file)

Example Of A File Extension That Is Not Allowed

The example file being uploaded here is a .txt but a text file is not provided in the accepted file extensions list. This will raise a FileTypeNotAllowed error.

from file_validation_decorator.file_validation import file_validation

@file_validation(accepted_file_extensions=['jpeg'])
def hello(file):
    print('Hello World!')

with open('test.txt', 'r') as file:
    hello(file)

Exceptions

  • FileExtensionNotAllowed - Raised when the file's extension is not found in the list of accepted_file_extensions

  • FileTypeNotAllowed - Raised when the file's type is not found in the list of accepted_file_types

  • FileMimeTypeNotAllowed - Raised when the file's mime type is not found in the list of accepted_mime_types

  • FileSizeExceeded - Raised when the file's size is larger than the max_file_size

Metadata

Release files for file-validation-decorator 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for file-validation-decorator 0.0.6
File Size Uploaded
file_validation_decorator-0.0.6.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for file-validation-decorator 0.0.6
File Interpreter ABI Platform
file_validation_decorator-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 kB

Release files / file_validation_decorator-0.0.6.tar.gz

Download URL file_validation_decorator-0.0.6.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
57c5c293909d09d4e4c339bae5c1dd1aab40b30d00c280e829cd96003d2b6300
BLAKE2b-256 checksum
How to use checksums
e9a89e0945eeeb9eb2abc500dc1b6aec09d35d137854059ddf1c87fb9a3fb2a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

Release files / file_validation_decorator-0.0.6-py3-none-any.whl

Download URL file_validation_decorator-0.0.6-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3054bc13cd324da690a36e472a0e910e8093961f420e4272c99bc10f6b465314
BLAKE2b-256 checksum
How to use checksums
85731a93c5fda8ade7f0022602714e0cdf1d6c5142b1dfb523e968a2b7498a4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

Release history Release notifications | RSS feed

0.0.8

2 release files

0.0.7

2 release files

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page