使用安静的RequestParser进行嵌套验证 [英] Nested validation with the flask-restful RequestParser

查看:823
本文介绍了使用安静的RequestParser进行嵌套验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用烧杯安息的微型框架,我无法构建一个 RequestParser 来验证嵌套的资源。假设预期的JSON资源格式为:

  {
'a_list':[
{
'obj1':1,
'obj2':2,
'obj3':3
},
{
'obj1':1,
'obj2':2,
'obj3':3
}
]
}

a_list 中的每一项对应于一个对象:

<$ p $ ($ obj)$ b $ self.obj1 = obj1
self.obj2 = obj2 $ b $ MyObject(object):
def __init __(self,obj1,obj2,obj3) b self.obj3 = obj3

...然后我们可以用类似的方式创建一个RequestParser :来自flask.ext.restful的

  import reqparse 
parser = reqparse.RequestParser()
解析器。 add_argument('a_list',type = MyObject,action ='append')

...但是会的ou验证 a_list 中每个字典的嵌套 MyObject 或者,或者,这是错误的方法?

API对应于每个 MyObject 一个对象字面值,可能有一个或多个被传递给服务;因此,扁平化资源格式在这种情况下是行不通的。 我已经成功创建 RequestParser 嵌套对象的实例。首先像往常一样解析根对象,然后使用结果将其馈送到嵌套对象的解析器中。

诀窍是 add_argument 位置方法和 parse_args 方法的 req 参数。他们让你操纵 RequestParser 看。



下面是一个例子:

  root_parser = reqparse.RequestParser()
root_parser.add_argument('id',type = int)
root_parser.add_argument('name' ,type = str)
root_parser.add_argument('nested_one',type = dict)
root_parser.add_argument('nested_two',type = dict)
root_args = root_parser.parse_args()

$ b nested_one_parser = reqparse.RequestParser()
nested_one_parser.add_argument('id',type = int,location =('nested_one',))
nested_one_args = nested_one_parser.parse_args(req = root_args)

nested_two_parser = reqparse.RequestParser()
nested_two_parser.add_argument('id',type = int,location =('nested_two',))
nested_two_args = nested_two_parser。 parse_args(req = root_args)


Using the flask-restful micro-framework, I am having trouble constructing a RequestParser that will validate nested resources. Assuming an expected JSON resource format of the form:

{
    'a_list': [
        {
            'obj1': 1,
            'obj2': 2,
            'obj3': 3
        },
        {
            'obj1': 1,
            'obj2': 2,
            'obj3': 3
        }
    ]
}

Each item in a_list corresponds to an object:

class MyObject(object):
    def __init__(self, obj1, obj2, obj3)
        self.obj1 = obj1
        self.obj2 = obj2
        self.obj3 = obj3

... and one would then create a RequestParser using a form something like:

from flask.ext.restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('a_list', type=MyObject, action='append')

... but how would you validate the nested MyObjects of each dictionary inside a_list? Or, alternately, is this the wrong approach?

The API this corresponds to treats each MyObject as, essentially, an object literal, and there may be one or more of them passed to the service; therefore, flattening the resource format will not work for this circumstance.

解决方案

I have had success by creating RequestParser instances for the nested objects. Parse the root object first as you normally would, then use the results to feed into the parsers for the nested objects.

The trick is the location argument of the add_argument method and the req argument of the parse_args method. They let you manipulate what the RequestParser looks at.

Here's an example:

root_parser = reqparse.RequestParser()
root_parser.add_argument('id', type=int)
root_parser.add_argument('name', type=str)
root_parser.add_argument('nested_one', type=dict)
root_parser.add_argument('nested_two', type=dict)
root_args = root_parser.parse_args()

nested_one_parser = reqparse.RequestParser()
nested_one_parser.add_argument('id', type=int, location=('nested_one',))
nested_one_args = nested_one_parser.parse_args(req=root_args)

nested_two_parser = reqparse.RequestParser()
nested_two_parser.add_argument('id', type=int, location=('nested_two',))
nested_two_args = nested_two_parser.parse_args(req=root_args)

这篇关于使用安静的RequestParser进行嵌套验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆