Validating optional field in marshmallow

Question:

I have a field in my model which is required in the resource body while creating the resource (POST) and is not passed in the request body(will be passed along with URL) while updating the resource (PUT).

I have created my marshmallow model:

class DummySchema():
   field_1 = fields.Str(required=True)
   field_2 = fields.Id()

If I leave field_1 to required=True, I get the following error on PUT :

{
    "message": {
        "field_1": [
            "Missing data for required field."
        ]
    }
}

I could leave it required in the model, but that would mean it would cause a problem in POST request.

Is there a way I could set the field as optional, but set it required for my POST request so that I can perform my validation?

Asked By: Foo Bar

||

Answers:

I think I should’ve read the documentation thoroughly before 🙁

I could set a field as partial, so when it’ll do validation marshmallow would skip the field

data, errors = DummySchema().load({'field_2': 42}, partial=('field_1',))

REF: https://marshmallow.readthedocs.io/en/2.x-line/quickstart.html#validation

Answered By: Foo Bar

If you want to use it for /POST request then this field can be added in dump_only list.
This can also be used for /PUT request.

class StrategySchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Strategy
        sqla_session = db.session
        ordered = True
        load_instance = True
        dump_only = (  # read-only
            "id", 
            "created_by",
            "created_by_id",
            "created_at",
            "updated_at",
        )

dump_only means:

  • Consider these fields only while dumping a model to json (deserialization)
  • ignore it while loading a model from json
  • read-only fields in other words
Answered By: Karishma Sukhwani
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.