Skip to main content

No project description provided

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": {"member_ids": conlist(int, min_length=1, max_length=20)},
    "dict3": {"include": {"member_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": {"member_ids": [1, 2, 3]},
    "dict3": {"include": {"member_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.0.tar.gz (3.8 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.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_nested-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/24.0.0

File hashes

Hashes for pydantic_nested-0.1.0.tar.gz
Algorithm Hash digest
SHA256 965d639da91a6cccf2b0cda7ac74c31b3524296dd6744c17477d640dd614a74f
MD5 2c3e3001189360635bbb7ae81d0a75d2
BLAKE2b-256 383acc38077680a6e6d8923f70a6894709b71618f80e4e5a675164a797f94491

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_nested-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/24.0.0

File hashes

Hashes for pydantic_nested-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5cdbd41ecc7bf12abffc9626a09f6bb37b25b23e8080f148633c60e2d054130c
MD5 48e255df2f7407317582ffd2da68fee2
BLAKE2b-256 15ba257af636b4257aaf7cb0cb9fc0041a5000be98b55e709d6c7ef22730477b

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