api-response-cleaner 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- api_response_cleaner-0.1.0/LICENSE +21 -0
- api_response_cleaner-0.1.0/PKG-INFO +246 -0
- api_response_cleaner-0.1.0/README.md +223 -0
- api_response_cleaner-0.1.0/pyproject.toml +44 -0
- api_response_cleaner-0.1.0/setup.cfg +4 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/__init__.py +8 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/cleaner.py +199 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/errors.py +39 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/normalizer.py +80 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/py.typed +1 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/schema.py +149 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner/validator.py +32 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner.egg-info/PKG-INFO +246 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner.egg-info/SOURCES.txt +19 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner.egg-info/dependency_links.txt +1 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner.egg-info/requires.txt +5 -0
- api_response_cleaner-0.1.0/src/api_response_cleaner.egg-info/top_level.txt +1 -0
- api_response_cleaner-0.1.0/tests/test_cleaner.py +240 -0
- api_response_cleaner-0.1.0/tests/test_normalizer.py +49 -0
- api_response_cleaner-0.1.0/tests/test_schema.py +61 -0
- api_response_cleaner-0.1.0/tests/test_validator.py +37 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Narala Vamsi Krishna
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: api-response-cleaner
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Turns inconsistent API responses into validated Python data using a developer-defined schema.
|
|
5
|
+
Author: Narala Vamsi Krishna
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/VamsiKrishnaNarala/api-response-cleaner
|
|
8
|
+
Project-URL: Repository, https://github.com/VamsiKrishnaNarala/api-response-cleaner
|
|
9
|
+
Project-URL: Issues, https://github.com/VamsiKrishnaNarala/api-response-cleaner/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
20
|
+
Requires-Dist: build; extra == "dev"
|
|
21
|
+
Requires-Dist: twine; extra == "dev"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# api-response-cleaner
|
|
25
|
+
|
|
26
|
+
Turns inconsistent API responses into validated Python data using a developer-defined schema. Zero dependencies.
|
|
27
|
+
|
|
28
|
+
## Why Use This Library? (Use Cases)
|
|
29
|
+
|
|
30
|
+
- **[Standardize third-party APIs](#example-a--standardizing-apis)**: Normalize inconsistent field types and response formats into a predictable output schema.
|
|
31
|
+
- **[Handle legacy and inconsistent data](#example-b--legacy-field-names-and-defaults)**: Support field aliases, optional fields, defaults, and explicit missing/null behavior.
|
|
32
|
+
- **Lightweight alternative to Pydantic**: A smaller, standard-library-runtime-dependency-free option for dictionary-based cleaning and validation when you don't need arbitrary object models.
|
|
33
|
+
- **[Validate nested JSON safely](#example-c--nested-data)**: Perform nested schema validation and error reporting without manual chains of `.get()` calls.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install api-response-cleaner
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from api_response_cleaner import clean
|
|
45
|
+
|
|
46
|
+
result = clean(
|
|
47
|
+
{"full_name": " Ravi ", "age": "25"},
|
|
48
|
+
schema={"name": {"type": str, "required": True},
|
|
49
|
+
"age": {"type": int, "required": True}},
|
|
50
|
+
aliases={"name": ["full_name"]},
|
|
51
|
+
)
|
|
52
|
+
print(result.data) # {"name": "Ravi", "age": 25}
|
|
53
|
+
print(result.errors) # []
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Schema Reference
|
|
57
|
+
|
|
58
|
+
Define fields as dictionaries. Supported keys:
|
|
59
|
+
- `type`: `str`, `int`, `float`, `bool`, `list`, `dict`, `object`
|
|
60
|
+
- `required`: boolean, default `False`
|
|
61
|
+
- `default`: fallback value if key absent
|
|
62
|
+
- `nullable`: boolean, default `False`
|
|
63
|
+
- `aliases`: list of alternative keys
|
|
64
|
+
- `strip`: boolean, default `True` (for strings)
|
|
65
|
+
- `choices`: iterable of allowed values
|
|
66
|
+
- `min`, `max`: bounds for `int` and `float`
|
|
67
|
+
- `min_length`, `max_length`: length bounds for `str` and `list`
|
|
68
|
+
- `schema`: dictionary of sub-fields (implies `type` dict)
|
|
69
|
+
- `items`: type or spec for list elements
|
|
70
|
+
|
|
71
|
+
Shorthand: `{"age": int}` is equivalent to `{"age": {"type": int}}`.
|
|
72
|
+
|
|
73
|
+
## Missing and Null Decision Table
|
|
74
|
+
|
|
75
|
+
| Input state | optional | required | default=7 | nullable=True |
|
|
76
|
+
|-----------------------------|----------------|-----------------------|-----------|-----------------------|
|
|
77
|
+
| key absent | key omitted | missing_required | 7 | key omitted |
|
|
78
|
+
| key present, value null | key omitted | null_not_allowed | 7 | None (null preserved) |
|
|
79
|
+
| key present, value "25" | 25 | 25 | 25 | 25 |
|
|
80
|
+
| key present, value "" | conversion_failed in all four columns |
|
|
81
|
+
|
|
82
|
+
Note: `nullable=True` + `required=True` with key absent gives `missing_required`.
|
|
83
|
+
|
|
84
|
+
## Alias and Nested Rules
|
|
85
|
+
|
|
86
|
+
- Aliases are tried in order if canonical name is missing.
|
|
87
|
+
- If canonical name and alias are both present and non-null, canonical wins.
|
|
88
|
+
- Alias keys are consumed; they are not reported as extra or unexpected.
|
|
89
|
+
- Errors are reported on canonical paths.
|
|
90
|
+
- Nested schema values must be objects (dict).
|
|
91
|
+
- Inner errors are collected and reported with full dotted paths (e.g. `address.city`).
|
|
92
|
+
- If an inner error occurs, the whole nested key is omitted from result data.
|
|
93
|
+
|
|
94
|
+
## Structured Defaults Warning
|
|
95
|
+
|
|
96
|
+
A structured default (`list` or `dict`) is **trusted**. It is returned as-is (deep copied) and its contents are NOT normalized, constraint-checked, or mapped for aliases. Structured defaults must already be in their final valid form.
|
|
97
|
+
|
|
98
|
+
## Constraints
|
|
99
|
+
|
|
100
|
+
| Constraint | Supported Types |
|
|
101
|
+
|---------------------------|-------------------------|
|
|
102
|
+
| `min`, `max` | `int`, `float` |
|
|
103
|
+
| `min_length`, `max_length`| `str`, `list` |
|
|
104
|
+
| `choices` | `str`, `int`, `float`, `bool`, `object` |
|
|
105
|
+
|
|
106
|
+
## Conversion Rules
|
|
107
|
+
|
|
108
|
+
- `str`: Accepts strings, ints, finite floats. Strips whitespace by default.
|
|
109
|
+
- `int`: Accepts ints, whole floats (e.g., `25.0`), and numeric strings.
|
|
110
|
+
- `float`: Accepts finite floats, ints, and numeric strings.
|
|
111
|
+
- `bool`: Accepts booleans, ints `0` and `1`, strings `true`, `false`, `yes`, `no`, `on`, `off`, `1`, `0`.
|
|
112
|
+
- `list`: Accepts lists and tuples.
|
|
113
|
+
- `dict`: Accepts dicts.
|
|
114
|
+
- `object`: Accepts anything.
|
|
115
|
+
|
|
116
|
+
## Error Codes
|
|
117
|
+
|
|
118
|
+
- `missing_required`
|
|
119
|
+
- `null_not_allowed`
|
|
120
|
+
- `conversion_failed`
|
|
121
|
+
- `not_in_choices`
|
|
122
|
+
- `too_small`
|
|
123
|
+
- `too_large`
|
|
124
|
+
- `too_short`
|
|
125
|
+
- `too_long`
|
|
126
|
+
- `unexpected_field`
|
|
127
|
+
- `invalid_input`
|
|
128
|
+
|
|
129
|
+
## Examples
|
|
130
|
+
|
|
131
|
+
### Example A — Standardizing APIs
|
|
132
|
+
|
|
133
|
+
Normalize inconsistent payloads into a predictable shape.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from api_response_cleaner import clean
|
|
137
|
+
|
|
138
|
+
schema = {
|
|
139
|
+
"name": {"type": str, "aliases": ["full_name"], "required": True},
|
|
140
|
+
"age": {"type": int, "nullable": True}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
api_a = {"name": "Ravi", "age": 25}
|
|
144
|
+
api_b = {"name": "Ravi", "age": "25"}
|
|
145
|
+
api_c = {"full_name": "Ravi", "age": None}
|
|
146
|
+
|
|
147
|
+
for api, payload in zip(["A", "B", "C"], [api_a, api_b, api_c]):
|
|
148
|
+
res = clean(payload, schema)
|
|
149
|
+
print(f"API {api}: data={res.data}, errors={res.errors}")
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
*Note that API C's `age=None` is handled exactly according to the schema (preserved because `nullable=True`). It is never silently converted to a zero value or a default unless explicitly configured.*
|
|
153
|
+
|
|
154
|
+
### Example B — Legacy field names and defaults
|
|
155
|
+
|
|
156
|
+
Define canonical fields while supporting old aliases and providing sensible defaults.
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from api_response_cleaner import clean
|
|
160
|
+
|
|
161
|
+
schema = {
|
|
162
|
+
"theme": {"type": str, "aliases": ["ui_theme", "color_mode"], "default": "light"}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
# 1. Canonical key works normally
|
|
166
|
+
print(clean({"theme": "dark"}, schema).data)
|
|
167
|
+
# -> {"theme": "dark"}
|
|
168
|
+
|
|
169
|
+
# 2. Alias is seamlessly mapped to the canonical key
|
|
170
|
+
print(clean({"ui_theme": "dark"}, schema).data)
|
|
171
|
+
# -> {"theme": "dark"}
|
|
172
|
+
|
|
173
|
+
# 3. Missing key falls back to the default
|
|
174
|
+
print(clean({}, schema).data)
|
|
175
|
+
# -> {"theme": "light"}
|
|
176
|
+
|
|
177
|
+
# 4. Explicit null ignores the default unless the field is nullable
|
|
178
|
+
res = clean({"theme": None}, schema)
|
|
179
|
+
print(res.errors[0].code)
|
|
180
|
+
# -> ErrorCode.null_not_allowed
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Example C — Nested data
|
|
184
|
+
|
|
185
|
+
Safely parse nested user/address data without manual `.get()` chains. Invalid nested objects are omitted entirely instead of returned partially cleaned.
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from api_response_cleaner import Cleaner
|
|
189
|
+
|
|
190
|
+
cleaner = Cleaner({
|
|
191
|
+
"address": {
|
|
192
|
+
"schema": {
|
|
193
|
+
"city": {"type": str, "required": True},
|
|
194
|
+
"zip": int
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
# Valid payload
|
|
200
|
+
res_valid = cleaner.clean({"address": {"city": "NY", "zip": "10001"}})
|
|
201
|
+
print(res_valid.data)
|
|
202
|
+
# -> {"address": {"city": "NY", "zip": 10001}}
|
|
203
|
+
|
|
204
|
+
# Invalid payload
|
|
205
|
+
res_invalid = cleaner.clean({"address": {"zip": "abc"}})
|
|
206
|
+
print("Errors:", [(e.field, e.code.value) for e in res_invalid.errors])
|
|
207
|
+
# -> Errors: [('address.city', 'missing_required'), ('address.zip', 'conversion_failed')]
|
|
208
|
+
print("Data:", res_invalid.data)
|
|
209
|
+
# -> Data: {}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Example D — List validation
|
|
213
|
+
|
|
214
|
+
Validate lists of typed items. Bad elements are reported by their original index, and the whole list is omitted from data if any element is invalid.
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from api_response_cleaner import clean
|
|
218
|
+
|
|
219
|
+
schema = {
|
|
220
|
+
"scores": {"type": list, "items": int}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
res = clean({"scores": ["1", "x", None, "3"]}, schema)
|
|
224
|
+
print("Errors:", [(e.field, e.code.value) for e in res.errors])
|
|
225
|
+
# -> Errors: [('scores[1]', 'conversion_failed'), ('scores[2]', 'null_not_allowed')]
|
|
226
|
+
print("Data:", res.data)
|
|
227
|
+
# -> Data: {}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Development Commands
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install -e .[dev]
|
|
234
|
+
pytest
|
|
235
|
+
python -m build
|
|
236
|
+
twine check dist/*
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Releasing
|
|
240
|
+
|
|
241
|
+
- [ ] pytest passes on Python 3.11 and the newest installed Python
|
|
242
|
+
- [ ] python -m build produces one sdist and one wheel
|
|
243
|
+
- [ ] twine check dist/* passes
|
|
244
|
+
- [ ] the wheel installs into a clean virtual environment and the quick-start example runs
|
|
245
|
+
- [ ] [project.urls], author and license holder are filled in
|
|
246
|
+
- [ ] the package name is confirmed available on PyPI, then upload to TestPyPI first
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# api-response-cleaner
|
|
2
|
+
|
|
3
|
+
Turns inconsistent API responses into validated Python data using a developer-defined schema. Zero dependencies.
|
|
4
|
+
|
|
5
|
+
## Why Use This Library? (Use Cases)
|
|
6
|
+
|
|
7
|
+
- **[Standardize third-party APIs](#example-a--standardizing-apis)**: Normalize inconsistent field types and response formats into a predictable output schema.
|
|
8
|
+
- **[Handle legacy and inconsistent data](#example-b--legacy-field-names-and-defaults)**: Support field aliases, optional fields, defaults, and explicit missing/null behavior.
|
|
9
|
+
- **Lightweight alternative to Pydantic**: A smaller, standard-library-runtime-dependency-free option for dictionary-based cleaning and validation when you don't need arbitrary object models.
|
|
10
|
+
- **[Validate nested JSON safely](#example-c--nested-data)**: Perform nested schema validation and error reporting without manual chains of `.get()` calls.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install api-response-cleaner
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from api_response_cleaner import clean
|
|
22
|
+
|
|
23
|
+
result = clean(
|
|
24
|
+
{"full_name": " Ravi ", "age": "25"},
|
|
25
|
+
schema={"name": {"type": str, "required": True},
|
|
26
|
+
"age": {"type": int, "required": True}},
|
|
27
|
+
aliases={"name": ["full_name"]},
|
|
28
|
+
)
|
|
29
|
+
print(result.data) # {"name": "Ravi", "age": 25}
|
|
30
|
+
print(result.errors) # []
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Schema Reference
|
|
34
|
+
|
|
35
|
+
Define fields as dictionaries. Supported keys:
|
|
36
|
+
- `type`: `str`, `int`, `float`, `bool`, `list`, `dict`, `object`
|
|
37
|
+
- `required`: boolean, default `False`
|
|
38
|
+
- `default`: fallback value if key absent
|
|
39
|
+
- `nullable`: boolean, default `False`
|
|
40
|
+
- `aliases`: list of alternative keys
|
|
41
|
+
- `strip`: boolean, default `True` (for strings)
|
|
42
|
+
- `choices`: iterable of allowed values
|
|
43
|
+
- `min`, `max`: bounds for `int` and `float`
|
|
44
|
+
- `min_length`, `max_length`: length bounds for `str` and `list`
|
|
45
|
+
- `schema`: dictionary of sub-fields (implies `type` dict)
|
|
46
|
+
- `items`: type or spec for list elements
|
|
47
|
+
|
|
48
|
+
Shorthand: `{"age": int}` is equivalent to `{"age": {"type": int}}`.
|
|
49
|
+
|
|
50
|
+
## Missing and Null Decision Table
|
|
51
|
+
|
|
52
|
+
| Input state | optional | required | default=7 | nullable=True |
|
|
53
|
+
|-----------------------------|----------------|-----------------------|-----------|-----------------------|
|
|
54
|
+
| key absent | key omitted | missing_required | 7 | key omitted |
|
|
55
|
+
| key present, value null | key omitted | null_not_allowed | 7 | None (null preserved) |
|
|
56
|
+
| key present, value "25" | 25 | 25 | 25 | 25 |
|
|
57
|
+
| key present, value "" | conversion_failed in all four columns |
|
|
58
|
+
|
|
59
|
+
Note: `nullable=True` + `required=True` with key absent gives `missing_required`.
|
|
60
|
+
|
|
61
|
+
## Alias and Nested Rules
|
|
62
|
+
|
|
63
|
+
- Aliases are tried in order if canonical name is missing.
|
|
64
|
+
- If canonical name and alias are both present and non-null, canonical wins.
|
|
65
|
+
- Alias keys are consumed; they are not reported as extra or unexpected.
|
|
66
|
+
- Errors are reported on canonical paths.
|
|
67
|
+
- Nested schema values must be objects (dict).
|
|
68
|
+
- Inner errors are collected and reported with full dotted paths (e.g. `address.city`).
|
|
69
|
+
- If an inner error occurs, the whole nested key is omitted from result data.
|
|
70
|
+
|
|
71
|
+
## Structured Defaults Warning
|
|
72
|
+
|
|
73
|
+
A structured default (`list` or `dict`) is **trusted**. It is returned as-is (deep copied) and its contents are NOT normalized, constraint-checked, or mapped for aliases. Structured defaults must already be in their final valid form.
|
|
74
|
+
|
|
75
|
+
## Constraints
|
|
76
|
+
|
|
77
|
+
| Constraint | Supported Types |
|
|
78
|
+
|---------------------------|-------------------------|
|
|
79
|
+
| `min`, `max` | `int`, `float` |
|
|
80
|
+
| `min_length`, `max_length`| `str`, `list` |
|
|
81
|
+
| `choices` | `str`, `int`, `float`, `bool`, `object` |
|
|
82
|
+
|
|
83
|
+
## Conversion Rules
|
|
84
|
+
|
|
85
|
+
- `str`: Accepts strings, ints, finite floats. Strips whitespace by default.
|
|
86
|
+
- `int`: Accepts ints, whole floats (e.g., `25.0`), and numeric strings.
|
|
87
|
+
- `float`: Accepts finite floats, ints, and numeric strings.
|
|
88
|
+
- `bool`: Accepts booleans, ints `0` and `1`, strings `true`, `false`, `yes`, `no`, `on`, `off`, `1`, `0`.
|
|
89
|
+
- `list`: Accepts lists and tuples.
|
|
90
|
+
- `dict`: Accepts dicts.
|
|
91
|
+
- `object`: Accepts anything.
|
|
92
|
+
|
|
93
|
+
## Error Codes
|
|
94
|
+
|
|
95
|
+
- `missing_required`
|
|
96
|
+
- `null_not_allowed`
|
|
97
|
+
- `conversion_failed`
|
|
98
|
+
- `not_in_choices`
|
|
99
|
+
- `too_small`
|
|
100
|
+
- `too_large`
|
|
101
|
+
- `too_short`
|
|
102
|
+
- `too_long`
|
|
103
|
+
- `unexpected_field`
|
|
104
|
+
- `invalid_input`
|
|
105
|
+
|
|
106
|
+
## Examples
|
|
107
|
+
|
|
108
|
+
### Example A — Standardizing APIs
|
|
109
|
+
|
|
110
|
+
Normalize inconsistent payloads into a predictable shape.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from api_response_cleaner import clean
|
|
114
|
+
|
|
115
|
+
schema = {
|
|
116
|
+
"name": {"type": str, "aliases": ["full_name"], "required": True},
|
|
117
|
+
"age": {"type": int, "nullable": True}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
api_a = {"name": "Ravi", "age": 25}
|
|
121
|
+
api_b = {"name": "Ravi", "age": "25"}
|
|
122
|
+
api_c = {"full_name": "Ravi", "age": None}
|
|
123
|
+
|
|
124
|
+
for api, payload in zip(["A", "B", "C"], [api_a, api_b, api_c]):
|
|
125
|
+
res = clean(payload, schema)
|
|
126
|
+
print(f"API {api}: data={res.data}, errors={res.errors}")
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
*Note that API C's `age=None` is handled exactly according to the schema (preserved because `nullable=True`). It is never silently converted to a zero value or a default unless explicitly configured.*
|
|
130
|
+
|
|
131
|
+
### Example B — Legacy field names and defaults
|
|
132
|
+
|
|
133
|
+
Define canonical fields while supporting old aliases and providing sensible defaults.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from api_response_cleaner import clean
|
|
137
|
+
|
|
138
|
+
schema = {
|
|
139
|
+
"theme": {"type": str, "aliases": ["ui_theme", "color_mode"], "default": "light"}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
# 1. Canonical key works normally
|
|
143
|
+
print(clean({"theme": "dark"}, schema).data)
|
|
144
|
+
# -> {"theme": "dark"}
|
|
145
|
+
|
|
146
|
+
# 2. Alias is seamlessly mapped to the canonical key
|
|
147
|
+
print(clean({"ui_theme": "dark"}, schema).data)
|
|
148
|
+
# -> {"theme": "dark"}
|
|
149
|
+
|
|
150
|
+
# 3. Missing key falls back to the default
|
|
151
|
+
print(clean({}, schema).data)
|
|
152
|
+
# -> {"theme": "light"}
|
|
153
|
+
|
|
154
|
+
# 4. Explicit null ignores the default unless the field is nullable
|
|
155
|
+
res = clean({"theme": None}, schema)
|
|
156
|
+
print(res.errors[0].code)
|
|
157
|
+
# -> ErrorCode.null_not_allowed
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Example C — Nested data
|
|
161
|
+
|
|
162
|
+
Safely parse nested user/address data without manual `.get()` chains. Invalid nested objects are omitted entirely instead of returned partially cleaned.
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from api_response_cleaner import Cleaner
|
|
166
|
+
|
|
167
|
+
cleaner = Cleaner({
|
|
168
|
+
"address": {
|
|
169
|
+
"schema": {
|
|
170
|
+
"city": {"type": str, "required": True},
|
|
171
|
+
"zip": int
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
# Valid payload
|
|
177
|
+
res_valid = cleaner.clean({"address": {"city": "NY", "zip": "10001"}})
|
|
178
|
+
print(res_valid.data)
|
|
179
|
+
# -> {"address": {"city": "NY", "zip": 10001}}
|
|
180
|
+
|
|
181
|
+
# Invalid payload
|
|
182
|
+
res_invalid = cleaner.clean({"address": {"zip": "abc"}})
|
|
183
|
+
print("Errors:", [(e.field, e.code.value) for e in res_invalid.errors])
|
|
184
|
+
# -> Errors: [('address.city', 'missing_required'), ('address.zip', 'conversion_failed')]
|
|
185
|
+
print("Data:", res_invalid.data)
|
|
186
|
+
# -> Data: {}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Example D — List validation
|
|
190
|
+
|
|
191
|
+
Validate lists of typed items. Bad elements are reported by their original index, and the whole list is omitted from data if any element is invalid.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
from api_response_cleaner import clean
|
|
195
|
+
|
|
196
|
+
schema = {
|
|
197
|
+
"scores": {"type": list, "items": int}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
res = clean({"scores": ["1", "x", None, "3"]}, schema)
|
|
201
|
+
print("Errors:", [(e.field, e.code.value) for e in res.errors])
|
|
202
|
+
# -> Errors: [('scores[1]', 'conversion_failed'), ('scores[2]', 'null_not_allowed')]
|
|
203
|
+
print("Data:", res.data)
|
|
204
|
+
# -> Data: {}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Development Commands
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
pip install -e .[dev]
|
|
211
|
+
pytest
|
|
212
|
+
python -m build
|
|
213
|
+
twine check dist/*
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Releasing
|
|
217
|
+
|
|
218
|
+
- [ ] pytest passes on Python 3.11 and the newest installed Python
|
|
219
|
+
- [ ] python -m build produces one sdist and one wheel
|
|
220
|
+
- [ ] twine check dist/* passes
|
|
221
|
+
- [ ] the wheel installs into a clean virtual environment and the quick-start example runs
|
|
222
|
+
- [ ] [project.urls], author and license holder are filled in
|
|
223
|
+
- [ ] the package name is confirmed available on PyPI, then upload to TestPyPI first
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "api-response-cleaner"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Turns inconsistent API responses into validated Python data using a developer-defined schema."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Narala Vamsi Krishna" }
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/VamsiKrishnaNarala/api-response-cleaner"
|
|
24
|
+
Repository = "https://github.com/VamsiKrishnaNarala/api-response-cleaner"
|
|
25
|
+
Issues = "https://github.com/VamsiKrishnaNarala/api-response-cleaner/issues"
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
dev = [
|
|
28
|
+
"pytest>=8",
|
|
29
|
+
"build",
|
|
30
|
+
"twine"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.dynamic]
|
|
34
|
+
version = {attr = "api_response_cleaner.__version__"}
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
"*" = ["py.typed"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from .cleaner import Cleaner, CleanResult
|
|
2
|
+
from .schema import Schema, Field
|
|
3
|
+
from .errors import ErrorCode, FieldError, SchemaError, CleaningError
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
def clean(data: dict, schema: Schema, aliases: dict = None, extra: str = "ignore") -> CleanResult:
|
|
8
|
+
return Cleaner(schema, aliases, extra).clean(data)
|