SCIM2 Models
scim2-models is a Python library providing Pydantic models for SCIM (System for Cross-domain Identity Management) schemas, specifically RFC7643 and RFC7644. It facilitates the serialization and validation of SCIM2 payloads using native Python objects. The library includes features like context-aware validation and dynamic schema extensions, serving as a foundational block for building SCIM2 servers and clients. The current version is 0.6.12, and it is actively maintained with frequent releases.
Common errors
-
pydantic.ValidationError: ... field required (type=value_error.missing)
cause The input JSON payload is missing a required attribute as defined by the SCIM schema (RFC7643/7644) or a custom schema extension. This is a common Pydantic validation error when data does not conform.fixReview the SCIM RFCs (e.g., RFC7643 Section 2 for core resources) or your custom schema definitions to identify all mandatory attributes. Ensure the JSON payload includes all required fields with correct data types. -
AttributeError: type object 'User' has no attribute 'schemas'
cause This error typically occurs if you've upgraded from `scim2-models <0.6.0` to `0.6.0` or later and your code or custom models relied on accessing a `schemas` attribute directly from a resource class. The `schemas` field was replaced by the `__schema__` class variable.fixIf you were accessing a class-level `schemas` attribute, update your code to use the `__schema__` class variable directly or instantiate the model and access the `schemas` field from the instance (which is dynamically populated based on `__schema__`). For custom models, ensure `__schema__` is defined as a class variable.
Warnings
- breaking In version 0.6.0, the mechanism for defining schema URNs on custom SCIM resource models changed. Resources now define their schema URN with a `__schema__` class variable instead of a `schemas` default value.
- gotcha The library is officially marked as '3 - Alpha' status on PyPI. This indicates that the API might still be unstable, and backward-incompatible changes could occur in minor versions without strict adherence to semantic versioning until a stable '1.0' release.
- gotcha The library supports context-aware validation (e.g., for creation, query, or replacement operations). This means that validation behavior can differ based on the `context` parameter provided to validation methods. An attribute that is optional during a PUT (replacement) might be required during a POST (creation).
Install
-
pip install scim2-models
Imports
- User
from scim2_models.resources import User
- Group
from scim2_models.resources import Group
- Schema
from scim2_models.schemas import Schema
Quickstart
from scim2_models.resources import User
from pydantic import ValidationError
user_data = {
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
"userName": "bjensen@example.com",
"id": "2819c223-7f76-453a-919d-413861904646",
"name": {
"givenName": "Babs",
"familyName": "Jensen"
},
"emails": [
{
"value": "bjensen@example.com",
"type": "work",
"primary": True
}
],
"meta": {
"resourceType": "User",
"created": "2024-04-13T12:00:00Z",
"lastModified": "2024-04-13T12:00:00Z",
"location": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646"
}
}
try:
user = User.model_validate(user_data)
print(f"Successfully validated user: {user.user_name}")
print(f"User ID: {user.id}")
except ValidationError as e:
print(f"Validation error: {e}")
# You can also create a user directly
new_user = User(
userName="testuser",
emails=[{"value": "test@example.com", "type": "work", "primary": True}]
)
print(f"Created new user: {new_user.model_dump_json(indent=2)}")