Metadata-Version: 2.4
Name: bear-django-request-normalizer
Version: 0.3.1
Summary: A Django middleware to clean and normalize incoming request data (GET, POST, JSON).
Author-email: Bohdan Riabunets <bearablyk@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/bearablyk/django-request-normalizer
Project-URL: Issues, https://github.com/bearablyk/django-request-normalizer/issues
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Framework :: Django
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: Django>=3.2
Requires-Dist: bear-django-request-utils>=0.1.0

# 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

```bash
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.

```python
# 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`.

```python
# 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.
