Viktor Kerkez wrote:
> I've just looked at SqlAlchemy, and liked it, but I'm curious if there
> is any concept of validators and manipulators similar to those found
> in Django's ORM, or I have to roll my own?
> 
> For those not familiar with Django, for every column type Django have
> predefined validators, also you can define your own and add them to
> the validators list. Manipulators (simplified) take a mapped object,
> validate every column and save it, update it or return an appropriate
> error message...
> 

This is not part of what SQLAlchemy does (or should do). As others have said, 
validation is a separate concern from mapping database tables to objects. For 
one thing, tying the validation layer directly to the object layer is very 
limiting because a given object may need to be validated with different rules 
depending on the context.

But there is an excellent solution that can easily be used with SQLAlchemy. Ian 
Bicking has written a validation package called FormEncode 
(http://www.formencode.org/). From my limited experience with Django, its 
Manipulators and Validators correspond to FormEncode's Schemas and Validators. 
Personally I prefer having a separate validation package because it can be used 
in contexts that do not directly involve the ORM (i.e. any object can be 
validated). Here's a short example of how it can be used with SQLAclhemy 
objects (or any other object for that matter):

# WARNING: THIS IS NOT PRODUCTION CODE #

class UserSchema(formencode.Schema):
    ident = validators.Int()
    name = validators.String()
    password = validators.String()
    email = validators.Email()
    # ...

# get a dict of valuse from client
values = getDataFromClient()

validator = UserSchema()
try:
    valid = validator.to_python(values)
except formencode.Invalid, ex:
    # handle error...
else:
    ident = valid.pop("ident")
    user = session.query(User).get(ident)
    for name, value in valid.items():
        setattr(user, name, value)


HTH

~ Daniel

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to