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 MyObject
s 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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…