Skip to content

Add PatchSchema #1450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add PatchSchema #1450

wants to merge 1 commit into from

Conversation

adriaanwm
Copy link
Contributor

Disclaimer: The code in this PR was written entirely with AI. Based on my tests it is working. However, I am not very familiar with Pydantic.

Suppose you have the following schema:

class MyProfileIn(Schema):
    bio: str
    email: Optional[str]

If you use PatchDict[MyProfileIn], then the following patch body passes validation:

{ "bio": null, "email": "[email protected]" }

This should return a 422, because if bio is included, it should be a string. This will cause issues when updating models where non-nullable fields are being set to null.

For this reason I made PatchSchema (PatchSchema[MyProfileIn]) which would result in the following:

{ "bio": null, "email": "[email protected]" }  # 422
{ "email": null }  # 200
{ "bio": "Example" }  # 200
{ "bio": "Example", "email": null }  # 200

@vitalik
Copy link
Owner

vitalik commented Apr 18, 2025

@adriaanwm - we already have PatchDict

@vitalik vitalik closed this Apr 18, 2025
@adriaanwm
Copy link
Contributor Author

adriaanwm commented Apr 22, 2025

@vitalik PatchDict allows null to be provided for a field where null is not a valid value.

if you have

class MyProfileIn(Schema):
    bio: str

then PatchDict[MyProfileIn] means that this payload passes validation:

{ 'bio': null }

Ideally this should return a 422 because null is not a valid value for bio (if it is included, it should be a string). This is why I made PatchSchema.

@vitalik
Copy link
Owner

vitalik commented Apr 22, 2025

I would say this is questinoable/undefinded behaviour - sending { 'bio': null } via PATCH request clearly tells that client wants to set some value as None

anyway - I would look into extending PatchDict with some option instead of introducing class with very similar name and a bit different behaviour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants