Skip to main content

A Django middleware to clean and normalize incoming request data (GET, POST, JSON).

Project description

Django Request Normalizer

A configurable Django middleware that automatically cleans and normalizes incoming request data before it reaches your views.

Features

  • Trims Whitespace: Strips leading/trailing whitespace from all string values.
  • Normalizes Booleans: Converts string values like "true" and "false" to actual booleans.
  • Normalizes Nulls: Converts string "null", empty strings "" for ID fields, and integer 0 for ID fields to None.
  • Cleans Email: Removes spaces and lowercases email fields.
  • Handles All Data: Works seamlessly with GET parameters, application/json, multipart/form-data, and x-www-form-urlencoded data.
  • Configurable: You can enable/disable the middleware or specify which URL prefixes it should act on.

Installation

pip install django-request-normalizer

Usage

Add the middleware to your MIDDLEWARE list in settings.py. It should be placed before any middleware that accesses request data, such as DRF's authentication classes or your own views.

# settings.py
MIDDLEWARE = [
    # ...
    'request_normalizer.middleware.RequestNormalizerMiddleware',
    # ...
]

⚙️ Configuration

To enable normalization, you must specify a list of URL prefixes for which it will be applied. This is done in your settings.py.

# settings.py

# A list of URL prefixes for which normalization will be active.
API_PREFIXES = ['/api/v1/', '/api/v2/']

🧮 Normalization Rules

Input Value Key Output Value Explanation
"null" any None The string "null" becomes None
"true" any True The string "true" becomes True
"false" any False The string "false" becomes False
0 (integer) user_id None 0 for _id or id fields becomes None
"0" (string) id None The string "0" for _id or id fields becomes None
"" (empty string) category_id None An empty string for _id or id fields becomes None
" test%20string " name "test string" Whitespace is trimmed and characters are decoded
" User@Example.com " email "user@example.com" Whitespace is removed and the string is lowercased

📜 License

This project is licensed under the 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

bear_django_request_normalizer-0.3.1.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file bear_django_request_normalizer-0.3.1.tar.gz.

File metadata

File hashes

Hashes for bear_django_request_normalizer-0.3.1.tar.gz
Algorithm Hash digest
SHA256 82be4ac8c3182c09b58e850a4655971954a16193e6fd879291ceb7426c7fb1dc
MD5 a17d9442b5e9f8e1a408000c7bcbef1f
BLAKE2b-256 0d8b0dc33c850655064077ddef816623b634c3bf1e121618d15dc25b7f2943c7

See more details on using hashes here.

File details

Details for the file bear_django_request_normalizer-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bear_django_request_normalizer-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef300b82cadb1f14d3674fd6d41d5637431f69d1c5a05c5db94bab54b122ae07
MD5 f33fa0e36d1cbba2042de904b64fa558
BLAKE2b-256 0d33d35fe47ad72babec75726add59b487631b732fac5a3c13a6fba5044f02d3

See more details on using hashes here.

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