pylons: validating data at model layer
I am evaluating some web application frameworks out there, and finally, two of the biggest contenders (in my opinion) are Ruby on Rails and Pylons. In order to have a better understanding of the two frameworks without spending too much time, I decided to follow a non-trivial application tutorial on one framework, and try to repeat the same thing on the other. I am hoping that the exercise will highlight the obvious differences.
For my experiment, I've chosen Ruby on Rails Tutorial. I've finished the application in Rails, and now, I've started doing it in Pylons. I've reached chapter 6 without too much drama (given that it is mostly static pages up until that point, this is not surprising). Now, I need to implement the model for users, and add the validation logic into the model. The first part is straightforward, but I am stuck on the second part.
From what I see, Pylons takes the approach of implementing validation on the form level. Doing some research, I've seen a lot of people suggest that the point where you accept the form input is the right location for input validation. I've also gone through quite a number of pylons projects on github, and I haven't been able to find a single project that handles model level data validation. The cl开发者_开发技巧osest I've seen was where developers stored their forms alongside the data in the model layer, but it is cheating in my opinion. I wouldn't mind not following the tutorial to the letter and following the crowd, but I happen to agree with the tutorial. For the model in question, the validations are done in the right place: checking password length, user name length, and validating that the e-mail is actually an e-mail all feel to be model level constraints. In addition, if I will have at least two forms that will add data to this model (creating a new user, and modifying information), and repeating the same validation in two different forms doesn't sound right.
To make a long story short (highlighted for TLDR): is there an infrastructure that I can use that ties the forms to the models a bit more tightly than the suggested SQLAlchemy/formencode pair? With these two, the best I can do is add assertions at the model layer. Actually, that is not true I could try to bridge the gap with custom code, but it does look like an awful lot of code, and quite hard to get right. So, I thought it would be better to ask around before trying to extend code that I don't really understand.
So first, I don't know of any built in way. Validation at the form level makes sense because different forms/views might access the same model and need to perform different validation based on the current user, the situation (time based), etc. So I'm one of those that's doing validation as part of the controller level of things.
Now as to your point on it being a lot of code, I think you could easily create the formencode scheme as part of your SqlAlchemy (SA) models and then just hook into the SA events for object save() and run the validation schema in there. Or write a wrapper function you use to populate the initial object before performing the save. So rather than:
person = Session.Query(People).get(10)
person.fname = request.params['fname']
person.lname = request.params['lname']
You'd do something more like:
person = Session.Query(People).get(10)
person.populate(request.params)
and it would iterate through the params and columns in the object to set it, doing validation along the way.
Sorry that there's not what you're looking for, but hope this helps you find a decent middle ground.
SQLAlchemy gives you some basic validation decorators when you're using the class + mapper method. Is that infrastructure sufficient for your needs?
Also, echoing Rick, keep in mind that your application might not have to follow the same validation rules at all times regarding a given table.
精彩评论