Skip to main content

Makes partial Pydantic models without making fields nullable.

Project description

pydantic-strict-partial

PyPI version PyPI Supported Python Versions CI badge

Like pydantic-partial, but respects all the validators, and not nullable field. GitHub homepage.

Installation

pydantic-strict-partial compatible with Python 3.10+ and Pydantic 2.1+.

Using pip

pip install pydantic-strict-partial

Using poetry

poetry add pydantic-strict-partial

About

Create partial models from your normal pydantic models. Partial models will allow some or all fields to be optional and thus not be required when creating the model instance.

The most common use case is a PATCH request on FastAPI endpoints where you want to allow partial updates.

Usage

from typing import Annotated

from annotated_types import Ge
from pydantic import BaseModel

from pydantic_strict_partial import create_partial_model


class UserSchema(BaseModel):
    name: str
    nickname: str | None
    age: Annotated[int, Ge(18)]


UserPartialUpdateSchema = create_partial_model(UserSchema)

assert UserPartialUpdateSchema(age=20).model_dump(exclude_unset=True) == {
    'age': 20
}

UserPartialUpdateSchema(name=None)  # raises ValidationError
UserPartialUpdateSchema(age=17)  # raises ValidationError

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_strict_partial-0.2.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

pydantic_strict_partial-0.2.0-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

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