如何将棉花糖模式用作经过验证的网络参数之一?

时间:2018-10-09 12:34:40

标签: flask marshmallow webargs

我正在Flask(烧瓶安宁)中试用API,并且正在使用webargs和棉花糖对POST请求中的args进行序列化。 args看起来像这样:

from webargs import fields, ValidationError
from app.core.utilities.helper_functions import raise_
from webargs.flaskparser import use_args
from marshmallow import Schema

comparators = ['not_exactly', 'exactly', '>=', '<=']


class ConfigSchema(Schema):
    comparator = fields.Str(required=True, validate=lambda x: True if x in comparators else
                            "Invalid comparator: {}. Allowed values: {}".format(x, comparators))
    value = fields.Str(required=True)

# config_schema = ConfigSchema()

bond_screen_args = {'config': fields.Dict(keys=fields.Str(validate=lambda x: True if len(x) > 0 else
raise_(ValidationError("Invalid factor name {}. Should be a non-zero length string.".format(x)))),
                                          values=fields.List(ConfigSchema, validate=lambda list_: True if
                                          (len(list_) >= 1 and len(list_) <= 2) else "Invalid number of screen "
                                          "options: {}. For floats, give two options at max, for string screen, "
                                          "give only one option.".format(list_)),
                                          required=True)}

问题部分是代码valued=fields.List(ConfigSchema)部分中的ConfigSchema。由于此类不是marshmallow.type.FieldsABC类型,因此出现错误。在这种情况下,应如何正确使用模式?还是我没有正确使用它们?

0 个答案:

没有答案