djc-ext-pydantic
Validate components' inputs and outputs using Pydantic.
djc-ext-pydantic is a django-component extension that integrates Pydantic for input and data validation.
Example Usage
from django_components import Component, SlotInput
from djc_pydantic import ArgsBaseModel
from pydantic import BaseModel
# 1. Define the Component with Pydantic models
class MyComponent(Component):
class Args(ArgsBaseModel):
var1: str
class Kwargs(BaseModel):
name: str
age: int
class Slots(BaseModel):
header: SlotInput
footer: SlotInput
class TemplateData(BaseModel):
data1: str
data2: int
class JsData(BaseModel):
js_data1: str
js_data2: int
class CssData(BaseModel):
css_data1: str
css_data2: int
...
# 2. Render the component
MyComponent.render(
# ERROR: Expects a string
args=(123,),
kwargs={
"name": "John",
# ERROR: Expects an integer
"age": "invalid",
},
slots={
"header": "...",
# ERROR: Expects key "footer"
"foo": "invalid",
},
)
Installation
pip install djc-ext-pydantic
Then add the extension to your project:
# settings.py
COMPONENTS = {
"extensions": [
"djc_pydantic.PydanticExtension",
],
}
or by reference:
# settings.py
from djc_pydantic import PydanticExtension
COMPONENTS = {
"extensions": [
PydanticExtension,
],
}
Validating args
By default, Pydantic's BaseModel requires all fields to be passed as keyword arguments. If you want to validate positional arguments, you can use a custom subclass ArgsBaseModel:
from pydantic import BaseModel
from djc_pydantic import ArgsBaseModel
class MyTable(Component):
class Args(ArgsBaseModel):
a: int
b: str
c: float
MyTable.render(
args=[1, "hello", 3.14],
)
Release notes
Read the Release Notes to see the latest features and fixes.
Development
Tests
To run tests, use:
pytest
Metadata
Release files for djc-ext-pydantic 1.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 | |
|---|---|---|---|---|
| djc_ext_pydantic-1.1.0.tar.gz | 7.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| djc_ext_pydantic-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / djc_ext_pydantic-1.1.0.tar.gz
| Download URL | djc_ext_pydantic-1.1.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c583dd6bfcc687a18a8510391787c7bb0b3b7fe10886bbe48fcdff240b21d788
|
|
BLAKE2b-256 checksum How to use checksums |
ba72c64a5a0e849d831bf210acaf49120035502e57341387cbccafc3b0469562
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / djc_ext_pydantic-1.1.0-py3-none-any.whl
| Download URL | djc_ext_pydantic-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5d94279d058bdae6eba45fad7229372cc514c3e49db1d3ef7b71f5bf44a9111f
|
|
BLAKE2b-256 checksum How to use checksums |
ba6820d628d53fb78d847261910c2ee61ab888a7687d697d5f2cb3a0c664767f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for djc-ext-pydantic 1.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.