Question

I have RESTFul API written in pyramid. View functions processes data in request.POST and request.matchdict and returns json response.

Eg: A method inside view class.

@view_config(route_name="temp_name", request_method="PUT")
def put_item(self):
    # validates and processes self.request.POST 
    # validates and processes self.request.matchdict
    # returns json reponse

As you can see, I'm doing validation inside view method, which I want to avoid.My intention is to separate validation from actual functionality. How do I handle this?

I saw colander http://cornice.readthedocs.org/en/latest/validation.html#using-colander which looks really good in my case. But looks like it is integrated with cornice which I'm not using at all. And also, I can't convert whole app into cornice now. Is it possible to use colander in the same way as given in the above link with my app?

This is the first time I'm writing RESTFul API's, also just started learning pyramid and colander. Need your help. Thanks in advance.

No correct solution

OTHER TIPS

You can use Colander independently of cornice. The most basic example for using Colander Schema in a pyramid application I remember you find here:

This way you can encapsulate schema validation using colander schemas and validators.

A more recent introduction of pyramid 1.5 branch into that topic you find here:

Oh, and look at that SO question. I liked it, may be it will be helpful to you as well:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top