Skip to content

Commit

Permalink
feat: implement Schema.get_attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Dec 8, 2024
1 parent eb67018 commit 1ea045f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

[0.2.11] - Unreleased
---------------------
Added
^^^^^
- Implement :meth:`Schema.get_attribute <scim2_models.Schema.get_attribute>`.

[0.2.10] - 2024-12-02
---------------------

Expand Down
7 changes: 7 additions & 0 deletions scim2_models/rfc7643/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,10 @@ class Schema(Resource):
def urn_id(cls, value: str) -> str:
"""Ensure that schema ids are URI, as defined in RFC7643 §7."""
return str(Url(value))

def get_attribute(self, attribute_name: str) -> Optional[Attribute]:
"""Find an attribute by its name."""
for attribute in self.attributes or []:
if attribute.name == attribute_name:
return attribute
return None
8 changes: 8 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def test_uri_ids():
Schema(id="urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
with pytest.raises(ValidationError):
Schema(id="invalid\nuri")


def test_get_attribute(load_sample):
"""Test the Schema.get_attribute method."""
payload = load_sample("rfc7643-8.7.1-schema-user.json")
schema = Schema.model_validate(payload)
assert schema.get_attribute("invalid") is None
assert isinstance(schema.get_attribute("userName"), Attribute)

0 comments on commit 1ea045f

Please sign in to comment.