wred-pydantic
Explicit Pydantic v2 input validation and response conversion for Wred 0.13–0.14. Python 3.11+. No dependency injection or annotation inference.
pip install .
from pydantic import BaseModel, Field
from wred import App
from wred_pydantic import body, query, path, response
class Product(BaseModel):
name: str = Field(min_length=1)
app = App()
@app.route("/products", methods=["POST"])
@body(Product)
@response(Product, status=201)
async def create(request, body):
return body, [("content-type", "application/json")], 201
body(Model, argument='body', max_bytes=1048576)validates raw JSON bytes and injects a keyword argument. Bodies over the limit return 413. Setmax_bytes=Noneto disable the limit, or supply a nonnegative byte count.query(Model, argument='query')validates Wred's query mapping. Duplicate query keys retain the last value; array query syntax is not inferred.path(Model, argument='path')validates route kwargs and injects a model while forwarding the original route kwargs. Handlers must accept both.response(Model, status=200, by_alias=True)validates and serializes successful handler returns. Serialization aliases are used in both JSON and schema; setby_alias=Falseto use field names in both.Modelmay also be a supported type such aslist[Product].statusdescribes OpenAPI metadata; the decorator does not change the returned HTTP status.
Invalid input returns 422 with detail and errors containing locations and types,
without raw inputs or custom validator messages. Model settings control coercion,
strictness, aliases, and extra fields. Invalid server responses raise Pydantic's
ValidationError; they are not converted to client errors. Tuples retain headers
and status. Non-2xx responses, 204/205 responses, and callable ASGI responses pass
through without validation. Successful payloads are validated regardless of the
metadata status value.
Use @app.route outermost, then input decorators, then response conversion.
Place exception mapping outside response conversion to handle response validation
failures. Injected argument names cannot collide with
route parameters. Schema metadata is consumed by wred-openapi without a dependency
on that package. Do not register duplicate metadata for the same input source.
Run pip install -e '.[test]' and pytest inside this repository.
The body limit counts actual streamed bytes, including requests without a Content-Length header, and stops reading when exceeded. Cached bodies are also checked, but middleware that reads the body first has already allocated that memory. Configure body limits and read timeouts at the server or reverse proxy as well. The decorator cannot prevent the ASGI server from buffering a large individual chunk before delivering it.
Constrain collection lengths, string lengths, and nesting in your models, and
paginate large responses. Validation and serialization run synchronously in the
async worker; expensive custom validators and large payloads delay other requests
on that worker. The body limit is not a CPU or response-size limit. Model instances
follow Pydantic's revalidation policy; use revalidate_instances='always' if your
application returns models that may have been mutated after construction.
Run tox for the Python 3.12 compatibility matrix (Wred 0.13/0.14 and Pydantic
2.8.0/latest 2.x). Run python benchmarks/validation.py after installing this
package to compare plain Wred with request and response validation for 1, 100,
and 10,000 items. It reports in-process ASGI latency, excluding networking and
application I/O. Measure throughput, memory, and tail latency under your expected
concurrency before sizing a production deployment.
Metadata
Release files for wred-pydantic 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wred_pydantic-0.1.0.tar.gz | 7.9 kB | Without provenance | Details for wred_pydantic-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wred_pydantic-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for wred_pydantic-0.1.0-py3-none-any.whl |
Total release size: 13.7 kB
Release files / wred_pydantic-0.1.0.tar.gz
| Download URL | wred_pydantic-0.1.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
150acab5bd3476ba34a23fdbd8109b5cc8329bd51555a5eb2da5a0937b9d247a
|
|
BLAKE2b-256 checksum How to use checksums |
a2b69774693b9d5c1720db1ea7c9cb4ac74035175ac7181bf29768580caff0a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.5
|
Release files / wred_pydantic-0.1.0-py3-none-any.whl
| Download URL | wred_pydantic-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5ca2827af56f0ee48a9fa7d4c3ee90bf59f25f6435b9254753b2b5cd9f68de1e
|
|
BLAKE2b-256 checksum How to use checksums |
a777a643651c2a8844e8cbc848405066d9ccb478264eb400a354fbce20f49853
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.5
|
Security for wred-pydantic 0.1.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.