Input validation with Pydantic for Django Components
Project description
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. It uses the types defined on the component's class to validate both inputs and outputs of Django components.
Validated Inputs and Outputs
-
Inputs:
-
Outputs:
Example Usage
from pydantic import BaseModel
from typing import Tuple, TypedDict
# 1. Define the types
MyCompArgs = Tuple[str, ...]
class MyCompKwargs(TypedDict):
name: str
age: int
class MyCompSlots(TypedDict):
header: SlotContent
footer: SlotContent
class MyCompData(BaseModel):
data1: str
data2: int
class MyCompJsData(BaseModel):
js_data1: str
js_data2: int
class MyCompCssData(BaseModel):
css_data1: str
css_data2: int
# 2. Define the component with those types
class MyComponent(Component[
MyCompArgs,
MyCompKwargs,
MyCompSlots,
MyCompData,
MyCompJsData,
MyCompCssData,
]):
...
# 3. 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",
},
)
If you don't want to validate some parts, set them to Any.
class MyComponent(Component[
MyCompArgs,
MyCompKwargs,
MyCompSlots,
Any,
Any,
Any,
]):
...
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,
],
}
Release notes
Read the Release Notes to see the latest features and fixes.
Development
Tests
To run tests, use:
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djc_ext_pydantic-1.0.0.tar.gz.
File metadata
- Download URL: djc_ext_pydantic-1.0.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49649525b6cb19d60903b2076bdc7b4feeb74b6a665030be39f1fb5d839f2998
|
|
| MD5 |
fabf85b05be10b5ecf447723ce81bf08
|
|
| BLAKE2b-256 |
c5be7af7b407922524bcc5301b4d611a8aa8f702b7460b0593fc5f832feb2600
|
File details
Details for the file djc_ext_pydantic-1.0.0-py3-none-any.whl.
File metadata
- Download URL: djc_ext_pydantic-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9042f3f644f4dd721d050d21e7691ea5d67c6c11e889dcfbe2c68f356916c039
|
|
| MD5 |
5f8c729726e6dc5e10a48ce53fdf5184
|
|
| BLAKE2b-256 |
47fa4850dfffc3a8880deff4895b18cfbcfd4804a6dff2c43a8104d032538f24
|