marshmallow-code / marshmallow-code/flask-smorest

Schema validation of mandatory fields on a blueprint with a HTTP PATCH method containing only modified fields

Open
#628 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
717
Forks
77
Avg merge
7h 49m
Merged PRs (30d)
3

Description

I have a simple CRUD class (based on `flask.views.MethodView`).
The Schema class is based on `marshmallow_sqlalchemy.SQLAlchemyAutoSchema`.

```python
@_bp.arguments(AccountGroupModelSchema)
@_bp.response(200, AccountGroupModelSchema)
def patch(self, updated, account_group_id):
"""Update an existing object."""
item = AccountGroup.query.get_or_404(account_group_id)

item.name = updated.name or item.name

db.session.commit()
return item
```

When submitting a HTTP PATCH, the JSON payload contains only those fields being modified, e.g. ```{ "name": "new_name" }```. This means the Schema validation fails, because mandatory fields are not present.

Is there anyway to handle validating the fields that are present AND rejecting any fields that are unknown but NOT failing validation for missing (as not being modified) fields in a PATCH? Or do I just have to take the JSON directly, update the entity, then validate prior to updated in the ORM?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the @_bp.arguments(AccountGroupModelSchema) decorator and trace how the PATCH payload is validated. Compare the requested partial-field behavior with existing framework behavior, documentation, or tests; done means present fields are validated, unknown fields are rejected, and omitted mandatory fields do not fail validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python, sqlalchemy
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.