Skip to content

feat: pydantic validation as djc extension #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add more tests
  • Loading branch information
JuroOravec committed Mar 25, 2025
commit 4dca74c950882b24e09b254f8890cdd7ccb54138
1 change: 0 additions & 1 deletion src/djc_ext_pydantic/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django_components import Component
from pydantic import TypeAdapter, ValidationError


ComponentTypes = Tuple[Any, Any, Any, Any, Any, Any]


Expand Down
76 changes: 75 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Any, Tuple
from typing import Any, Dict, List, Tuple, Type, Union

import pytest
from django_components import Component, SlotContent, types
Expand Down Expand Up @@ -427,3 +427,77 @@ def get_context_data(self, a, b, c, var1, var2, **attrs):
"slot2": lambda ctx, data, ref: "abc",
},
)

@djc_test(
components_settings={"extensions": [PydanticExtension]},
)
def test_handles_component_types(self):
TestArgs = Tuple[Type[Component]]

class TestKwargs(TypedDict):
component: Type[Component]

class TestComponent(Component[TestArgs, TestKwargs, Any, Any, Any, Any]):
def get_context_data(self, a, component, **attrs):
return {
"component": component,
}

template: types.django_html = """
{% load component_tags %}
Component: <strong>{{ component }}</strong>
"""

with pytest.raises(
ValidationError,
match=re.escape("Positional arguments of component 'TestComponent' failed validation"),
):
TestComponent.render(
args=[123], # type: ignore
kwargs={"component": 1}, # type: ignore
)

TestComponent.render(
args=(TestComponent,),
kwargs={"component": TestComponent},
)

def test_handles_typing_module(self):
TodoArgs = Tuple[
Union[str, int],
Dict[str, int],
List[str],
Tuple[int, Union[str, int]],
]

class TodoKwargs(TypedDict):
one: Union[str, int]
two: Dict[str, int]
three: List[str]
four: Tuple[int, Union[str, int]]

class TodoData(TypedDict):
one: Union[str, int]
two: Dict[str, int]
three: List[str]
four: Tuple[int, Union[str, int]]

TodoComp = Component[TodoArgs, TodoKwargs, Any, TodoData, Any, Any]

class TestComponent(TodoComp):
def get_context_data(self, *args, **kwargs):
return {
**kwargs,
}

template = ""

TestComponent.render(
args=("str", {"str": 123}, ["a", "b", "c"], (123, "123")),
kwargs={
"one": "str",
"two": {"str": 123},
"three": ["a", "b", "c"],
"four": (123, "123"),
},
)
Loading