marshmallow-code / marshmallow-code/flask-smorest
Schema validation of mandatory fields on a blueprint with a HTTP PATCH method containing only modified fields
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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