Skip to content

Built-in Operations

This module contains the built-in RFC 6902 operation models: AddOp, RemoveOp, ReplaceOp, MoveOp, CopyOp, and TestOp.

AddOp

Bases: OperationSchema, Generic[T]

RFC 6902 add operation.

Source code in jsonpatchx/builtins.py
class AddOp(OperationSchema, Generic[T]):
    """RFC 6902 add operation."""

    model_config = ConfigDict(
        title="Add operation",
        json_schema_extra={"description": "RFC 6902 add operation."},
    )

    op: Literal["add"] = "add"
    path: JSONPointer[T]
    value: T

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        return self.path.add(doc, self.value)

CopyOp

Bases: OperationSchema, Generic[T]

RFC 6902 copy operation.

Source code in jsonpatchx/builtins.py
class CopyOp(OperationSchema, Generic[T]):
    """RFC 6902 copy operation."""

    model_config = ConfigDict(
        title="Copy operation",
        json_schema_extra={"description": "RFC 6902 copy operation."},
    )

    op: Literal["copy"] = "copy"
    from_: JSONPointer[T] = Field(alias="from")
    path: JSONPointer[T]

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        value = self.from_.get(doc)
        duplicate = copy.deepcopy(value)
        return AddOp[T](path=self.path, value=duplicate).apply(doc)

MoveOp

Bases: OperationSchema, Generic[T]

RFC 6902 move operation.

Source code in jsonpatchx/builtins.py
class MoveOp(OperationSchema, Generic[T]):
    """RFC 6902 move operation."""

    model_config = ConfigDict(
        title="Move operation",
        json_schema_extra={"description": "RFC 6902 move operation."},
    )

    op: Literal["move"] = "move"
    from_: JSONPointer[T] = Field(alias="from")
    path: JSONPointer[T]

    @model_validator(mode="after")
    def _reject_proper_prefixes(self) -> Self:
        if self.from_.is_parent_of(self.path):
            raise OperationValidationError(
                "pointer 'path' cannot be a child of pointer 'from'"
            )
        return self

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        value = self.from_.get(doc)
        doc = RemoveOp[T](path=self.from_).apply(doc)
        return AddOp[T](path=self.path, value=value).apply(doc)

RemoveOp

Bases: OperationSchema, Generic[T]

RFC 6902 remove operation. Removal of the root sets it to null.

Source code in jsonpatchx/builtins.py
class RemoveOp(OperationSchema, Generic[T]):
    """RFC 6902 remove operation. Removal of the root sets it to null."""

    model_config = ConfigDict(
        title="Remove operation",
        json_schema_extra={
            "description": "RFC 6902 remove operation. Removal of the root sets it to null."
        },
    )

    op: Literal["remove"] = "remove"
    path: JSONPointer[T]

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        return self.path.remove(doc)

ReplaceOp

Bases: OperationSchema, Generic[T]

RFC 6902 replace operation.

Source code in jsonpatchx/builtins.py
class ReplaceOp(OperationSchema, Generic[T]):
    """RFC 6902 replace operation."""

    model_config = ConfigDict(
        title="Replace operation",
        json_schema_extra={"description": "RFC 6902 replace operation."},
    )

    op: Literal["replace"] = "replace"
    path: JSONPointer[T]
    value: T

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        doc = RemoveOp[T](path=self.path).apply(doc)
        return AddOp[T](path=self.path, value=self.value).apply(doc)

TestOp

Bases: OperationSchema, Generic[T]

RFC 6902 test operation.

Source code in jsonpatchx/builtins.py
class TestOp(OperationSchema, Generic[T]):
    """RFC 6902 test operation."""

    __test__ = False  # Suppress pytest warning

    model_config = ConfigDict(
        title="Test operation",
        json_schema_extra={"description": "RFC 6902 test operation."},
    )

    op: Literal["test"] = "test"
    path: JSONPointer[T]
    value: T

    @override
    def apply(self, doc: JSONValue) -> JSONValue:
        actual = self.path.get(doc)
        if actual != self.value:
            raise TestOpFailed(
                f"test at path {self.path!r} failed, got {actual!r} but expected {self.value!r}"
            )
        return doc