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.
🚀 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d85ebfd26ed8b9b6ef38acc1acbee2635f52061a22ee8a411918596a94edf54f
|
|
| MD5 |
b82776a3b0ba90bf55273426cebcbba8
|
|
| BLAKE2b-256 |
66400b963fabec38906494d59f3d504e340d98b8286503956d5c6f00fd69f7c1
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_file_validator-0.0.2.tar.gz -
Subject digest:
d85ebfd26ed8b9b6ef38acc1acbee2635f52061a22ee8a411918596a94edf54f - Sigstore transparency entry: 1558929984
- Sigstore integration time:
-
Permalink:
Seungkyu-Han/fastapi-file-validator@8b114c212942c32ec821481c1d780fbb9ba4e4aa -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Seungkyu-Han
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@8b114c212942c32ec821481c1d780fbb9ba4e4aa -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_file_validator-0.0.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_file_validator-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60f7ecac259d483a264e7fc1b5195517c00aa5f9add10ca3281f941b17363474
|
|
| MD5 |
cf8a2e009e9fdf29935b4533bbec0b07
|
|
| BLAKE2b-256 |
e50ca9190386296153fd838b6739e72cfc2192352982fc762a09c77d30c49f44
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_file_validator-0.0.2-py3-none-any.whl -
Subject digest:
60f7ecac259d483a264e7fc1b5195517c00aa5f9add10ca3281f941b17363474 - Sigstore transparency entry: 1558930145
- Sigstore integration time:
-
Permalink:
Seungkyu-Han/fastapi-file-validator@8b114c212942c32ec821481c1d780fbb9ba4e4aa -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Seungkyu-Han
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@8b114c212942c32ec821481c1d780fbb9ba4e4aa -
Trigger Event:
push
-
Statement type: