marshmallow-code / marshmallow-code/marshmallow
Convert fields to optional if parent's field has a specific value
Nobody has claimed this yet.
- 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
Childschema must be optional. However, if one passes a field that is not inChild, the validation must still fail. - In all other cases, all fields of
Childmust 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
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 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