Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,4 @@ Contributors (chronological)
- `@rstar327 <https://github.com/rstar327>`_
- Kadir Can Ozden `@bysiber <https://github.com/bysiber>`_
- Dhruvil Darji `@dhruvildarji <https://github.com/dhruvildarji>`_
- `@nyxst4ck <https://github.com/nyxst4ck>`_
7 changes: 5 additions & 2 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,9 +1192,12 @@ def _invoke_schema_validators(
pass_original = validator_kwargs.get("pass_original", False)

if many and not pass_collection:
for idx, (item, orig) in enumerate(
data_items = (
zip(data, original_data, strict=True)
):
if pass_original
else ((item, None) for item in data)
)
for idx, (item, orig) in enumerate(data_items):
self._run_validator(
validator,
item,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,22 @@ def check(datum):
errors = schema.validate([{"foo": 4, "bar": "42"}], many=True)
assert errors["_schema"] == ["bar cannot be a string"]

def test_collection_pre_load_can_change_length_before_item_schema_validation(self):
class MySchema(Schema):
foo = fields.Int()

@pre_load(pass_collection=True)
def remove_items_without_foo(self, data, **kwargs):
return [item for item in data if item.get("foo") != ""]

@validates_schema
def validate_foo(self, data, **kwargs):
assert "foo" in data

schema = MySchema()

assert schema.load([{"foo": 24}, {"foo": ""}], many=True) == [{"foo": 24}]

def test_allow_reporting_field_errors_in_schema_validator(self):
class NestedSchema(Schema):
baz = fields.Int(required=True)
Expand Down