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

Conversation

JuroOravec
Copy link
Contributor

@JuroOravec JuroOravec commented Mar 25, 2025

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:

  1. Make PR to remove validation from django-components and redirect users to this extension.
  2. Release django-components version that doesn't contain validation.
  3. Update this PR to the latest django-components version, merge and release.

NOTE: The tests will be failing until django-components v0.136, see this comment.

pull_request:
workflow_dispatch:

jobs:
Copy link
Contributor Author

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:
Copy link
Contributor Author

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]:
Copy link
Contributor Author

@JuroOravec JuroOravec Mar 25, 2025

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
Copy link
Contributor Author

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:

Screenshot 2025-04-01 at 12 14 02

@JuroOravec JuroOravec marked this pull request as ready for review April 1, 2025 10:28
@JuroOravec JuroOravec requested a review from EmilStenstrom April 1, 2025 10:29
Copy link

@EmilStenstrom EmilStenstrom left a 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!

@JuroOravec JuroOravec merged commit b1de5cd into main Apr 5, 2025
1 of 7 checks passed
@JuroOravec JuroOravec deleted the jo-feat-pydantic-validation branch April 5, 2025 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants