marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Override _schema

Open 1Mark opened this issue 3 years ago • 1 comments

_schema is used to store schema level errors, if we know what the schema is called why do we use _schema

from marshmallow import Schema, fields


class UserSchema(Schema):
    name = fields.String()
    email = fields.Email()
    created_at = fields.DateTime()


class BlogSchema(Schema):
    title = fields.String()
    authors = fields.List(fields.Nested(UserSchema))

BlogSchema().load("""{"title":"mark", "authors":3}""")

ValidationError: {'_schema': ['Invalid input type.']}

Why doesn't it instead say

ValidationError: {'Blog.authors': ['Invalid input type.']}

1Mark avatar Jul 04 '22 15:07 1Mark

I know this is a design decision but it seems weird, especially since you cannot override marshmallow.exceptions.SCHEMA

1Mark avatar Jul 04 '22 15:07 1Mark

I think the error you get is at Blog schema level. Because you pass a string, not a dict. If the error was at authors field level, you'd get what you expect.

lafrech avatar Nov 15 '22 09:11 lafrech