Skip to content

Commit 3fa714c

Browse files
committed
feat: ensure PatchOp.path is not none for remove operations
1 parent 2b135d4 commit 3fa714c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

scim2_models/rfc7644/patch_op.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from pydantic import Field
77
from pydantic import field_validator
8+
from pydantic import model_validator
9+
from typing_extensions import Self
810

911
from ..base import ComplexAttribute
1012
from ..base import Required
@@ -32,6 +34,12 @@ class Op(str, Enum):
3234
"""The "path" attribute value is a String containing an attribute path
3335
describing the target of the operation."""
3436

37+
@model_validator(mode="after")
38+
def validate_path(self) -> Self:
39+
if self.path is None and self.op == PatchOperation.Op.remove:
40+
raise ValueError("Op.path is required for remove operations")
41+
return self
42+
3543
value: Optional[Any] = None
3644

3745
@field_validator("op", mode="before")

tests/test_patch_op.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,29 @@ def test_validate_patchop_case_insensitivith():
3535
"operations": [{"op": 42, "path": "userName", "value": "Rivard"}],
3636
},
3737
)
38+
39+
40+
def test_path_required_for_remove_operations():
41+
PatchOp.model_validate(
42+
{
43+
"operations": [
44+
{"op": "replace", "value": "foobar"},
45+
],
46+
}
47+
)
48+
PatchOp.model_validate(
49+
{
50+
"operations": [
51+
{"op": "add", "value": "foobar"},
52+
],
53+
}
54+
)
55+
56+
with pytest.raises(ValidationError):
57+
PatchOp.model_validate(
58+
{
59+
"operations": [
60+
{"op": "remove", "value": "foobar"},
61+
],
62+
}
63+
)

0 commit comments

Comments
 (0)