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.

Example Usage

from django_components import Component, SlotInput
from djc_pydantic import ArgsBaseModel
from pydantic import BaseModel

# 1. Define the Component with Pydantic models
class MyComponent(Component):
    class Args(ArgsBaseModel):
        var1: str

    class Kwargs(BaseModel):
        name: str
        age: int

    class Slots(BaseModel):
        header: SlotInput
        footer: SlotInput

    class TemplateData(BaseModel):
        data1: str
        data2: int

    class JsData(BaseModel):
        js_data1: str
        js_data2: int

    class CssData(BaseModel):
        css_data1: str
        css_data2: int

    ...

# 2. 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",
    },
)

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,
    ],
}

Validating args

By default, Pydantic's BaseModel requires all fields to be passed as keyword arguments. If you want to validate positional arguments, you can use a custom subclass ArgsBaseModel:

from pydantic import BaseModel
from djc_pydantic import ArgsBaseModel

class MyTable(Component):
    class Args(ArgsBaseModel):
        a: int
        b: str
        c: float

MyTable.render(
    args=[1, "hello", 3.14],
)

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.1.0.tar.gz (7.8 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.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file djc_ext_pydantic-1.1.0.tar.gz.

File metadata

  • Download URL: djc_ext_pydantic-1.1.0.tar.gz
  • Upload date:
  • Size: 7.8 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.1.0.tar.gz
Algorithm Hash digest
SHA256 c583dd6bfcc687a18a8510391787c7bb0b3b7fe10886bbe48fcdff240b21d788
MD5 70cd006897ad5c7befef0ea548079b77
BLAKE2b-256 ba72c64a5a0e849d831bf210acaf49120035502e57341387cbccafc3b0469562

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djc_ext_pydantic-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d94279d058bdae6eba45fad7229372cc514c3e49db1d3ef7b71f5bf44a9111f
MD5 60a5c39a18718c62bb4c1f492e3aac9c
BLAKE2b-256 ba6820d628d53fb78d847261910c2ee61ab888a7687d697d5f2cb3a0c664767f

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