i am learning how to validate using marshamallow and i want to validate a field that is a list of dictonaries such my_list = [ {"my_fild":1}, {"my_field":2} ] reading the documentation from https://marshmallow.readthedocs.io/en/stable/nesting.html say i can put a field.List with a Nested field, but when i try it i get
{'a_list': {0: {'my_key': ['Not a valid mapping type.']}, 1: {'my_key': ['Not a valid mapping type.']}}}
wonder why is that.
from marshmallow import Schema, fields, ValidationError
from pprint import pprint
class UserSchema(Schema):
name = fields.String(required=True)
email = fields.Email(required=True)
class SomeDict(Schema):
# field my_key should be
my_key = fields.Dict(keys=fields.Str(), values=fields.Int())
class BlogSchema(Schema):
title = fields.String(required=True)
author = fields.Nested(UserSchema, required=True)
some_dicts = fields.Dict(required=True, keys=fields.Str(), values=fields.Str())
# i am nesting SomeDict
a_list = fields.List(fields.Nested(SomeDict), required=True)
j = {
"title": "bla",
"author": {"name": "ble", "email": "d@gmail.com"},
"some_dicts": {"one": "one", "two": "2"},
# i want to pass a list of dictionaries
"a_list": [ {"my_key": 1}, {"my_key": 2} ]
}
result = BlogSchema()
try:
result = BlogSchema().load(j)
except ValidationError as err:
print(err.messages)
shouldn't the 'SomeDict' class should work? thanks guys for the help =)