marshmallow-code / marshmallow-code/marshmallow

Convert fields to optional if parent's field has a specific value

Open
#1,940 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

Hello,

I am trying to define the following schema (pseudo-ish code):

class Parent(marshmallow.Schema):
    action = marshmallow.fields.EnumField(required=True)
    parameters = marshmallow.fields.Nested(Child, required=True)

class Child(marshmallow.Schema)
    param1 = marshmallow.fields.Str()
    param2 = marshmallow.fields.Int()
    # so on and so forth....

What I want the schema above to do is:

  • In case "action" has a specific value, all fields of the Child schema must be optional. However, if one passes a field that is not in Child, the validation must still fail.
  • In all other cases, all fields of Child must be required.

So far I've found this which does something similar, but I can't find a way to modify it for the case of a nested schema. Is this even possible to do with marshmallow?

Any ideas? Thanks in advance!

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 by reviewing the approach referenced in issue #1143 and the Parent/Child nested-schema example in this report. Determine whether conditional requiredness can be supported for the nested Child fields while still rejecting unknown fields; done means the behavior is clearly specified for both action cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.