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": {"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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_nested-0.1.1.tar.gz.
File metadata
- Download URL: pydantic_nested-0.1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/24.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e557e01a4013e71fa7288b8e06c9308acaadf98f9ad34aa684fb05efa8cf9135
|
|
| MD5 |
7afff4e665d2f6187eede4232126d37e
|
|
| BLAKE2b-256 |
a1ac188c3b8d96f1f1dadb4c862e429e234f62a8b99c236838c201755fa0ef52
|
File details
Details for the file pydantic_nested-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_nested-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44bc61cd80077e8f21e8d951dbdab95e06b98d7a5de2383109336afee2774959
|
|
| MD5 |
45b4111986a9f4a0b2b4bb007653b4b6
|
|
| BLAKE2b-256 |
33e3a13f9883da913cc5e85d153c0372a19b5b60b57615ac85bf5f2ffa2e9243
|