Skip to content

Commit

Permalink
fix: schema ids must be URIs
Browse files Browse the repository at this point in the history
as defined by RFC763 §7

   id
      The unique URI of the schema.  When applicable, service providers
      MUST specify the URI, e.g.,
      "urn:ietf:params:scim:schemas:core:2.0:User".  Unlike most other
      schemas, which use some sort of Globally Unique Identifier (GUID)
      for the "id", the schema "id" is a URI so that it can be
      registered and is portable between different service providers and
      clients.  REQUIRED.
  • Loading branch information
azmeuk committed Oct 14, 2024
1 parent db3a573 commit 21b907b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Added
Changed
^^^^^^^
- :attr:`SearchRequest.attributes <scim2_models.SearchRequest.attributes>` and :attr:`SearchRequest.attributes <scim2_models.SearchRequest.excluded_attributes>` are mutually exclusive. #19
- :class:`~scim2_models.Schema` ids must be valid URIs. #26

[0.2.2] - 2024-09-20
--------------------
Expand Down
14 changes: 13 additions & 1 deletion scim2_models/rfc7643/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
from pydantic import Base64Bytes
from pydantic import Field
from pydantic import create_model
from pydantic import field_validator
from pydantic.alias_generators import to_pascal
from pydantic.alias_generators import to_snake
from pydantic_core import Url

from ..base import CaseExact
from ..base import ComplexAttribute
Expand Down Expand Up @@ -63,7 +65,10 @@ def make_python_model(
for attr in (obj.attributes or [])
if attr.name
}
pydantic_attributes["schemas"] = (Optional[List[str]], Field(default=[obj.id]))
pydantic_attributes["schemas"] = (
Optional[List[str]],
Field(default=[obj.id]),
)

model_name = to_pascal(to_snake(obj.name))
model = create_model(model_name, __base__=base, **pydantic_attributes)
Expand Down Expand Up @@ -254,3 +259,10 @@ class Schema(Resource):
] = None
"""A complex type that defines service provider attributes and their
qualities via the following set of sub-attributes."""

@field_validator("id")
@classmethod
def urn_id(cls, value: str) -> str:
"""Schema ids are URI, as defined in RFC7643 §7."""

return str(Url(value))
14 changes: 14 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from pydantic import ValidationError

from scim2_models import Attribute
from scim2_models import Mutability
from scim2_models import Returned
Expand Down Expand Up @@ -72,3 +75,14 @@ def test_group_schema(load_sample):
)

assert obj.model_dump(exclude_unset=True) == payload


def test_uri_ids():
"""Test that schema ids are URI, as defined in RFC7643 §7.
https://datatracker.ietf.org/doc/html/rfc7643#section-7
"""

Schema(id="urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
with pytest.raises(ValidationError):
Schema(id="invalid\nuri")

0 comments on commit 21b907b

Please sign in to comment.