-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The project structure, including the Github workflows, were based on django-components repo.
|
||
# For custom classes, Pydantic needs to be told how to handle them. | ||
# We achieve that by setting the `__get_pydantic_core_schema__` attribute on the classes. | ||
def monkeypatch_pydantic_core_schema() -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file modifies the classes defined in Django components / Django, so that they these classes can be used in typing.
types_store = WeakKeyDictionary() | ||
|
||
|
||
def get_component_typing(cls: Type[Component]) -> Optional[ComponentTypes]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file, together with on_component_input
and on_component_data
in src/djc_ext_pydantic/extension.py
are practically what was on the Component class previously here
Slot 2: {% slot "my_slot2" / %} | ||
""" | ||
|
||
# TODO: Set more specific error message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bit of a chicken-and-egg issue with the tests:
- The tests run on latest django-components, which is v0.135
- v0.135 still has the built-in validation, which runs BEFORE this pydantic validation
- So the error message and raised error types are different.
So right now, these tests assert that the validation written inside django-components works.
But to be able to test the Pydantic validation, we'd need to update to v0.136.
But for better user experience, it'd be better if this pydantic extension was already available once 0.136 comes out, so they can switch right away.
So for now I've set the tests like this, just checking that some exception is raised, so we're not blocked by the tests, and so we can release djc-ext-pydantic
v1.
Then, after django-components v0.136 comes out, that's when we can update this code to check for proper error messages.
I did the tests locally, where I was able to use the dev build of django-components, and it all passed too:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good from a quick read!
This moves the input validation for components into its own extension / project.
Instead of home-brew validation, this extension uses Pydantic. This also means that it will be finally possible to define nested types like a dictionary within a dictonary. Because this was not possible with the custom validation logic.
The tests are currently failing, as django-components will also have to be updated to remove the validation logic. So the course of action is:
NOTE: The tests will be failing until django-components v0.136, see this comment.