Skip to main content

View decorator to create the child pydantic models from the root model.

Project description

Pydantic view helper decorator

License: MIT

Installation

pip install pydantic_view

Usage

In [1]: from uuid import UUID, uuid4
   ...: 
   ...: from pydantic import BaseModel, Field
   ...: from pydantic_view import view
   ...: 
   ...: 
   ...: @view("Create", exclude={"id"})
   ...: @view("Update")
   ...: @view("Patch", optional={"username", "password", "address"})
   ...: @view("Out", exclude={"password"})
   ...: class User(BaseModel):
   ...:     id: int
   ...:     username: str
   ...:     password: str
   ...:     address: str
   ...: 

In [2]: user = User(id=0, username="human", password="iamaman", address="Earth")
   ...: user.Out()
   ...: 
Out[2]: UserOut(id=0, username='human', address='Earth')

In [3]: User.Update(id=0, username="human", password="iamasuperman", address="Earth")
   ...: 
Out[3]: UserUpdate(id=0, username='human', password='iamasuperman', address='Earth')

In [4]: User.Patch(id=0, address="Mars")
   ...: 
Out[4]: UserPatch(id=0, username=None, password=None, address='Mars')

FastAPI example

from typing import List, Optional

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel, ConfigDict, Field

from pydantic_view import view, view_field_validator

@view("Out", exclude={"secret"})
@view("Create")
@view("Update")
@view("Patch", optional_not_none={"public", "secret"})
class UserSettings(BaseModel):
    model_config = ConfigDict(extra="forbid")

    public: Optional[str] = None
    secret: Optional[str] = None


@view("Out", exclude={"password"})
@view("Create", exclude=["id"], fields={"settings": Field(default_factory=UserSettings)})
@view("Update", exclude={"id"})
@view("Patch", exclude={"id"}, optional_not_none={"username", "password", "settings"})
class User(BaseModel):
    model_config = ConfigDict(extra="forbid")

    id: int
    username: str
    password: str = Field(default_factory=lambda: "password")
    settings: UserSettings

    @view_field_validator(["Create", "Update", "Patch"], "username")
    @classmethod
    def validate_username(cls, v):
        if len(v) < 3:
            raise ValueError
        return v


app = FastAPI()
db = {}


@app.get("/users/{user_id}", response_model=User.Out)
async def get(user_id: int) -> User.Out:
    return db[user_id]


@app.post("/users", response_model=User.Out)
async def post(user: User.Create) -> User.Out:
    user_id = 0  # generate_user_id()
    db[0] = User(id=user_id, **user.model_dump())
    return db[0]


@app.put("/users/{user_id}", response_model=User.Out)
async def put(user_id: int, user: User.Update) -> User.Out:
    db[user_id] = User(id=user_id, **user.model_dump())
    return db[user_id]


@app.patch("/users/{user_id}", response_model=User.Out)
async def patch(user_id: int, user: User.Patch) -> User.Out:
    db[user_id] = User(**{**db[user_id].model_dump(), **user.model_dump(exclude_unset=True)})
    return db[user_id]


def test_fastapi():
    client = TestClient(app)

    # POST
    response = client.post(
        "/users",
        json={
            "username": "admin",
            "password": "admin",
        },
    )
    assert response.status_code == 200, response.text
    assert response.json() == {
        "id": 0,
        "username": "admin",
        "settings": {"public": None},
    }

    # GET
    response = client.get("/users/0")
    assert response.status_code == 200, response.text
    assert response.json() == {
        "id": 0,
        "username": "admin",
        "settings": {"public": None},
    }

    # PUT
    response = client.put(
        "/users/0",
        json={
            "username": "superadmin",
            "password": "superadmin",
            "settings": {"public": "foo", "secret": "secret"},
        },
    )
    assert response.status_code == 200, response.text
    assert response.json() == {
        "id": 0,
        "username": "superadmin",
        "settings": {"public": "foo"},
    }

    # PATCH
    response = client.patch(
        "/users/0",
        json={
            "username": "guest",
            "settings": {"public": "bar"},
        },
    )
    assert response.status_code == 200, response.text
    assert response.json() == {
        "id": 0,
        "username": "guest",
        "settings": {"public": "bar"},
    }

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_view-2.0.0a0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

pydantic_view-2.0.0a0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_view-2.0.0a0.tar.gz.

File metadata

  • Download URL: pydantic_view-2.0.0a0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/20.6.0

File hashes

Hashes for pydantic_view-2.0.0a0.tar.gz
Algorithm Hash digest
SHA256 77414982ad10992274f895d68bf11d3764fcfb8e989420996a13027d320db421
MD5 db43b26434676e179c776a61a071f0ea
BLAKE2b-256 b5c83a06335905c5083f265b936037582729a1b00e773110f4fa8008ad45249d

See more details on using hashes here.

File details

Details for the file pydantic_view-2.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_view-2.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/20.6.0

File hashes

Hashes for pydantic_view-2.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 69c87854e9e8160302849426ceffd11f6dd8b6b1a517f3b4fbba3f823ccc73f9
MD5 124a745edcbcbb8658219e913534db09
BLAKE2b-256 2f6774c97d66d7bb926360ec38cbf97331b626a741c5613389f79763b64f1da6

See more details on using hashes here.

Supported by

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