Skip to main content

Input validation with Pydantic for Django Components

Project description

djc-ext-pydantic

PyPI - Version PyPI - Python Version PyPI - License PyPI - Downloads GitHub Actions Workflow Status

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:

    • args: Positional arguments, expected to be defined as a Tuple type.
    • kwargs: Keyword arguments, can be defined using TypedDict or Pydantic's BaseModel.
    • slots: Can also be defined using TypedDict or Pydantic's BaseModel.
  • Outputs:

    • Data returned from get_context_data(), get_js_data(), and get_css_data(), which can be defined using TypedDict or Pydantic's BaseModel.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djc_ext_pydantic-1.0.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djc_ext_pydantic-1.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

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

Hashes for djc_ext_pydantic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 49649525b6cb19d60903b2076bdc7b4feeb74b6a665030be39f1fb5d839f2998
MD5 fabf85b05be10b5ecf447723ce81bf08
BLAKE2b-256 c5be7af7b407922524bcc5301b4d611a8aa8f702b7460b0593fc5f832feb2600

See more details on using hashes here.

File details

Details for the file djc_ext_pydantic-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for djc_ext_pydantic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9042f3f644f4dd721d050d21e7691ea5d67c6c11e889dcfbe2c68f356916c039
MD5 5f8c729726e6dc5e10a48ce53fdf5184
BLAKE2b-256 47fa4850dfffc3a8880deff4895b18cfbcfd4804a6dff2c43a8104d032538f24

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page