Skip to content

Commit 5f41c7b

Browse files
authored
Merge pull request #81 from yaal-coop/issue-26-schema-id-uris
schema ids must be URIs
2 parents db3a573 + 21b907b commit 5f41c7b

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Added
1212
Changed
1313
^^^^^^^
1414
- :attr:`SearchRequest.attributes <scim2_models.SearchRequest.attributes>` and :attr:`SearchRequest.attributes <scim2_models.SearchRequest.excluded_attributes>` are mutually exclusive. #19
15+
- :class:`~scim2_models.Schema` ids must be valid URIs. #26
1516

1617
[0.2.2] - 2024-09-20
1718
--------------------

scim2_models/rfc7643/schema.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
from pydantic import Base64Bytes
1515
from pydantic import Field
1616
from pydantic import create_model
17+
from pydantic import field_validator
1718
from pydantic.alias_generators import to_pascal
1819
from pydantic.alias_generators import to_snake
20+
from pydantic_core import Url
1921

2022
from ..base import CaseExact
2123
from ..base import ComplexAttribute
@@ -63,7 +65,10 @@ def make_python_model(
6365
for attr in (obj.attributes or [])
6466
if attr.name
6567
}
66-
pydantic_attributes["schemas"] = (Optional[List[str]], Field(default=[obj.id]))
68+
pydantic_attributes["schemas"] = (
69+
Optional[List[str]],
70+
Field(default=[obj.id]),
71+
)
6772

6873
model_name = to_pascal(to_snake(obj.name))
6974
model = create_model(model_name, __base__=base, **pydantic_attributes)
@@ -254,3 +259,10 @@ class Schema(Resource):
254259
] = None
255260
"""A complex type that defines service provider attributes and their
256261
qualities via the following set of sub-attributes."""
262+
263+
@field_validator("id")
264+
@classmethod
265+
def urn_id(cls, value: str) -> str:
266+
"""Schema ids are URI, as defined in RFC7643 §7."""
267+
268+
return str(Url(value))

tests/test_schema.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
14
from scim2_models import Attribute
25
from scim2_models import Mutability
36
from scim2_models import Returned
@@ -72,3 +75,14 @@ def test_group_schema(load_sample):
7275
)
7376

7477
assert obj.model_dump(exclude_unset=True) == payload
78+
79+
80+
def test_uri_ids():
81+
"""Test that schema ids are URI, as defined in RFC7643 §7.
82+
83+
https://datatracker.ietf.org/doc/html/rfc7643#section-7
84+
"""
85+
86+
Schema(id="urn:ietf:params:scim:schemas:extension:enterprise:2.0:User")
87+
with pytest.raises(ValidationError):
88+
Schema(id="invalid\nuri")

0 commit comments

Comments
 (0)