marshmallow-code / marshmallow-code/marshmallow

load many functionality doesn't seem to be of much help

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

Nobody has claimed this yet.

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

Description

I have a UserSchema with a pre_load(pass_many=False) decorator, suppose if the pre_load decorator raises ValidationError, all the objects in the collection I passed became invalid even though there are some valid objects available. IMO errors of one object shouldn't stop the other objects in the collection from being deserialized, validated, and post-processed.

The hack would be to run a loop on the collection and load the object one by one but it would be nice if the 'many' attribute can handle this. There are some trade-offs in loading the object one by one.

from marshmallow import Schema, ValidationError, fields, pre_load

class UserSchema(Schema):
    name = fields.Str()
    age = fields.Int()
    height = fields.Float()

    @pre_load(pass_many=False)
    def check_age(self, data, **kwargs):
        if data['age'] < 18:
            raise ValidationError('Minor user', 'age')
        
        return data

user_schema = UserSchema()

data = [
    {
        'name': 'dk',
        'age': 20,
        'height': '5.5'
    },
    {
        'name': 'dd',
        'age': 19,
        'height': '5.5'
    },
    {
        'name': 'df',
        'age': 10,
        'height': '5.5'
    }
]

try:
    user_schema.load(data, many=True)
except ValidationError as err:
    print(err.messages, err.valid_data)


for d in data:
    try:
        d = user_schema.load(d)
        print(d)
    except ValidationError as err:
        print(err.messages, err.valid_data)

Suggestions are welcome.

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 reproducing the example with UserSchema.load(data, many=True) and compare it with loading each item separately. Trace the many=True deserialization and pre_load validation flow; done means invalid items report their errors while valid items are deserialized and post-processed, with the intended valid_data behavior covered by tests.

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
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.