Python Marshmallow验证包含对象列表的模式?

huangapple go评论70阅读模式
英文:

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 data so that it's valid JSON data.
  • I've removed the many=True from the call to MyResponseSchema() because you've edited your question such that you're no longer trying to parse a list of objects.

huangapple
  • 本文由 发表于 2023年6月1日 00:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定