Skip to main content

This decorator validates the file extension and file size of an in memory file object.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

file_validation_decorator-0.0.8.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

file_validation_decorator-0.0.8-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file file_validation_decorator-0.0.8.tar.gz.

File metadata

  • Download URL: file_validation_decorator-0.0.8.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for file_validation_decorator-0.0.8.tar.gz
Algorithm Hash digest
SHA256 ff008a9679dca790fc3c33e4869a3f06eacb655de2e64c814f388e577e6e95dc
MD5 d381acc257d9e1b7b9e3b49e3123abb3
BLAKE2b-256 55eb63aaa2951c15be25530385da58957923029b8895bf87e760504adcf605f0

See more details on using hashes here.

File details

Details for the file file_validation_decorator-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: file_validation_decorator-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for file_validation_decorator-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 57a6ece2bd49260cf437d1ab89dc33d800c025258e7fe75147bc344ea62600a8
MD5 c568717720e3a5148d5ff8e781dd3698
BLAKE2b-256 9b32c6f1c3ee9858f507714e268857658c6fa3dab007df6394ad63cfa606d8d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page