scim2-models 0.2.12__py3-none-any.whl → 0.3.1__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(
@@ -245,6 +245,12 @@ class Attribute(ComplexAttribute):
245
245
  return sub_attribute
246
246
  return None
247
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
+
248
254
 
249
255
  class Schema(Resource):
250
256
  schemas: Annotated[list[str], Required.true] = [
@@ -280,3 +286,9 @@ class Schema(Resource):
280
286
  if attribute.name == attribute_name:
281
287
  return attribute
282
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")
@@ -104,7 +104,13 @@ class ListResponse(Message, Generic[AnyResource], metaclass=ListResponseMetaclas
104
104
  def check_results_number(
105
105
  cls, value: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
106
106
  ) -> Self:
107
- """:rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>` indicates that 'resources' must be set if 'totalResults' is non-zero."""
107
+ """Validate result numbers.
108
+
109
+ :rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>` indicates that:
110
+
111
+ - 'totalResults' is required
112
+ - 'resources' must be set if 'totalResults' is non-zero.
113
+ """
108
114
  obj = handler(value)
109
115
 
110
116
  if (
@@ -114,6 +120,12 @@ class ListResponse(Message, Generic[AnyResource], metaclass=ListResponseMetaclas
114
120
  ):
115
121
  return obj
116
122
 
123
+ if obj.total_results is None:
124
+ raise PydanticCustomError(
125
+ "required_error",
126
+ "Field 'total_results' is required but value is missing or null",
127
+ )
128
+
117
129
  if obj.total_results > 0 and not obj.resources:
118
130
  raise PydanticCustomError(
119
131
  "no_resource_error",
@@ -49,11 +49,11 @@ class SearchRequest(Message):
49
49
  @field_validator("start_index")
50
50
  @classmethod
51
51
  def start_index_floor(cls, value: int) -> int:
52
- """According to :rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>, start_index values less than 0 are interpreted as 0.
52
+ """According to :rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>, start_index values less than 1 are interpreted as 1.
53
53
 
54
54
  A value less than 1 SHALL be interpreted as 1.
55
55
  """
56
- return None if value is None else max(0, value)
56
+ return None if value is None else max(1, value)
57
57
 
58
58
  count: Optional[int] = None
59
59
  """An integer indicating the desired maximum number of query results per
@@ -62,11 +62,11 @@ class SearchRequest(Message):
62
62
  @field_validator("count")
63
63
  @classmethod
64
64
  def count_floor(cls, value: int) -> int:
65
- """According to :rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>, count values less than 1 are interpreted as 1.
65
+ """According to :rfc:`RFC7644 §3.4.2 <7644#section-3.4.2.4>, count values less than 0 are interpreted as 0.
66
66
 
67
- A value less than 1 SHALL be interpreted as 1.
67
+ A negative value SHALL be interpreted as 0.
68
68
  """
69
- return None if value is None else max(1, value)
69
+ return None if value is None else max(0, value)
70
70
 
71
71
  @model_validator(mode="after")
72
72
  def attributes_validator(self):
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: scim2-models
3
- Version: 0.2.12
3
+ Version: 0.3.1
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
@@ -208,6 +208,7 @@ License: Apache License
208
208
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
209
209
  See the License for the specific language governing permissions and
210
210
  limitations under the License.
211
+ License-File: LICENSE
211
212
  Keywords: provisioning,pydantic,rfc7643,rfc7644,scim,scim2
212
213
  Classifier: Development Status :: 3 - Alpha
213
214
  Classifier: Environment :: Web Environment
@@ -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,17 +8,17 @@ 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=aC4wUNBdZ8USpvhCGyGQ97T40VhrL1Pl3VnB3zS1PfE,9929
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
15
15
  scim2_models/rfc7644/bulk.py,sha256=I6S40kyJPwDQwPFi668wFFDVTST7z6QTe9HTL5QUViI,2577
16
16
  scim2_models/rfc7644/error.py,sha256=l4-vtGQYm5u13-ParhbHSeqXEil0E09QXSO9twAT3SU,6185
17
- scim2_models/rfc7644/list_response.py,sha256=Zh8Od2iOd3kinwLP-PHQMEbfM1tdl111Qap9DR8DHc4,4251
17
+ scim2_models/rfc7644/list_response.py,sha256=ltjV1WII2fNy8j3RX8J0_Jb5DW4hTFoO6P-Myr7TnBY,4551
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
- scim2_models/rfc7644/search_request.py,sha256=X3DZQOtLu8EWYvkNfNOcFlGtx3TGNEkpL59dAdut7Sc,3006
21
- scim2_models-0.2.12.dist-info/METADATA,sha256=ifbETwkgl1cZNInG66HBGtJW4ANs_WpvtxZQ9__yAXE,16267
22
- scim2_models-0.2.12.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
23
- scim2_models-0.2.12.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
24
- scim2_models-0.2.12.dist-info/RECORD,,
20
+ scim2_models/rfc7644/search_request.py,sha256=DRGlixcWtYtbUuP9MT7PsnvyxlONLcXGEcQveWdqQng,3003
21
+ scim2_models-0.3.1.dist-info/METADATA,sha256=UMsrXnMlvGREPr8wUwJ_MSnlBDDvLOa3YBmDPlt8x_M,16288
22
+ scim2_models-0.3.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
23
+ scim2_models-0.3.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
24
+ scim2_models-0.3.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.26.3
2
+ Generator: hatchling 1.27.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any