1

I have this schema

from marshmallow import validate, ValidationError from marshmallow_jsonapi import fields from marshmallow_jsonapi.flask import Relationship, Schema

class UserSchema(Schema):
    first_name = fields.Str(required=True])
    last_name = fields.Str(required=True)
    title = fields.Str(required=True)

    class Meta:
       type_ = 'users'
       self_view = "blog_view.users_detail"
       self_view_kwargs = {"user_id": "<id>", "_external": True}
       self_view_many = "blog_view.users_list"

    blog= Relationship(
        many=False,
        include_data=True,
        type_="blogs",
        include_resource_linkage=True,
        schema="BlogSchema"
        )

I want to load this data(coming from UI) for validation:

bulk_data = [
              { 'type': 'users', 
                'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
                 {'first_name': 'Billy', 'last_name': 'Butcher', 'title': 'Supe Hunter'} 
              },
              { 'type': 'users', 
                'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
                 {'first_name': 'Home', 'last_name': 'Lander', 'title': 'Leader'} 
              },
              { 'type': 'users', 
                'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
                 {'first_name': 'Black', 'last_name': 'Noir', 'title': 'Super Ninja'} 
              }
]

For validation I did:

data = UserSchema(many=True).load(input_data)

I get an error saying,

AttributeError: 'list' object has no attribute 'get'

which is obvious because I'm passing a list. The validation works fine when I pass a single dictionary from the above list, but I want to pass the bulk data and do validation at once as shown in Marshmallow doc: https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation

When

many=True

, load method expects a collection type so list, tuple, queryset etc.

Any suggestion on how to validate a list of data in Marshmallow? The marshmallow versions are:

marshmallow==2.18.0
marshmallow-jsonapi==0.23.1

Thanks!

Shafin M
  • 91
  • 2
  • 11

0 Answers0