codeplay_
codeplay_

Reputation: 31

Set Required=True based on a condition in marshmallow

I was wondering whether there is a way in marshmallow to have a field to be required depending on another value.

Eg:

{ 'name':'Tom' }

{ 'name':'Arthur', 'job':'teacher' }

I'd like to know if there is a way to make the field job required if isWorking=True (isWorking is not a field in the schema)

Upvotes: 0

Views: 715

Answers (1)

Jérôme
Jérôme

Reputation: 14714

You may use a schema validator with marshmallow.decorators.validates_schema.

If you set pass_original=True, you'll get input data in which you'll find isWorking. Then you can raise a ValidationError if job is missing.

Upvotes: 0

Related Questions