|
1 | 1 | from collections import UserString |
2 | 2 | from enum import Enum |
3 | | -from enum import auto |
4 | 3 | from inspect import isclass |
5 | 4 | from typing import Annotated |
6 | 5 | from typing import Any |
|
29 | 28 | from typing_extensions import NewType |
30 | 29 | from typing_extensions import Self |
31 | 30 |
|
| 31 | +from scim2_models.context import Context |
32 | 32 | from scim2_models.utils import normalize_attribute_name |
33 | 33 | from scim2_models.utils import to_camel |
34 | 34 |
|
@@ -162,172 +162,6 @@ def _validate(cls, input_value: str, /) -> str: |
162 | 162 | return input_value |
163 | 163 |
|
164 | 164 |
|
165 | | -class Context(Enum): |
166 | | - """Represent the different HTTP contexts detailed in :rfc:`RFC7644 §3.2 <7644#section-3.2>`. |
167 | | -
|
168 | | - Contexts are intended to be used during model validation and serialization. |
169 | | - For instance a client preparing a resource creation POST request can use |
170 | | - :code:`resource.model_dump(Context.RESOURCE_CREATION_REQUEST)` and |
171 | | - the server can then validate it with |
172 | | - :code:`resource.model_validate(Context.RESOURCE_CREATION_REQUEST)`. |
173 | | - """ |
174 | | - |
175 | | - DEFAULT = auto() |
176 | | - """The default context. |
177 | | -
|
178 | | - All fields are accepted during validation, and all fields are |
179 | | - serialized during a dump. |
180 | | - """ |
181 | | - |
182 | | - RESOURCE_CREATION_REQUEST = auto() |
183 | | - """The resource creation request context. |
184 | | -
|
185 | | - Should be used for clients building a payload for a resource creation request, |
186 | | - and servers validating resource creation request payloads. |
187 | | -
|
188 | | - - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`. |
189 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError`: |
190 | | - - when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`, |
191 | | - - when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null. |
192 | | - """ |
193 | | - |
194 | | - RESOURCE_CREATION_RESPONSE = auto() |
195 | | - """The resource creation response context. |
196 | | -
|
197 | | - Should be used for servers building a payload for a resource |
198 | | - creation response, and clients validating resource creation response |
199 | | - payloads. |
200 | | -
|
201 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`; |
202 | | - - When used for serialization, it will: |
203 | | - - always dump attributes annotated with :attr:`~scim2_models.Returned.always`; |
204 | | - - never dump attributes annotated with :attr:`~scim2_models.Returned.never`; |
205 | | - - dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded; |
206 | | - - not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included. |
207 | | - """ |
208 | | - |
209 | | - RESOURCE_QUERY_REQUEST = auto() |
210 | | - """The resource query request context. |
211 | | -
|
212 | | - Should be used for clients building a payload for a resource query request, |
213 | | - and servers validating resource query request payloads. |
214 | | -
|
215 | | - - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`. |
216 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`. |
217 | | - """ |
218 | | - |
219 | | - RESOURCE_QUERY_RESPONSE = auto() |
220 | | - """The resource query response context. |
221 | | -
|
222 | | - Should be used for servers building a payload for a resource query |
223 | | - response, and clients validating resource query response payloads. |
224 | | -
|
225 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`; |
226 | | - - When used for serialization, it will: |
227 | | - - always dump attributes annotated with :attr:`~scim2_models.Returned.always`; |
228 | | - - never dump attributes annotated with :attr:`~scim2_models.Returned.never`; |
229 | | - - dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded; |
230 | | - - not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included. |
231 | | - """ |
232 | | - |
233 | | - RESOURCE_REPLACEMENT_REQUEST = auto() |
234 | | - """The resource replacement request context. |
235 | | -
|
236 | | - Should be used for clients building a payload for a resource replacement request, |
237 | | - and servers validating resource replacement request payloads. |
238 | | -
|
239 | | - - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`. |
240 | | - - When used for validation, it will ignore attributes annotated with :attr:`scim2_models.Mutability.read_only` and raise a :class:`~pydantic.ValidationError`: |
241 | | - - when finding attributes annotated with :attr:`~scim2_models.Mutability.immutable` different than :paramref:`~scim2_models.BaseModel.model_validate.original`: |
242 | | - - when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null. |
243 | | - """ |
244 | | - |
245 | | - RESOURCE_REPLACEMENT_RESPONSE = auto() |
246 | | - """The resource replacement response context. |
247 | | -
|
248 | | - Should be used for servers building a payload for a resource |
249 | | - replacement response, and clients validating resource query |
250 | | - replacement payloads. |
251 | | -
|
252 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`; |
253 | | - - When used for serialization, it will: |
254 | | - - always dump attributes annotated with :attr:`~scim2_models.Returned.always`; |
255 | | - - never dump attributes annotated with :attr:`~scim2_models.Returned.never`; |
256 | | - - dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded; |
257 | | - - not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included. |
258 | | - """ |
259 | | - |
260 | | - SEARCH_REQUEST = auto() |
261 | | - """The search request context. |
262 | | -
|
263 | | - Should be used for clients building a payload for a search request, |
264 | | - and servers validating search request payloads. |
265 | | -
|
266 | | - - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`. |
267 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`. |
268 | | - """ |
269 | | - |
270 | | - SEARCH_RESPONSE = auto() |
271 | | - """The resource query response context. |
272 | | -
|
273 | | - Should be used for servers building a payload for a search response, |
274 | | - and clients validating resource search payloads. |
275 | | -
|
276 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`; |
277 | | - - When used for serialization, it will: |
278 | | - - always dump attributes annotated with :attr:`~scim2_models.Returned.always`; |
279 | | - - never dump attributes annotated with :attr:`~scim2_models.Returned.never`; |
280 | | - - dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded; |
281 | | - - not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included. |
282 | | - """ |
283 | | - |
284 | | - RESOURCE_PATCH_REQUEST = auto() |
285 | | - """The resource patch request context. |
286 | | -
|
287 | | - Should be used for clients building a payload for a PATCH request, |
288 | | - and servers validating PATCH request payloads. |
289 | | -
|
290 | | - - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`. |
291 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError`: |
292 | | - - when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`, |
293 | | - - when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing or null. |
294 | | - """ |
295 | | - |
296 | | - RESOURCE_PATCH_RESPONSE = auto() |
297 | | - """The resource patch response context. |
298 | | -
|
299 | | - Should be used for servers building a payload for a PATCH response, |
300 | | - and clients validating patch response payloads. |
301 | | -
|
302 | | - - When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`; |
303 | | - - When used for serialization, it will: |
304 | | - - always dump attributes annotated with :attr:`~scim2_models.Returned.always`; |
305 | | - - never dump attributes annotated with :attr:`~scim2_models.Returned.never`; |
306 | | - - dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded; |
307 | | - - not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included. |
308 | | - """ |
309 | | - |
310 | | - @classmethod |
311 | | - def is_request(cls, ctx: "Context") -> bool: |
312 | | - return ctx in ( |
313 | | - cls.RESOURCE_CREATION_REQUEST, |
314 | | - cls.RESOURCE_QUERY_REQUEST, |
315 | | - cls.RESOURCE_REPLACEMENT_REQUEST, |
316 | | - cls.SEARCH_REQUEST, |
317 | | - cls.RESOURCE_PATCH_REQUEST, |
318 | | - ) |
319 | | - |
320 | | - @classmethod |
321 | | - def is_response(cls, ctx: "Context") -> bool: |
322 | | - return ctx in ( |
323 | | - cls.RESOURCE_CREATION_RESPONSE, |
324 | | - cls.RESOURCE_QUERY_RESPONSE, |
325 | | - cls.RESOURCE_REPLACEMENT_RESPONSE, |
326 | | - cls.SEARCH_RESPONSE, |
327 | | - cls.RESOURCE_PATCH_RESPONSE, |
328 | | - ) |
329 | | - |
330 | | - |
331 | 165 | class Mutability(str, Enum): |
332 | 166 | """A single keyword indicating the circumstances under which the value of the attribute can be (re)defined.""" |
333 | 167 |
|
|
0 commit comments