Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions scim2_models/rfc7644/patch_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from pydantic import Field
from pydantic import field_validator
from pydantic import model_validator
from typing_extensions import Self

from ..base import ComplexAttribute
from ..base import Required
Expand Down Expand Up @@ -32,6 +34,18 @@ class Op(str, Enum):
"""The "path" attribute value is a String containing an attribute path
describing the target of the operation."""

@model_validator(mode="after")
def validate_path(self) -> Self:
# The "path" attribute value is a String containing an attribute path
# describing the target of the operation. The "path" attribute is
# OPTIONAL for "add" and "replace" and is REQUIRED for "remove"
# operations. See relevant operation sections below for details.

if self.path is None and self.op == PatchOperation.Op.remove:
raise ValueError("Op.path is required for remove operations")

return self

value: Optional[Any] = None

@field_validator("op", mode="before")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_patch_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,29 @@ def test_validate_patchop_case_insensitivith():
"operations": [{"op": 42, "path": "userName", "value": "Rivard"}],
},
)


def test_path_required_for_remove_operations():
PatchOp.model_validate(
{
"operations": [
{"op": "replace", "value": "foobar"},
],
}
)
PatchOp.model_validate(
{
"operations": [
{"op": "add", "value": "foobar"},
],
}
)

with pytest.raises(ValidationError):
PatchOp.model_validate(
{
"operations": [
{"op": "remove", "value": "foobar"},
],
}
)
Loading