Pydantic models for implementing JSON Patch.
Project description
Pydantic JSON Patch
Pydantic models for implementing JSON Patch.
Installation
Pydantic JSON Patch is published to PyPI, and can be installed with e.g.:
pip install pydantic-json-patch
Models
A model is provided for each of the six JSON Patch operations:
AddOpCopyOpMoveOpRemoveOpReplaceOpTestOp
As repeating the op is a bit awkward (CopyOp(op="copy", ...)), a create factory method is available:
>>> from pydantic_json_patch import AddOp
>>> op = AddOp.create(path="/foo/bar", value=123)
>>> op
AddOp(op='add', path='/foo/bar', value=123)
>>> op.model_dump_json()
'{"op":"add","path":"/foo/bar","value":123}'
Additionally, there are two compound models:
Operationis the union of all the operators; andJsonPatchrepresents a list of that union type.
Pointer tokens
The path property (and from property, where present) of an operation is a JSON Pointer.
This means that any ~ or / characters in property names need to be properly encoded.
To aid working with these, the models expose a read-only path_tokens property (and, where appropriate, from_tokens):
>>> from pydantic_json_patch import CopyOp
>>> op = CopyOp.model_validate_json('{"op":"copy","path":"/foo/bar~1new","from":"/foo/bar~0old"}')
>>> op
CopyOp(op='copy', path='/foo/bar~1new', from_='/foo/bar~0old')
>>> op.path_tokens
('foo', 'bar/new')
>>> op.from_tokens
('foo', 'bar~old')
Similarly, the create factory methods can accept tuples of tokens, and will encode them appropriately:
>>> from pydantic_json_patch import TestOp
>>> op = TestOp.create(path=("annotations", "scope/value"), value=None)
>>> op
TestOp(op='test', path='/annotations/scope~1value', value=None)
>>> op.model_dump_json()
'{"op":"test","path":"/annotations/scope~1value","value":null}'
FastAPI
You can use this package to validate a JSON Patch endpoint in a FastAPI application, for example:
import typing as tp
from uuid import UUID
from fastapi import Body, FastAPI
from pydantic_json_patch import JsonPatch
app = FastAPI()
@app.patch("/resource/{resource_id}")
def _(resource_id: UUID, operations: tp.Annotated[JsonPatch, Body()]) -> ...:
...
This will provide a sensible example of the request body:
and list the models along with the other schemas:
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_json_patch-0.1.2.tar.gz.
File metadata
- Download URL: pydantic_json_patch-0.1.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
999c086d881cdf03cf4247a551411c5c1d1d380981c298ead041b29ce1483fa3
|
|
| MD5 |
b628996c73e6df9f01b763390bbfe014
|
|
| BLAKE2b-256 |
bb7cbd0a3831c5b8f29eb5dc16369f3f90cbc3b8afd40640f999054dbb367cca
|
Provenance
The following attestation bundles were made for pydantic_json_patch-0.1.2.tar.gz:
Publisher:
push.yml on textbook/pydantic_json_patch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_json_patch-0.1.2.tar.gz -
Subject digest:
999c086d881cdf03cf4247a551411c5c1d1d380981c298ead041b29ce1483fa3 - Sigstore transparency entry: 929052100
- Sigstore integration time:
-
Permalink:
textbook/pydantic_json_patch@03045e287498d7bf15018c136f53a5e154f3cd9e -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/textbook
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push.yml@03045e287498d7bf15018c136f53a5e154f3cd9e -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydantic_json_patch-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_json_patch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
927c43c83df053f96dd47c1151d4033e1ffdad1def93255eb1c3f220c4b46001
|
|
| MD5 |
e311366de0f6e9a104662180d19202ee
|
|
| BLAKE2b-256 |
0f8ebfbeee785dc38128bf8bcc71213473a81e486ce39bedba6702877ae49b10
|
Provenance
The following attestation bundles were made for pydantic_json_patch-0.1.2-py3-none-any.whl:
Publisher:
push.yml on textbook/pydantic_json_patch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_json_patch-0.1.2-py3-none-any.whl -
Subject digest:
927c43c83df053f96dd47c1151d4033e1ffdad1def93255eb1c3f220c4b46001 - Sigstore transparency entry: 929052110
- Sigstore integration time:
-
Permalink:
textbook/pydantic_json_patch@03045e287498d7bf15018c136f53a5e154f3cd9e -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/textbook
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push.yml@03045e287498d7bf15018c136f53a5e154f3cd9e -
Trigger Event:
push
-
Statement type: