Is it possible to define a nested schema with mutually exclusive fields in marshmallow?
Create a Mutex
schema with both text
and id_
and add a schema-level validation to fail if both are provided.
class Mutex(Schema): @validates_schema def validate_numbers(self, data): if ( ('text' in data and 'id_' in data) or ('text' not in data and 'id_' not in data) ): raise ValidationError('Only one of text and _id is allowed') text = fields.Str() id_ = fields.Str() class Meta: strict = True
Side notes:
- Input validation error should not return a 500 (server error) but a 422.
- I'm not familiar with flask-restful, but it looks like you could save yourself some boilerplate by using webargs to parse the resource inputs.