英文:
Python Marshmallow validate schema for list of objects?
问题
Your schema appears to be written in Python using the Marshmallow library. It defines two classes, MyResponseDataSchema and MyResponseSchema, for parsing JSON data. Without specific details on what issue you're encountering, it's challenging to determine what might be wrong with your schema. If you have a specific problem or error message you're facing, please provide more details so that I can assist you further.
英文:
I am trying to call an API, and parse the results and then return the parsed results.
Here is my schema in marshmallow:
from marshmallow import Schema, fields, post_load
import json
class MyResponseDataSchema(Schema):
path = fields.Str(required=False)
content_match_count = fields.Int(required=False)
class MyResponseSchema(Schema):
value = fields.List(
fields.Nested(MyResponseDataSchema),
required=False,
)
data = '{value: [{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.component.ts", "content_match_count" : 3},{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.module.ts", "content_match_count" : 5}]}'
schema_data = MyResponseSchema(many=True).load(data)
What is wrong with my schema?
答案1
得分: 0
问题不在于您的模式,而在于您的数据。您试图对一个字符串进行load()操作,但 Marshmallow 不是这样工作的;您需要向.load()方法传递一个字典:
from marshmallow import Schema, fields
import json
class MyResponseDataSchema(Schema):
path = fields.Str(required=False)
content_match_count = fields.Int(required=False)
class MyResponseSchema(Schema):
value = fields.List(
fields.Nested(MyResponseDataSchema),
required=False,
)
data = '{"value": [{"path": "libs/shared/components/shortcuts/src/lib/shortcuts.component.ts", "content_match_count": 3},{"path": "libs/shared/components/shortcuts/src/lib/shortcuts.module.ts", "content_match_count": 5}]}'
obj = json.loads(data)
schema_data = MyResponseSchema().load(obj)
这在Marshmallow文档中有说明。
请注意,在上面的代码中,我对您的示例进行了两处更改:
- 我修改了
data以使其成为有效的JSON数据。 - 我从对
MyResponseSchema()的调用中删除了many=True,因为您已经编辑了您的问题,不再尝试解析对象列表。
英文:
The problem here isn't with your schema, it's with your data. You're trying to load() a string, but Marshmallow doesn't work that way; you need to pass a dictionary to the .load() method:
from marshmallow import Schema, fields
import json
class MyResponseDataSchema(Schema):
path = fields.Str(required=False)
content_match_count = fields.Int(required=False)
class MyResponseSchema(Schema):
value = fields.List(
fields.Nested(MyResponseDataSchema),
required=False,
)
data = '{"value": [{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.component.ts", "content_match_count" : 3},{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.module.ts", "content_match_count" : 5}]}'
obj = json.loads(data)
schema_data = MyResponseSchema().load(obj)
This is shown in the Marshmallow documentation.
Note that in the above code, I've made two changes from your example:
- I've modified
dataso that it's valid JSON data. - I've removed the
many=Truefrom the call toMyResponseSchema()because you've edited your question such that you're no longer trying to parse a list of objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论