-
-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Currently if you want to validate or modify models according to some Context, you have to use either model_validate() or model_dump().
But let's say I have a User model (var user) I want to pass to a POST request. If I want to include contexts, I need to do user.model_dump(scim_ctx=Context.RESOURCE_CREATION_REQUEST). However, I want to pass the User model itself, not a dictionary, so I have to convert it back with User.model_validate(user.model_dump(scim_ctx=Context.RESOURCE_CREATION_REQUEST)). Now in the validation we can pass a second context like User.model_validate(user.model_dump(scim_ctx=Context.RESOURCE_CREATION_REQUEST) scim_ctx=Context.RESOURCE_CREATION_REQUEST).
I'm not sure if the last double context is necessary (edit: it is), but doing context checks becomes really annoying if I don't want to convert the model itself.
Suggestion
Some kind of helper methods for resources to just check the context stuff.
For example:
# Creates a new User according to context (in this case ignores read_only fields)
new_user: User = user.context(scim_ctx=Context.RESOURCE_CREATION_REQUEST)
# Validate if new user fits for POST (raise or return error if invalid, otherwise nothing)
new_user.context_validate(scim_ctx=Context.RESOURCE_CREATION_REQUEST)
Actually combining these into one function is even better
# Validate user model and return a new one
# (Error if missing required fields + ignore read_only)
new_user: User = user.context_validate(scim_ctx=Context.RESOURCE_CREATION_REQUEST)
# OR same thing but modify the existing one (e.g. read_only fields to None)
user.context_validate(scim_ctx=Context.RESOURCE_CREATION_REQUEST)