marshmallow-code / marshmallow-code/marshmallow

Can/should marshmallow include pre-built validates_schema methods?

Open
#1,988 3 comments 0 reactions 0 assignees View on GitHub
polymorphism
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

I have a case in which I'm looking at adding a mutually exclusive set of fields to a schema.
It would be awesome if I could define this behavior with `marshmallow.validate.MutuallyExclusive(["foo", "bar"])`, and I'd be happy to work on an implementation of that. But where would it go and how would users apply it?

Unless I've missed/forgotten something, there's no way to provide pre-packaged schema validators for users to apply.

The following could work but is ugly:
```python
class MySchema(Schema):
foo = fields.String()
bar = fields.String()

_foo_bar_mutex = validates_schema(MutuallyExclusive(["foo", "bar"]))
```

What about a class decorator which adds a validator?

```python
@with_validates_schema(MutuallyExclusive(["foo", "bar"]))
class MySchema(Schema):
foo = fields.String()
bar = fields.String()
```

Is this worth pursuing? And if so, what is the correct interface to build?

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the existing marshmallow.validate and validates_schema APIs, along with the Schema usage shown in the issue. Determine whether pre-built schema validators should be supported and what interface best applies MutuallyExclusive-style behavior. Done means an agreed public interface and implementation scope, with its application to the mutually exclusive foo and bar fields made clear.

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.