Closed hyacinthus closed 9 years ago
Hmm, I'm not quite certain if this is something we want to bake directly into schema.validate
with no option to disable, since it's entirely possible someone may want an empty response from a request as defined behaviour (although this is unlikely).
That being said, I think it would be best to add a boolean keyword argument here like on_empty_404=False
for example. Then if you wanted to use it by default, you could just create a partial in your application like so, and use it:
import functools
validate = functools.partial(schema.validate, on_empty_404=True)
Could you also add a test for this? Thanks!
There are definitely times when you may want an empty response. For example, GET a list of user-uploaded images. If you have a brand-new user and the request is GET images, the result should be empty to indicate they haven't uploaded any images yet. Returning a 404 would be atypical behavior in this very standard use case. Returning an empty set is expected.
@hfaran @malexh Agree with you. Boolean keyword is better. I'll test it.
In fact I can't find a right place to add this feature. But It is stupid to check the output in every handler, or a decorator one more? And now, I need import functools.partial, 2 lines per file, neither better nor worse...
Well, you can just define a custom version of the decorator in your project like I do above, and then just import that.
For example, if you package is called mywebapp
, just create a schema
module and add this to it:
import functools
from tornado_json import schema
validate = functools.partial(schema.validate, on_empty_404=True)
Then you can use mywebapp.schema.validate
instead of tornado_json.schema.validate
.
Thanks , Good Idea .
@hyacinthus Are you planning on adding a test for this?
Oh, sorry, I've misconstrued your words about test... I'll add it later.
OK
Looks good! Merging.
Thank you for your advice and patience.
Hey, How about automatically return 404 when output data is empty?