Operation Schema¶
This module contains OperationSchema, the base class for built-in and custom
patch operations.
OperationSchema
¶
Bases: BaseModel, ABC
Base class for typed JSON Patch operations.
An OperationSchema is a Pydantic model representing one JSON Patch operation:
standard RFC 6902 operations (add/remove/replace/...) and custom domain operations.
The library's workflow is:
- Define operations as Pydantic models.
- Register them in an
OperationRegistry. - Parse incoming patch documents into concrete operation instances via a discriminated union
keyed by
op. - Apply operations sequentially by calling
apply.
Example
Required op field:
class ReplaceOp(OperationSchema):
op: Literal["replace"] = "replace"
path: JSONPointer[JSONValue]
value: JSONValue
Multiple identifiers (aliases):
class CreateOp(OperationSchema):
op: Literal["create", "add"] = "create"
Notes
opmust be a normal annotated attribute, not aClassVar.ClassVarvalues are not Pydantic fields and cannot participate in discriminated-union dispatch.- Instances are frozen and strict by default.
- Instances are revalidated when parsed, which matters for fields that depend on validation context (for example, registry-scoped pointer backends).
- Subclasses are validated at class-definition time. If
opis not declared correctly, the class raisesInvalidOperationDefinitionduring import.
Source code in jsonpatchx/schema.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
apply(doc)
abstractmethod
¶
Apply this operation to doc and return the updated document.
Notes
- Implementations may mutate the provided
docobject in-place and should return the updated document (often the same object). - Raise
PatchErrorsubclasses for expected patch failures. Unexpected exceptions will be wrapped by the patch engine. - Whether the caller-owned document is mutated is controlled by the patch engine
(see
_apply_ops(..., inplace=...)), not by this method.