Skip to main content

Pydantic models from nested dictionaries

Project description

Pydantic Nested

This Python library allows you to create Pydantic models with support for nested dictionaries, lists, default values, and field constraints. The schema is defined using standard Python types and Pydantic's features.

Features

  • Supports simple fields (e.g., str, int, float)
  • Nested dictionaries and lists
  • Default values for fields
  • Pydantic field constraints (e.g., conint, conlist)
  • Full validation with dynamically created models

Usage

Where could this be useful? Sometimes data models can be very nested. Also, API queries exposing search engines such as elasticsearch can get convoluted and required validation. Models defined in dictionary format instead of classes could be easier to read.

Define the model

from pydantic import Field, conint, conlist
from pydantic_nested.model import create_nested_model

schema = {
    "str1": str,
    "str2": (str, "default_string"),
    "str3": (str | None, None),
    "str4": (str, Field("str4 default value", description="str4 description")),
    "list1": list,
    "list2": list | tuple,
    "list3": list[int | str],
    "list4": (list, None),
    "list5": (list[float], Field(default_factory=list)),
    "dict1": {
        "str1": (str | None, None),
        "str2": (str, "default_string"),
    },
    "dict2": {"product_ids": conlist(int, min_length=1, max_length=20)},
    "dict3": {"include": {"product_ids": list[conint(ge=1)]}},
    "dict_list_nullable": (
        {
            "str1": (str | None, None),
            "str2": (str, "default_string"),
        },
        None,
    ),
    "list_of_objects1": [{"str1": str}],
    "list_of_objects2": [{"str1": str}, {"bool1": bool}],
    "list_of_objects3": ([{"str1": str}, {"bool1": bool}], None),
    "list_of_objects4": ([{"str1": str}, {"bool1": bool}], Field(default_factory=list, max_length=5)),
}

Create the model

TestModel = create_nested_model(schema, "TestModel")

sample_data = {
    "str1": "example string",
    "list1": [1, 2, 3],
    "list2": (1, 2),
    "list3": ["1", 2],
    "dict1": {},
    "dict2": {"product_ids": [1, 2, 3]},
    "dict3": {"include": {"product_ids": [1, 2, 3]}},
    "list_of_objects1": [{"str1": "object1"}],
    "list_of_objects2": [{"bool1": True}],
}

# Instantiate the model and validate the data
model_instance = TestModel(**sample_data)
print(model_instance.dict())

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

pydantic_nested-0.1.2.tar.gz (38.7 kB view details)

Uploaded Source

Built Distribution

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

pydantic_nested-0.1.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_nested-0.1.2.tar.gz.

File metadata

  • Download URL: pydantic_nested-0.1.2.tar.gz
  • Upload date:
  • Size: 38.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for pydantic_nested-0.1.2.tar.gz
Algorithm Hash digest
SHA256 707a989ba8f93700134d16149befa037a596c59e442df02622451fc88595fe35
MD5 e1262fc38585bbf0e686280d9695aef0
BLAKE2b-256 247529c99290c0bc29b8822a7a8ec34bd7fe23add8ecf68fcc837283d46b741e

See more details on using hashes here.

File details

Details for the file pydantic_nested-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_nested-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe12c54f3215ca1b415454347442fec21ec812f1abe04ba54cce51ce478fcd24
MD5 d560163e2899d85f2f1571fd0867f086
BLAKE2b-256 0da9ddeee7aa4baccbe245f223998fdc652f7af73750629415b0a523eca3c08c

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