djc-ext-pydantic 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.
- djc_ext_pydantic-0.1.0/LICENSE +21 -0
- djc_ext_pydantic-0.1.0/PKG-INFO +167 -0
- djc_ext_pydantic-0.1.0/README.md +134 -0
- djc_ext_pydantic-0.1.0/pyproject.toml +103 -0
- djc_ext_pydantic-0.1.0/setup.cfg +4 -0
- djc_ext_pydantic-0.1.0/src/djc_ext_pydantic.egg-info/PKG-INFO +167 -0
- djc_ext_pydantic-0.1.0/src/djc_ext_pydantic.egg-info/SOURCES.txt +15 -0
- djc_ext_pydantic-0.1.0/src/djc_ext_pydantic.egg-info/dependency_links.txt +1 -0
- djc_ext_pydantic-0.1.0/src/djc_ext_pydantic.egg-info/requires.txt +2 -0
- djc_ext_pydantic-0.1.0/src/djc_ext_pydantic.egg-info/top_level.txt +1 -0
- djc_ext_pydantic-0.1.0/src/djc_pydantic/__init__.py +9 -0
- djc_ext_pydantic-0.1.0/src/djc_pydantic/extension.py +106 -0
- djc_ext_pydantic-0.1.0/src/djc_pydantic/monkeypatch.py +50 -0
- djc_ext_pydantic-0.1.0/src/djc_pydantic/py.typed +0 -0
- djc_ext_pydantic-0.1.0/src/djc_pydantic/validation.py +106 -0
- djc_ext_pydantic-0.1.0/tests/test_validation.py +514 -0
- djc_ext_pydantic-0.1.0/tests/testutils.py +54 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Juro Oravec
|
|
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,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: djc-ext-pydantic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Input validation with Pydantic for Django Components
|
|
5
|
+
Author-email: Juro Oravec <juraj.oravec.josefson@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/django-components/djc-ext-pydantic/
|
|
8
|
+
Keywords: pydantic,django-components,djc,django,components
|
|
9
|
+
Classifier: Framework :: Django
|
|
10
|
+
Classifier: Framework :: Django :: 4.2
|
|
11
|
+
Classifier: Framework :: Django :: 5.0
|
|
12
|
+
Classifier: Framework :: Django :: 5.1
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Environment :: Web Environment
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
25
|
+
Classifier: Intended Audience :: Developers
|
|
26
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
27
|
+
Requires-Python: <4.0,>=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: django-components>=0.131
|
|
31
|
+
Requires-Dist: pydantic>=2.9
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# djc-ext-pydantic
|
|
35
|
+
|
|
36
|
+
[](https://pypi.org/project/djc-ext-pydantic/) [](https://pypi.org/project/djc-ext-pydantic/) [](https://github.com/django-components/djc-ext-pydantic/blob/main/LICENSE) [](https://pypistats.org/packages/djc-ext-pydantic) [](https://github.com/django-components/djc-ext-pydantic/actions/workflows/tests.yml)
|
|
37
|
+
|
|
38
|
+
Validate components' inputs and outputs using Pydantic.
|
|
39
|
+
|
|
40
|
+
`djc-ext-pydantic` is a [django-component](https://github.com/django-components/django-components) extension that integrates [Pydantic](https://pydantic.dev/) for input and data validation. It uses the types defined on the component's class to validate both inputs and outputs of Django components.
|
|
41
|
+
|
|
42
|
+
### Validated Inputs and Outputs
|
|
43
|
+
|
|
44
|
+
- **Inputs:**
|
|
45
|
+
|
|
46
|
+
- `args`: Positional arguments, expected to be defined as a [`Tuple`](https://docs.python.org/3/library/typing.html#typing.Tuple) type.
|
|
47
|
+
- `kwargs`: Keyword arguments, can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
48
|
+
- `slots`: Can also be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
49
|
+
|
|
50
|
+
- **Outputs:**
|
|
51
|
+
- Data returned from `get_context_data()`, `get_js_data()`, and `get_css_data()`, which can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
52
|
+
|
|
53
|
+
### Example Usage
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from pydantic import BaseModel
|
|
57
|
+
from typing import Tuple, TypedDict
|
|
58
|
+
|
|
59
|
+
# 1. Define the types
|
|
60
|
+
MyCompArgs = Tuple[str, ...]
|
|
61
|
+
|
|
62
|
+
class MyCompKwargs(TypedDict):
|
|
63
|
+
name: str
|
|
64
|
+
age: int
|
|
65
|
+
|
|
66
|
+
class MyCompSlots(TypedDict):
|
|
67
|
+
header: SlotContent
|
|
68
|
+
footer: SlotContent
|
|
69
|
+
|
|
70
|
+
class MyCompData(BaseModel):
|
|
71
|
+
data1: str
|
|
72
|
+
data2: int
|
|
73
|
+
|
|
74
|
+
class MyCompJsData(BaseModel):
|
|
75
|
+
js_data1: str
|
|
76
|
+
js_data2: int
|
|
77
|
+
|
|
78
|
+
class MyCompCssData(BaseModel):
|
|
79
|
+
css_data1: str
|
|
80
|
+
css_data2: int
|
|
81
|
+
|
|
82
|
+
# 2. Define the component with those types
|
|
83
|
+
class MyComponent(Component[
|
|
84
|
+
MyCompArgs,
|
|
85
|
+
MyCompKwargs,
|
|
86
|
+
MyCompSlots,
|
|
87
|
+
MyCompData,
|
|
88
|
+
MyCompJsData,
|
|
89
|
+
MyCompCssData,
|
|
90
|
+
]):
|
|
91
|
+
...
|
|
92
|
+
|
|
93
|
+
# 3. Render the component
|
|
94
|
+
MyComponent.render(
|
|
95
|
+
# ERROR: Expects a string
|
|
96
|
+
args=(123,),
|
|
97
|
+
kwargs={
|
|
98
|
+
"name": "John",
|
|
99
|
+
# ERROR: Expects an integer
|
|
100
|
+
"age": "invalid",
|
|
101
|
+
},
|
|
102
|
+
slots={
|
|
103
|
+
"header": "...",
|
|
104
|
+
# ERROR: Expects key "footer"
|
|
105
|
+
"foo": "invalid",
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
If you don't want to validate some parts, set them to [`Any`](https://docs.python.org/3/library/typing.html#typing.Any).
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
class MyComponent(Component[
|
|
114
|
+
MyCompArgs,
|
|
115
|
+
MyCompKwargs,
|
|
116
|
+
MyCompSlots,
|
|
117
|
+
Any,
|
|
118
|
+
Any,
|
|
119
|
+
Any,
|
|
120
|
+
]):
|
|
121
|
+
...
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Installation
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
pip install djc-ext-pydantic
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Then add the extension to your project:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
# settings.py
|
|
134
|
+
COMPONENTS = {
|
|
135
|
+
"extensions": [
|
|
136
|
+
"djc_pydantic.PydanticExtension",
|
|
137
|
+
],
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
or by reference:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
# settings.py
|
|
145
|
+
from djc_pydantic import PydanticExtension
|
|
146
|
+
|
|
147
|
+
COMPONENTS = {
|
|
148
|
+
"extensions": [
|
|
149
|
+
PydanticExtension,
|
|
150
|
+
],
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Release notes
|
|
155
|
+
|
|
156
|
+
Read the [Release Notes](https://github.com/django-components/djc-ext-pydantic/tree/main/CHANGELOG.md)
|
|
157
|
+
to see the latest features and fixes.
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
### Tests
|
|
162
|
+
|
|
163
|
+
To run tests, use:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
pytest
|
|
167
|
+
```
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# djc-ext-pydantic
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/djc-ext-pydantic/) [](https://pypi.org/project/djc-ext-pydantic/) [](https://github.com/django-components/djc-ext-pydantic/blob/main/LICENSE) [](https://pypistats.org/packages/djc-ext-pydantic) [](https://github.com/django-components/djc-ext-pydantic/actions/workflows/tests.yml)
|
|
4
|
+
|
|
5
|
+
Validate components' inputs and outputs using Pydantic.
|
|
6
|
+
|
|
7
|
+
`djc-ext-pydantic` is a [django-component](https://github.com/django-components/django-components) extension that integrates [Pydantic](https://pydantic.dev/) for input and data validation. It uses the types defined on the component's class to validate both inputs and outputs of Django components.
|
|
8
|
+
|
|
9
|
+
### Validated Inputs and Outputs
|
|
10
|
+
|
|
11
|
+
- **Inputs:**
|
|
12
|
+
|
|
13
|
+
- `args`: Positional arguments, expected to be defined as a [`Tuple`](https://docs.python.org/3/library/typing.html#typing.Tuple) type.
|
|
14
|
+
- `kwargs`: Keyword arguments, can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
15
|
+
- `slots`: Can also be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
16
|
+
|
|
17
|
+
- **Outputs:**
|
|
18
|
+
- Data returned from `get_context_data()`, `get_js_data()`, and `get_css_data()`, which can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
19
|
+
|
|
20
|
+
### Example Usage
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from pydantic import BaseModel
|
|
24
|
+
from typing import Tuple, TypedDict
|
|
25
|
+
|
|
26
|
+
# 1. Define the types
|
|
27
|
+
MyCompArgs = Tuple[str, ...]
|
|
28
|
+
|
|
29
|
+
class MyCompKwargs(TypedDict):
|
|
30
|
+
name: str
|
|
31
|
+
age: int
|
|
32
|
+
|
|
33
|
+
class MyCompSlots(TypedDict):
|
|
34
|
+
header: SlotContent
|
|
35
|
+
footer: SlotContent
|
|
36
|
+
|
|
37
|
+
class MyCompData(BaseModel):
|
|
38
|
+
data1: str
|
|
39
|
+
data2: int
|
|
40
|
+
|
|
41
|
+
class MyCompJsData(BaseModel):
|
|
42
|
+
js_data1: str
|
|
43
|
+
js_data2: int
|
|
44
|
+
|
|
45
|
+
class MyCompCssData(BaseModel):
|
|
46
|
+
css_data1: str
|
|
47
|
+
css_data2: int
|
|
48
|
+
|
|
49
|
+
# 2. Define the component with those types
|
|
50
|
+
class MyComponent(Component[
|
|
51
|
+
MyCompArgs,
|
|
52
|
+
MyCompKwargs,
|
|
53
|
+
MyCompSlots,
|
|
54
|
+
MyCompData,
|
|
55
|
+
MyCompJsData,
|
|
56
|
+
MyCompCssData,
|
|
57
|
+
]):
|
|
58
|
+
...
|
|
59
|
+
|
|
60
|
+
# 3. Render the component
|
|
61
|
+
MyComponent.render(
|
|
62
|
+
# ERROR: Expects a string
|
|
63
|
+
args=(123,),
|
|
64
|
+
kwargs={
|
|
65
|
+
"name": "John",
|
|
66
|
+
# ERROR: Expects an integer
|
|
67
|
+
"age": "invalid",
|
|
68
|
+
},
|
|
69
|
+
slots={
|
|
70
|
+
"header": "...",
|
|
71
|
+
# ERROR: Expects key "footer"
|
|
72
|
+
"foo": "invalid",
|
|
73
|
+
},
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
If you don't want to validate some parts, set them to [`Any`](https://docs.python.org/3/library/typing.html#typing.Any).
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
class MyComponent(Component[
|
|
81
|
+
MyCompArgs,
|
|
82
|
+
MyCompKwargs,
|
|
83
|
+
MyCompSlots,
|
|
84
|
+
Any,
|
|
85
|
+
Any,
|
|
86
|
+
Any,
|
|
87
|
+
]):
|
|
88
|
+
...
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Installation
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install djc-ext-pydantic
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Then add the extension to your project:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
# settings.py
|
|
101
|
+
COMPONENTS = {
|
|
102
|
+
"extensions": [
|
|
103
|
+
"djc_pydantic.PydanticExtension",
|
|
104
|
+
],
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
or by reference:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
# settings.py
|
|
112
|
+
from djc_pydantic import PydanticExtension
|
|
113
|
+
|
|
114
|
+
COMPONENTS = {
|
|
115
|
+
"extensions": [
|
|
116
|
+
PydanticExtension,
|
|
117
|
+
],
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Release notes
|
|
122
|
+
|
|
123
|
+
Read the [Release Notes](https://github.com/django-components/djc-ext-pydantic/tree/main/CHANGELOG.md)
|
|
124
|
+
to see the latest features and fixes.
|
|
125
|
+
|
|
126
|
+
## Development
|
|
127
|
+
|
|
128
|
+
### Tests
|
|
129
|
+
|
|
130
|
+
To run tests, use:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pytest
|
|
134
|
+
```
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "djc-ext-pydantic"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
requires-python = ">=3.8, <4.0"
|
|
9
|
+
description = "Input validation with Pydantic for Django Components"
|
|
10
|
+
keywords = ["pydantic", "django-components", "djc", "django", "components"]
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Juro Oravec", email = "juraj.oravec.josefson@gmail.com"},
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Framework :: Django",
|
|
17
|
+
"Framework :: Django :: 4.2",
|
|
18
|
+
"Framework :: Django :: 5.0",
|
|
19
|
+
"Framework :: Django :: 5.1",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
"Programming Language :: Python",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.8",
|
|
24
|
+
"Programming Language :: Python :: 3.9",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Environment :: Web Environment",
|
|
30
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
31
|
+
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
|
|
32
|
+
"Intended Audience :: Developers",
|
|
33
|
+
"License :: OSI Approved :: MIT License",
|
|
34
|
+
]
|
|
35
|
+
dependencies = [
|
|
36
|
+
'django-components>=0.131',
|
|
37
|
+
'pydantic>=2.9',
|
|
38
|
+
]
|
|
39
|
+
license = {text = "MIT"}
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/django-components/djc-ext-pydantic/"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.packages.find]
|
|
46
|
+
where = ["src"]
|
|
47
|
+
include = ["djc_pydantic*"]
|
|
48
|
+
exclude = ["djc_pydantic.tests*"]
|
|
49
|
+
namespaces = false
|
|
50
|
+
|
|
51
|
+
[tool.black]
|
|
52
|
+
line-length = 119
|
|
53
|
+
include = '\.pyi?$'
|
|
54
|
+
exclude = '''
|
|
55
|
+
/(
|
|
56
|
+
\.git
|
|
57
|
+
| \.hg
|
|
58
|
+
| \.mypy_cache
|
|
59
|
+
| \.tox
|
|
60
|
+
| \.venv
|
|
61
|
+
| activate
|
|
62
|
+
| _build
|
|
63
|
+
| buck-out
|
|
64
|
+
| build
|
|
65
|
+
| dist
|
|
66
|
+
)/
|
|
67
|
+
'''
|
|
68
|
+
|
|
69
|
+
[tool.isort]
|
|
70
|
+
profile = "black"
|
|
71
|
+
line_length = 119
|
|
72
|
+
multi_line_output = 3
|
|
73
|
+
include_trailing_comma = "True"
|
|
74
|
+
known_first_party = "djc_pydantic"
|
|
75
|
+
|
|
76
|
+
[tool.flake8]
|
|
77
|
+
ignore = ['E302', 'W503']
|
|
78
|
+
max-line-length = 119
|
|
79
|
+
exclude = [
|
|
80
|
+
'__pycache__',
|
|
81
|
+
'env',
|
|
82
|
+
'.env',
|
|
83
|
+
'.venv',
|
|
84
|
+
'.tox',
|
|
85
|
+
'build',
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
[tool.mypy]
|
|
89
|
+
check_untyped_defs = true
|
|
90
|
+
ignore_missing_imports = true
|
|
91
|
+
exclude = [
|
|
92
|
+
'build',
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
[[tool.mypy.overrides]]
|
|
96
|
+
module = "djc_pydantic.*"
|
|
97
|
+
disallow_untyped_defs = true
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
[tool.pytest.ini_options]
|
|
101
|
+
testpaths = [
|
|
102
|
+
"tests"
|
|
103
|
+
]
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: djc-ext-pydantic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Input validation with Pydantic for Django Components
|
|
5
|
+
Author-email: Juro Oravec <juraj.oravec.josefson@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/django-components/djc-ext-pydantic/
|
|
8
|
+
Keywords: pydantic,django-components,djc,django,components
|
|
9
|
+
Classifier: Framework :: Django
|
|
10
|
+
Classifier: Framework :: Django :: 4.2
|
|
11
|
+
Classifier: Framework :: Django :: 5.0
|
|
12
|
+
Classifier: Framework :: Django :: 5.1
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Environment :: Web Environment
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
25
|
+
Classifier: Intended Audience :: Developers
|
|
26
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
27
|
+
Requires-Python: <4.0,>=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: django-components>=0.131
|
|
31
|
+
Requires-Dist: pydantic>=2.9
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# djc-ext-pydantic
|
|
35
|
+
|
|
36
|
+
[](https://pypi.org/project/djc-ext-pydantic/) [](https://pypi.org/project/djc-ext-pydantic/) [](https://github.com/django-components/djc-ext-pydantic/blob/main/LICENSE) [](https://pypistats.org/packages/djc-ext-pydantic) [](https://github.com/django-components/djc-ext-pydantic/actions/workflows/tests.yml)
|
|
37
|
+
|
|
38
|
+
Validate components' inputs and outputs using Pydantic.
|
|
39
|
+
|
|
40
|
+
`djc-ext-pydantic` is a [django-component](https://github.com/django-components/django-components) extension that integrates [Pydantic](https://pydantic.dev/) for input and data validation. It uses the types defined on the component's class to validate both inputs and outputs of Django components.
|
|
41
|
+
|
|
42
|
+
### Validated Inputs and Outputs
|
|
43
|
+
|
|
44
|
+
- **Inputs:**
|
|
45
|
+
|
|
46
|
+
- `args`: Positional arguments, expected to be defined as a [`Tuple`](https://docs.python.org/3/library/typing.html#typing.Tuple) type.
|
|
47
|
+
- `kwargs`: Keyword arguments, can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
48
|
+
- `slots`: Can also be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
49
|
+
|
|
50
|
+
- **Outputs:**
|
|
51
|
+
- Data returned from `get_context_data()`, `get_js_data()`, and `get_css_data()`, which can be defined using [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) or Pydantic's [`BaseModel`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel).
|
|
52
|
+
|
|
53
|
+
### Example Usage
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from pydantic import BaseModel
|
|
57
|
+
from typing import Tuple, TypedDict
|
|
58
|
+
|
|
59
|
+
# 1. Define the types
|
|
60
|
+
MyCompArgs = Tuple[str, ...]
|
|
61
|
+
|
|
62
|
+
class MyCompKwargs(TypedDict):
|
|
63
|
+
name: str
|
|
64
|
+
age: int
|
|
65
|
+
|
|
66
|
+
class MyCompSlots(TypedDict):
|
|
67
|
+
header: SlotContent
|
|
68
|
+
footer: SlotContent
|
|
69
|
+
|
|
70
|
+
class MyCompData(BaseModel):
|
|
71
|
+
data1: str
|
|
72
|
+
data2: int
|
|
73
|
+
|
|
74
|
+
class MyCompJsData(BaseModel):
|
|
75
|
+
js_data1: str
|
|
76
|
+
js_data2: int
|
|
77
|
+
|
|
78
|
+
class MyCompCssData(BaseModel):
|
|
79
|
+
css_data1: str
|
|
80
|
+
css_data2: int
|
|
81
|
+
|
|
82
|
+
# 2. Define the component with those types
|
|
83
|
+
class MyComponent(Component[
|
|
84
|
+
MyCompArgs,
|
|
85
|
+
MyCompKwargs,
|
|
86
|
+
MyCompSlots,
|
|
87
|
+
MyCompData,
|
|
88
|
+
MyCompJsData,
|
|
89
|
+
MyCompCssData,
|
|
90
|
+
]):
|
|
91
|
+
...
|
|
92
|
+
|
|
93
|
+
# 3. Render the component
|
|
94
|
+
MyComponent.render(
|
|
95
|
+
# ERROR: Expects a string
|
|
96
|
+
args=(123,),
|
|
97
|
+
kwargs={
|
|
98
|
+
"name": "John",
|
|
99
|
+
# ERROR: Expects an integer
|
|
100
|
+
"age": "invalid",
|
|
101
|
+
},
|
|
102
|
+
slots={
|
|
103
|
+
"header": "...",
|
|
104
|
+
# ERROR: Expects key "footer"
|
|
105
|
+
"foo": "invalid",
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
If you don't want to validate some parts, set them to [`Any`](https://docs.python.org/3/library/typing.html#typing.Any).
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
class MyComponent(Component[
|
|
114
|
+
MyCompArgs,
|
|
115
|
+
MyCompKwargs,
|
|
116
|
+
MyCompSlots,
|
|
117
|
+
Any,
|
|
118
|
+
Any,
|
|
119
|
+
Any,
|
|
120
|
+
]):
|
|
121
|
+
...
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Installation
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
pip install djc-ext-pydantic
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Then add the extension to your project:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
# settings.py
|
|
134
|
+
COMPONENTS = {
|
|
135
|
+
"extensions": [
|
|
136
|
+
"djc_pydantic.PydanticExtension",
|
|
137
|
+
],
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
or by reference:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
# settings.py
|
|
145
|
+
from djc_pydantic import PydanticExtension
|
|
146
|
+
|
|
147
|
+
COMPONENTS = {
|
|
148
|
+
"extensions": [
|
|
149
|
+
PydanticExtension,
|
|
150
|
+
],
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Release notes
|
|
155
|
+
|
|
156
|
+
Read the [Release Notes](https://github.com/django-components/djc-ext-pydantic/tree/main/CHANGELOG.md)
|
|
157
|
+
to see the latest features and fixes.
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
### Tests
|
|
162
|
+
|
|
163
|
+
To run tests, use:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
pytest
|
|
167
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/djc_ext_pydantic.egg-info/PKG-INFO
|
|
5
|
+
src/djc_ext_pydantic.egg-info/SOURCES.txt
|
|
6
|
+
src/djc_ext_pydantic.egg-info/dependency_links.txt
|
|
7
|
+
src/djc_ext_pydantic.egg-info/requires.txt
|
|
8
|
+
src/djc_ext_pydantic.egg-info/top_level.txt
|
|
9
|
+
src/djc_pydantic/__init__.py
|
|
10
|
+
src/djc_pydantic/extension.py
|
|
11
|
+
src/djc_pydantic/monkeypatch.py
|
|
12
|
+
src/djc_pydantic/py.typed
|
|
13
|
+
src/djc_pydantic/validation.py
|
|
14
|
+
tests/test_validation.py
|
|
15
|
+
tests/testutils.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
djc_pydantic
|