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.
The highest rated solution do not support 'strict=True', To solve the 'strict=True' not support issue, you can create a FakeRequest object to cheat RequestParser
BTW: flask restful will rip RequestParser out, and replace it with Marshmallow Linkage
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 theadd_argument
method and thereq
argument of theparse_args
method. They let you manipulate what theRequestParser
looks at.Here's an example:
Since the
type
argument here is nothing but a callable that either returns a parsed value or raise ValueError on invalid type, I would suggest creating your own type validator for this. The validator could look something like:I would suggest using a data validation tool such as cerberus. You start by defining a validation schema for your object (Nested object schema is covered in this paragraph), then use a validator to validate the resource against the schema. You also get detailed error messages when the validation fails.
In the following example, I want to validate a list of locations:
The argument is defined as follows:
I found the bbenne10s answer really useful, but it didn't work for me as is.
The way I did this is probably wrong, but it works. My problem is that I don't understand what
action='append'
does as what it seems to do is wrap the value received in a list, but it doesn't make any sense to me. Can someone please explain whats the point of this in the comments?So what I ended up doing is creating my own
listtype
, get the list inside thevalue
param and then iterate through the list this way:Not a really elegant solution, but at least it does the work. I hope some one can point us in the right direction...
Update
As bbenne10 has said in the comments, what
action='append'
does is append all the arguments named the same into a list, so in the case of the OP, it doesn't seem to be very useful.I have iterated over my solution because I didn't like the fact that
reqparse
wasn't parsing/validating any of the nested objects so I what I have done is usereqparse
inside the custom object typemyobjlist
.First, I have declared a new subclass of
Request
, to pass it as the request when parsing the nested objects:This class overrides the
request.json
so that it uses a new json with the object to being parsed. Then, I added areqparse
parser tomyobjlist
to parse all the arguments and added an except to catch the parsing error and pass thereqparse
message.This way, even the nested objects will get parsed through reqparse and will show its errors