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
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Fixed
^^^^^
- Fix :attr:`~SearchRequest.start_index` and :attr:`~SearchRequest.count` limits. :issue:`84`
- :attr:`~ListResponse.total_resuls` is required.

[0.3.0] - 2024-12-11
--------------------
Expand Down
14 changes: 13 additions & 1 deletion scim2_models/rfc7644/list_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ class ListResponse(Message, Generic[AnyResource], metaclass=ListResponseMetaclas
def check_results_number(
cls, value: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
) -> Self:
""":rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>` indicates that 'resources' must be set if 'totalResults' is non-zero."""
"""Validate result numbers.

:rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>` indicates that:

- 'totalResults' is required
- 'resources' must be set if 'totalResults' is non-zero.
"""
obj = handler(value)

if (
Expand All @@ -114,6 +120,12 @@ def check_results_number(
):
return obj

if obj.total_results is None:
raise PydanticCustomError(
"required_error",
"Field 'total_results' is required but value is missing or null",
)

if obj.total_results > 0 and not obj.resources:
raise PydanticCustomError(
"no_resource_error",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_list_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,26 @@ def test_list_response_schema_ordering():
],
}
ListResponse[Union[User[EnterpriseUser], Group]].model_validate(payload)


def test_total_results_required():
"""ListResponse.total_results is required."""
payload = {
"Resources": [
{
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
],
"userName": "bjensen@example.com",
"id": "foobar",
}
],
}

with pytest.raises(
ValidationError,
match="Field 'total_results' is required but value is missing or null",
):
ListResponse[User].model_validate(
payload, scim_ctx=Context.RESOURCE_QUERY_RESPONSE
)