All Python ORM and form frameworks love to define own field/schema model. This seems to lead to a situation where they define their own validation functions too.
Some examples:
- Django local flavour defines phone number validations for different countries
- Misc. regular expression validators in Plone’s Archetypes content type subsystem
- Every framework define their own email validator, with various regular expressions
Isn’t writing one’s own validation code a bit redundant and exactly “reinventing the wheel” what open source principles so hard try to avoid? Could validation be a low hanging fruit to share among fellow Python projects? As I see it, for the simple data validation, like email and URL, the core code could be easily shared and different Python projects. You basically want just method is_valid_phonenumber(str) and then framework specific way to raise the error to the user.
Do such frameworks already exist? At least I haven’t seen one being used in any big Python project yet 🙁
… or is validation so complex thing, so that validation functions must be tightly integrated with the parent framework and I am missing some big things (like locales, etc.) here?
Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+
This post makes me sorry that you don’t have Google +1 buttons on your blog 🙂
This is something validatish (yeah, bad name) tries to accomplish. Don’t know if it’s what you’re looking for or not, but take a look.
I’ve got a prototype of such thing laying around if you’re curious…
Have you checked out formencode (http://formencode.org/ , also see https://github.com/bdoms/formencode )? It has many kinds of validators including phone numbers.
Time for a python port of commons validator (http://commons.apache.org/validator/api-1.3.1/)?
validatish and colander are both validation-only libraries. formencode does a lot more and its extra validators are very US-oriented, so I would not recommend it here.
Colander is another option: http://docs.pylonsproject.org/projects/colander/dev/
Colander seems to define its own schema. I especially want to avoid this 🙁
If formencode’s only failing is being too US-centric, surely it’s extensible: fix it or fork it, right?
Here’s how you make a library like Colander or FormEncode which has its own schema work with other systems: an integration plugin generates a Colander schema that mirrors the existing schema of your Django model or whatever. Under normal usage you don’t need to deal with the generated schema, which at its simplest would just be a list of fields that are mirrored to the target schema.
It’s sort of necessary for any library that is tasked with “validation” to work from a schema support. Even the simplest configuration of “type your password / repeat your password” represents a composite schema. The validation use case extends into composite validations almost immediately in the real world.
FormEncode has international support though I haven’t worked with it to know if it has serious shortcomings. I have hacked FormEncode to do things it normally can’t do however.
Formencode is fantastic, and very very easy to extend in sophisticated ways. I have found the more complex the validation gets, the better it is, I only wish it’s documentation better highlighted the power of it’s inner working. It has excellent design features for handling things like nested structures intelligently, looping over subschemas with access to the outer validator, all kinds of goodies.
I asked for a simple validation and not form framework.
Why I am getting dozens of suggestion for form and schema frameworks? Is everyone missing the point here?
A bit late, but you might want to take a look at https://github.com/doncatnip/kanone
It is not form-centric and can be used in various scenarios – though the ‘context’ can be used to populate forms-likes.
I got very frustrated with the same thing. Python has many nice form / schema libraries but a simple use case like “tell me if this email is valid or not” is clumsy with these libraries.
Since there was not a single package doing these simple validations I created one: http://validators.readthedocs.org/en/latest/
The code is hosted here: https://github.com/kvesteri/validators