You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The request object is available as context for Schema validation of the incoming request. But there are cases where you would need more context based on the request to make the validation.
For example on of such cases is a PUT or PATCH request where you need access to the instance that is being updated. Incoming data validation happens so early that I have not found a way of adding extra context currently.
What I currently do is get the object ID from the context -> request.resolver_match.kwargs and fetch the object during validation.
@model_validator(mode="after")defvalidate_no_overlapping_leaves(self, info: ValidationInfo) ->"LeaveUpdateSchema":
# TODO: see if path_params could be added to context in Django ninja# https://github.com/vitalik/django-ninja/blob/master/ninja/params/models.py#L68path_params=info.context["request"].resolver_match.kwargsinstance=Leave.objects.filter(id=path_params["leave_id"]).first()
ifinstance:
# Check if there are any overlapping leaves for the same employee, excluding the current leavereturnself
I found that when the request is attached to the context in ParamModel resolve actually path_params is available. It's just not passed as context. It would be a minimal change but allow additonal context data for the validators.
I would be happy to prepare a PR for this if the idea finds support.
The text was updated successfully, but these errors were encountered:
The request object is available as context for Schema validation of the incoming request. But there are cases where you would need more context based on the request to make the validation.
For example on of such cases is a PUT or PATCH request where you need access to the instance that is being updated. Incoming data validation happens so early that I have not found a way of adding extra context currently.
What I currently do is get the object ID from the context -> request.resolver_match.kwargs and fetch the object during validation.
I found that when the request is attached to the context in ParamModel resolve actually path_params is available. It's just not passed as context. It would be a minimal change but allow additonal context data for the validators.
I would be happy to prepare a PR for this if the idea finds support.
The text was updated successfully, but these errors were encountered: