Skip to main content

A lightweight, flexible file extension validator and MIME type checker for fastapi

Project description

FastAPI File Validators

A simple, lightweight, and flexible decorator-based file validation library for FastAPI. Easily validate uploaded file extensions and file sizes before hitting your endpoint logic.

PyPI version codecov PyPI downloads license


🚀 Installation

Install via pip:

pip install fastapi-file-validators

Or install locally for development:

pip install -e .

📦 Usage

1. Validate File Extension

Ensure that the uploaded file matches specific allowed extensions. If the extension is invalid or missing, it automatically raises an HTTP 415 Unsupported Media Type or HTTP 400 Bad Request.

from fastapi import FastAPI, UploadFile, File
from file_validator import file_extension_validator

app = FastAPI()

@app.post("/upload/image")
@file_extension_validator(file_arg_name="file", file_extensions={"jpg", "jpeg", "png"})
async def upload_image(file: UploadFile = File(...)):
    return {"filename": file.filename, "message": "Extension validated successfully!"}

2. Validate File Size

Restrict the maximum size of an uploaded file in Megabytes (MB). If the file exceeds the limit, it automatically raises an HTTP 413 Content Too Large.

from fastapi import FastAPI, UploadFile, File
from file_validator import file_size_validator

app = FastAPI()

@app.post("/upload/large-file")
@file_size_validator(file_arg_name="file", max_size_mb=10)  # Limit to 10MB
async def upload_large_file(file: UploadFile = File(...)):
    return {"size": file.size, "message": "Size validated successfully!"}

⚙️ Configuration

Exception Handling

The decorators raise standard FastAPI HTTPExceptions. You can expect the following responses when validation fails:

Validator Scenario HTTP Status Detail Message Example
file_extension_validator Filename is empty 400 Bad Request "filename is empty"
file_extension_validator Invalid extension 415 Unsupported Media Type "exe type is unavailable"
file_size_validator File exceeds max size 413 Content Too Large "file size is bigger than 5MB"

📄 License

MIT License

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

fastapi_file_validator-0.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_file_validator-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_file_validator-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi_file_validator-0.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastapi_file_validator-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d85ebfd26ed8b9b6ef38acc1acbee2635f52061a22ee8a411918596a94edf54f
MD5 b82776a3b0ba90bf55273426cebcbba8
BLAKE2b-256 66400b963fabec38906494d59f3d504e340d98b8286503956d5c6f00fd69f7c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_file_validator-0.0.2.tar.gz:

Publisher: workflow.yml on Seungkyu-Han/fastapi-file-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_file_validator-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_file_validator-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 60f7ecac259d483a264e7fc1b5195517c00aa5f9add10ca3281f941b17363474
MD5 cf8a2e009e9fdf29935b4533bbec0b07
BLAKE2b-256 e50ca9190386296153fd838b6739e72cfc2192352982fc762a09c77d30c49f44

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_file_validator-0.0.2-py3-none-any.whl:

Publisher: workflow.yml on Seungkyu-Han/fastapi-file-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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