Skip to content

Commit cb70b08

Browse files
authored
Merge pull request #79 from yaal-coop/issue-19-search-request
fix: SearchRequest 'attributes' and 'excluded_attributes' are mutually exclusive.
2 parents f00ca07 + c446015 commit cb70b08

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

doc/changelog.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
[0.2.3] - Unreleased
5+
--------------------
6+
7+
Changed
8+
^^^^^^^
9+
- :attr:`SearchRequest.attributes <scim2_models.SearchRequest.attributes>` and :attr:`SearchRequest.attributes <scim2_models.SearchRequest.excluded_attributes>` are mutually exclusive. #19
10+
411
[0.2.2] - 2024-09-20
512
--------------------
613

@@ -16,7 +23,6 @@ Fixed
1623
^^^^^
1724
- :attr:`~scim2_models.Resource.external_id` is :data:`scim2_models.CaseExact.true`. #74
1825

19-
2026
[0.2.0] - 2024-08-18
2127
--------------------
2228

scim2_models/rfc7644/search_request.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional
44

55
from pydantic import field_validator
6+
from pydantic import model_validator
67

78
from .message import Message
89

@@ -64,3 +65,12 @@ def count_floor(cls, value: int) -> int:
6465
"""
6566

6667
return None if value is None else max(1, value)
68+
69+
@model_validator(mode="after")
70+
def attributes_validator(self):
71+
if self.attributes and self.excluded_attributes:
72+
raise ValueError(
73+
"'attributes' and 'excluded_attributes' are mutually exclusive"
74+
)
75+
76+
return self

tests/test_search_request.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
14
from scim2_models.rfc7644.search_request import SearchRequest
25

36

47
def test_search_request():
58
SearchRequest(
69
attributes=["userName", "displayName"],
10+
filter='userName Eq "john"',
11+
sort_by="userName",
12+
sort_order=SearchRequest.SortOrder.ascending,
13+
start_index=1,
14+
count=10,
15+
)
16+
17+
SearchRequest(
718
excluded_attributes=["timezone", "phoneNumbers"],
819
filter='userName Eq "john"',
920
sort_by="userName",
@@ -42,3 +53,24 @@ def test_count_floor():
4253

4354
sr = SearchRequest(count=-1)
4455
assert sr.count == 1
56+
57+
58+
def test_attributes_or_excluded_attributes():
59+
"""Test that a validation error is raised when both 'attributes' and 'excludedAttributes' are filled at the same time.
60+
https://datatracker.ietf.org/doc/html/rfc7644#section-3.9
61+
62+
Clients MAY request a partial resource representation on any
63+
operation that returns a resource within the response by specifying
64+
either of the mutually exclusive URL query parameters "attributes" or
65+
"excludedAttributes"...
66+
"""
67+
68+
payload = {
69+
"schemas": ["urn:ietf:params:scim:api:messages:2.0:SearchRequest"],
70+
"attributes": ["userName"],
71+
"excludedAttributes": [
72+
"displayName",
73+
],
74+
}
75+
with pytest.raises(ValidationError):
76+
SearchRequest.model_validate(payload)

0 commit comments

Comments
 (0)