scim2-models 0.2.11__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
scim2_models/base.py CHANGED
@@ -233,9 +233,9 @@ class Context(Enum):
233
233
  Should be used for clients building a payload for a resource replacement request,
234
234
  and servers validating resource replacement request payloads.
235
235
 
236
- - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only` and :attr:`~scim2_models.Mutability.immutable`.
236
+ - When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
237
237
  - When used for validation, it will ignore attributes annotated with :attr:`scim2_models.Mutability.read_only` and raise a :class:`~pydantic.ValidationError`:
238
- - when finding attributes annotated with :attr:`~scim2_models.Mutability.immutable`,
238
+ - when finding attributes annotated with :attr:`~scim2_models.Mutability.immutable` different than :paramref:`~scim2_models.BaseModel.model_validate.original`:
239
239
  - when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null.
240
240
  """
241
241
 
@@ -492,12 +492,6 @@ class BaseModel(PydanticBaseModel):
492
492
  ):
493
493
  raise exc
494
494
 
495
- if (
496
- context == Context.RESOURCE_REPLACEMENT_REQUEST
497
- and mutability == Mutability.immutable
498
- ):
499
- raise exc
500
-
501
495
  if (
502
496
  context
503
497
  in (Context.RESOURCE_CREATION_REQUEST, Context.RESOURCE_REPLACEMENT_REQUEST)
@@ -604,8 +598,55 @@ class BaseModel(PydanticBaseModel):
604
598
 
605
599
  return value
606
600
 
601
+ @model_validator(mode="wrap")
602
+ @classmethod
603
+ def check_replacement_request_mutability(
604
+ cls, value: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
605
+ ) -> Self:
606
+ """Check if 'immutable' attributes have been mutated in replacement requests."""
607
+ from scim2_models.rfc7643.resource import Resource
608
+
609
+ value = handler(value)
610
+
611
+ context = info.context.get("scim") if info.context else None
612
+ original = info.context.get("original") if info.context else None
613
+ if (
614
+ context == Context.RESOURCE_REPLACEMENT_REQUEST
615
+ and issubclass(cls, Resource)
616
+ and original is not None
617
+ ):
618
+ cls.check_mutability_issues(original, value)
619
+ return value
620
+
621
+ @classmethod
622
+ def check_mutability_issues(cls, original: "BaseModel", replacement: "BaseModel"):
623
+ """Compare two instances, and check for differences of values on the fields marked as immutable."""
624
+ model = replacement.__class__
625
+ for field_name in model.model_fields:
626
+ mutability = model.get_field_annotation(field_name, Mutability)
627
+ if mutability == Mutability.immutable and getattr(
628
+ original, field_name
629
+ ) != getattr(replacement, field_name):
630
+ raise PydanticCustomError(
631
+ "mutability_error",
632
+ "Field '{field_name}' is immutable but the request value is different than the original value.",
633
+ {"field_name": field_name},
634
+ )
635
+
636
+ attr_type = model.get_field_root_type(field_name)
637
+ if is_complex_attribute(attr_type) and not model.get_field_multiplicity(
638
+ field_name
639
+ ):
640
+ original_val = getattr(original, field_name)
641
+ replacement_value = getattr(replacement, field_name)
642
+ if original_val is not None and replacement_value is not None:
643
+ cls.check_mutability_issues(original_val, replacement_value)
644
+
607
645
  def mark_with_schema(self):
608
- """Navigate through attributes and sub-attributes of type ComplexAttribute, and mark them with a '_schema' attribute. '_schema' will later be used by 'get_attribute_urn'."""
646
+ """Navigate through attributes and sub-attributes of type ComplexAttribute, and mark them with a '_schema' attribute.
647
+
648
+ '_schema' will later be used by 'get_attribute_urn'.
649
+ """
609
650
  from scim2_models.rfc7643.resource import Resource
610
651
 
611
652
  for field_name in self.model_fields:
@@ -653,7 +694,8 @@ class BaseModel(PydanticBaseModel):
653
694
  scim_ctx = info.context.get("scim") if info.context else None
654
695
 
655
696
  if (
656
- scim_ctx == Context.RESOURCE_CREATION_REQUEST
697
+ scim_ctx
698
+ in (Context.RESOURCE_CREATION_REQUEST, Context.RESOURCE_REPLACEMENT_REQUEST)
657
699
  and mutability == Mutability.read_only
658
700
  ):
659
701
  return None
@@ -668,12 +710,6 @@ class BaseModel(PydanticBaseModel):
668
710
  ):
669
711
  return None
670
712
 
671
- if scim_ctx == Context.RESOURCE_REPLACEMENT_REQUEST and mutability in (
672
- Mutability.immutable,
673
- Mutability.read_only,
674
- ):
675
- return None
676
-
677
713
  return value
678
714
 
679
715
  def scim_response_serializer(self, value: Any, info: SerializationInfo) -> Any:
@@ -719,10 +755,28 @@ class BaseModel(PydanticBaseModel):
719
755
 
720
756
  @classmethod
721
757
  def model_validate(
722
- cls, *args, scim_ctx: Optional[Context] = Context.DEFAULT, **kwargs
758
+ cls,
759
+ *args,
760
+ scim_ctx: Optional[Context] = Context.DEFAULT,
761
+ original: Optional["BaseModel"] = None,
762
+ **kwargs,
723
763
  ) -> Self:
724
- """Validate SCIM payloads and generate model representation by using Pydantic :code:`BaseModel.model_validate`."""
725
- kwargs.setdefault("context", {}).setdefault("scim", scim_ctx)
764
+ """Validate SCIM payloads and generate model representation by using Pydantic :code:`BaseModel.model_validate`.
765
+
766
+ :param scim_ctx: The SCIM :class:`~scim2_models.Context` in which the validation happens.
767
+ :param original: If this parameter is set during :attr:`~Context.RESOURCE_REPLACEMENT_REQUEST`,
768
+ :attr:`~scim2_models.Mutability.immutable` parameters will be compared against the *original* model value.
769
+ An exception is raised if values are different.
770
+ """
771
+ context = kwargs.setdefault("context", {})
772
+ context.setdefault("scim", scim_ctx)
773
+ context.setdefault("original", original)
774
+
775
+ if scim_ctx == Context.RESOURCE_REPLACEMENT_REQUEST and original is None:
776
+ raise ValueError(
777
+ "Resource queries replacement validation must compare to an original resource"
778
+ )
779
+
726
780
  return super().model_validate(*args, **kwargs)
727
781
 
728
782
  def _prepare_model_dump(
@@ -238,6 +238,19 @@ class Attribute(ComplexAttribute):
238
238
 
239
239
  return annotation, field
240
240
 
241
+ def get_attribute(self, attribute_name: str) -> Optional["Attribute"]:
242
+ """Find an attribute by its name."""
243
+ for sub_attribute in self.sub_attributes or []:
244
+ if sub_attribute.name == attribute_name:
245
+ return sub_attribute
246
+ return None
247
+
248
+ def __getitem__(self, name):
249
+ """Find an attribute by its name."""
250
+ if attribute := self.get_attribute(name):
251
+ return attribute
252
+ raise KeyError(f"This attribute has no '{name}' sub-attribute")
253
+
241
254
 
242
255
  class Schema(Resource):
243
256
  schemas: Annotated[list[str], Required.true] = [
@@ -273,3 +286,9 @@ class Schema(Resource):
273
286
  if attribute.name == attribute_name:
274
287
  return attribute
275
288
  return None
289
+
290
+ def __getitem__(self, name):
291
+ """Find an attribute by its name."""
292
+ if attribute := self.get_attribute(name):
293
+ return attribute
294
+ raise KeyError(f"This schema has no '{name}' attribute")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: scim2-models
3
- Version: 0.2.11
3
+ Version: 0.3.0
4
4
  Summary: SCIM2 models serialization and validation with pydantic
5
5
  Project-URL: documentation, https://scim2-models.readthedocs.io
6
6
  Project-URL: repository, https://github.com/python-scim/scim2-models
@@ -1,5 +1,5 @@
1
1
  scim2_models/__init__.py,sha256=Y06vTA_51lXfv9zk_dTzyIIqEo1H2bcencvMM5KAwn8,3063
2
- scim2_models/base.py,sha256=_RTwLpwyiyKTc7VYuB854yuPhr8gakahmrOFTKTxHFI,32268
2
+ scim2_models/base.py,sha256=wLifcxRSuozJyemD4lAvlePh-qrkGzxE3Uck9xfx20M,34819
3
3
  scim2_models/constants.py,sha256=SuMGFtVNletdV5ZJRUcIq7o2CqZCRvOurnIdLE_cakE,540
4
4
  scim2_models/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  scim2_models/utils.py,sha256=MzZz212-lkVWgXcpXvNwoi_u28wBTpkTwPrfYC5v92A,2771
@@ -8,7 +8,7 @@ scim2_models/rfc7643/enterprise_user.py,sha256=EaxdHH2dcBrwWwGpaZC6iZ9dbcaVN1Npo
8
8
  scim2_models/rfc7643/group.py,sha256=CBmSnlzd4JktCpOmg4XkfJJUd8oD6KbYFRTt0dSdixE,1302
9
9
  scim2_models/rfc7643/resource.py,sha256=zMyk02Traoq6XNBcS_fSnXIOadZwxTdA9CV4vLuy38k,12648
10
10
  scim2_models/rfc7643/resource_type.py,sha256=p8IKV5IakPMBX6d2Fv2MFqaQL5iTw152vmozVVSC8gU,3196
11
- scim2_models/rfc7643/schema.py,sha256=tYKxQP791Vm58xjgaIyQmRF8WP04EPkJrP_OtzusHXA,9642
11
+ scim2_models/rfc7643/schema.py,sha256=m_iBK2ggQyE3BoPCQVWy0HM2fD3dIix8Ki72nVIDv4M,10382
12
12
  scim2_models/rfc7643/service_provider_config.py,sha256=deMNCXlqiNzuLcVRN9mdHiTUxhczDnvi-oO6k-Anj8U,5402
13
13
  scim2_models/rfc7643/user.py,sha256=-wlO2IC0MxFWUJpaddEwAG5LsEcORnbHfkRwGG3fVSk,9942
14
14
  scim2_models/rfc7644/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -18,7 +18,7 @@ scim2_models/rfc7644/list_response.py,sha256=Zh8Od2iOd3kinwLP-PHQMEbfM1tdl111Qap
18
18
  scim2_models/rfc7644/message.py,sha256=F4kPqbHAka3-wZzap9a45noQZw-o1vznTJypNABBF7w,253
19
19
  scim2_models/rfc7644/patch_op.py,sha256=OE-ixDanTkY5zQP7EK7OAp88uE_fMk03mqmaZHxgJ-g,2210
20
20
  scim2_models/rfc7644/search_request.py,sha256=X3DZQOtLu8EWYvkNfNOcFlGtx3TGNEkpL59dAdut7Sc,3006
21
- scim2_models-0.2.11.dist-info/METADATA,sha256=mfd5sP_S-WqTDypsNmGp8e0GvJLbzNM9S8Dpi4hL4_g,16267
22
- scim2_models-0.2.11.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
23
- scim2_models-0.2.11.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
24
- scim2_models-0.2.11.dist-info/RECORD,,
21
+ scim2_models-0.3.0.dist-info/METADATA,sha256=x9_m3LiXvIdc960TZyPyhlnq4KaEXwfEaZOfXlQByEc,16266
22
+ scim2_models-0.3.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
23
+ scim2_models-0.3.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
24
+ scim2_models-0.3.0.dist-info/RECORD,,