question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema Validation on Patch

See original GitHub issue

So I was trying t implement a simple Patch use case but its proving to be a difficult affair. As Patch allows for partial submission of schema fields I tried the obvious solution and added the partial=True argument to my schema declaration.

@blp.arguments(UserInputSchema(partial=True))

This however leads to a problem related to the marshmallow model schema class, since it immediately tries to create a model instance from the passed object.

Am I missing something or does patch just not play well with the intended behavior of marshmallow’s ModelSchema?

Thanks for the help in advance.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lafrechcommented, Mar 21, 2019

Patching is indeed complicated stuff.

What you’re trying to do is not a real patch but a put-like patch. It does not allow deleting a field, for instance.

A real patch would require something like json-patch. Search for json-patch on the Internet to get a better idea of what I’m talking about. I don’t do that and I have no recommendation.

This said, I think a lot of people live happily with put-like patches, which are much easier to achieve. I don’t do that either, I only use PUT.

This however leads to a problem related to the marshmallow model schema class, since it immediately tries to create a model instance from the passed object.

I think this is due to your schema. You must have some post_load decorator instantiating a model class, right?

I generally don’t to that. The schema deserializes the data and passes it to the view function as a dict. Then the view function uses it to update the data.

I think passing partial=True should do what you want.

@blp.route(.../<item_id>)
class UserInputById(MethodView):
    @blp.arguments(UserInputSchema(partial=True))
    @blp.response(UserInputSchema)
    def patch(self, new_data, user_input_id):
        # 1/ Get item object (using ORM/ODM)
        user_input = UserInput.get(user_input_id)
        # At this stage, new_data is a dict, user_input is an object
        # 2/ Update item with new_data
        # I don't have a generic method for that.
        # I suppose it depends on the application, the ORM/ODM, etc.
        # Doing it field by field can be verbose.
        [...]
        # 3/ Save item
        user_input.save()
        # 3/ Return item
        return user_input

Hope this helps.

0reactions
lafrechcommented, Sep 12, 2021

You shouldn’t have to override init. There won’t be any 1 appended if you only ever call the Edit schema with partial=True.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to validate http PATCH data with a JSON schema having ...
I'm trying to validate a classic JSON schema (with Ajv and json-server) with required fields, but for a HTTP PATCH request.
Read more >
Validating JSON Patch Requests - Medium
In a nutshell, these steps check that the object being modified by the JSON Patch is modified only in ways allowed by the...
Read more >
validating JSON body for PATCH request - MuleSoft Help Center
I need a validation to get 400 bad request if we give wrong key name. Right now I am validating the JSON schema...
Read more >
Modify Schema Validation — MongoDB Manual
You can modify all components of a schema validation, including its rules, validation level, and validation action. If you update a collection's validation ......
Read more >
Re: JSON Schema, Partial Updates, Subschema Validation
There is no way to validate that patch's data on its own. If keyPair1/keyPair2 are currently both strings, then it's invalid. But if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found