Skip to main content

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

Metadata

Release files for fastapi-file-validator 0.0.2

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

Source distribution (sdist)

Source distribution for fastapi-file-validator 0.0.2
File Size Uploaded Provenance Actions
fastapi_file_validator-0.0.2.tar.gz 4.0 kB With provenance Details for fastapi_file_validator-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-file-validator 0.0.2
File Interpreter ABI Platform Actions
fastapi_file_validator-0.0.2-py3-none-any.whl Python 3 none any Details for fastapi_file_validator-0.0.2-py3-none-any.whl

Total release size: 8.3 kB

Release files / fastapi_file_validator-0.0.2.tar.gz

Download URL fastapi_file_validator-0.0.2.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d85ebfd26ed8b9b6ef38acc1acbee2635f52061a22ee8a411918596a94edf54f
BLAKE2b-256 checksum
How to use checksums
66400b963fabec38906494d59f3d504e340d98b8286503956d5c6f00fd69f7c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Release files / fastapi_file_validator-0.0.2-py3-none-any.whl

Download URL fastapi_file_validator-0.0.2-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60f7ecac259d483a264e7fc1b5195517c00aa5f9add10ca3281f941b17363474
BLAKE2b-256 checksum
How to use checksums
e50ca9190386296153fd838b6739e72cfc2192352982fc762a09c77d30c49f44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

Security for fastapi-file-validator 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_file_validator-0.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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