Skip to content

Errors are not indexed with many=True #32

@sloria

Description

@sloria

When passing many=True to PolyField, errors are not keyed by index like they are with Nested.

Here's an example to reproduce the issue:

from marshmallow import Schema, fields, validate, ValidationError
from marshmallow_polyfield import PolyField


class ChildSchema(Schema):
    foo = fields.Str(validate=validate.Length(max=3))


class ParentSchema(Schema):
    children = fields.Nested(ChildSchema, many=True)


class ParentPolySchema(Schema):
    children = PolyField(deserialization_schema_selector=lambda _, __: ChildSchema, many=True)


try:
    ParentSchema().load({"children": [{'foo': 'x'}, {'foo': 'invalid'}, {'foo': 'alsoinvalid'}]})
except ValidationError as error:
    print('Nested:', error.messages)

try:
    ParentPolySchema().load({"children": [{'foo': 'x'}, {'foo': 'invalid'}, {'foo': 'alsoinvalid'}]})
except ValidationError as error:
    print('PolyField:', error.messages)

Output:

Nested: {'children': {1: {'foo': ['Longer than maximum length 3.']}, 2: {'foo': ['Longer than maximum length 3.']}}}
PolyField: {'children': {'foo': ['Longer than maximum length 3.']}}

I would expect both errors dictionaries to be the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions