Source code for scim2_models.messages.response_parameters

from typing import Any
from typing import Generic

from pydantic import field_validator
from pydantic import model_validator

from ..base import BaseModel
from ..exceptions import InvalidPathException
from ..path import Path
from ..path.path import ResourceT


[docs] class ResponseParameters(BaseModel, Generic[ResourceT]): """:rfc:`RFC7644 §3.9 <7644#section-3.9>` ``attributes`` and ``excludedAttributes`` query parameters. Parameterising them with the resource type an endpoint serves, as in ``ResponseParameters[User]``, resolves each path against that model, so a server can tell which attribute a client asked for. An endpoint covering several types takes a union of them. """ attributes: list[Path[ResourceT]] | None = None """A multi-valued list of strings indicating the names of resource attributes to return in the response, overriding the set of attributes that would be returned by default.""" excluded_attributes: list[Path[ResourceT]] | None = None """A multi-valued list of strings indicating the names of resource attributes to be removed from the default set of attributes to return.""" @field_validator("attributes", "excluded_attributes", mode="before") @classmethod def _split_comma_separated(cls, value: Any) -> Any: """Split comma-separated strings into lists. RFC7644 §3.9 defines these as comma-separated query parameter values. """ if isinstance(value, str): return [v.strip() for v in value.split(",") if v.strip()] if isinstance(value, list) and len(value) == 1 and isinstance(value[0], str): return [v.strip() for v in value[0].split(",") if v.strip()] return value @field_validator("attributes", "excluded_attributes") @classmethod def _entries_name_an_attribute(cls, value: Any) -> Any: """Refuse an entry naming the values an attribute holds. RFC7644 §3.9 requires these in the attribute notation of §3.10. """ for path in value or (): try: path._check_attribute_notation() except InvalidPathException as exc: raise exc.as_pydantic_error() from exc return value @model_validator(mode="after") def _attributes_validator(self) -> "ResponseParameters[ResourceT]": if self.attributes and self.excluded_attributes: raise ValueError( "'attributes' and 'excluded_attributes' are mutually exclusive" ) return self