Michael Bayer wrote:
> On Jan 24, 1:50 am, "chris e" <[EMAIL PROTECTED]> wrote:
>   
>> I am planning on using sqlalchemy to build the api for a database I am
>> developing, and I was wondering if there is any type of column
>> verification that occurs before database commit.
>>
>> I.E.:  a string column with length 40 would throw a verification
>> exception if a value longer that 40 characters was placed into it and
>> saved.
>>     
>
> your database will throw an error.  why reinvent the wheel ?
>
>   
>> Additionally has anyone thought of implementing some sort of
>> verification support for objects that are mapped which would allow the
>> object to perform pre database action logic?  The intent being that the
>> instance would check to see that the object meets additional business
>> logic requirements before it is inserted or updated.
>>
>> I.E.: a User business object would verify that the userid was part of
>> the [a-z] [A-Z]and [0-9] character classes, and if not an exception
>> would be raised to prevent the database action.
>>     
>
> thats exactly the kind of thing you should write into your 
> application.  has nothing to do with an ORM.  for generic validation 
> widgets to help, check out formencode ( http://formencode.org/ ).
>
>
>   
(Just to share a method which woks well for me :)

What I usually to perform validation is to create a property() 
(_set_attribute(), _get_attribute()) for each mapped column, then I use 
the column_prefix="_" attribute in SQLAlchemy.
After that I have a function which iterate on the columns 
(YourMappedObject.c.keys()) and use a try / except with a setattr (it's 
a bit more sophisticated than that in fact), for example :

assign_mapper(session_context, Language, table_languages, column_prefix='_')

class Language(object):
 
  def _set_iso_code(self, value):
    try:
      value = ''.join(value.split()).lower()
    except AttributeError:
      raise Invalid('iso code must be a string')
    if len(value) == 2:
      self._iso_code = value
    else:
      raise Invalid('Invalid iso code')

  def _get_iso_code(self):
    return self._iso_code

  iso_code = property(_get_iso_code, _set_iso_code)


(...)

then I do something like (not complete):
def populate(MappedObject, values):
  errors = []
  for c in MappedObject.c.keys():
    value = values.get(c, Undefined())
    if value is not Undefined:
      try:
        setattr(MappedObject, c , value)
      except Invalid, e:
        errors.append(str(e))
  return errors

also, I have in my models a __before_save__ / __before_update__ which 
check additional things like NOT NULL constraints (!None), ...

> >
>   


-- 
Julien Cigar
Belgian Biodiversity Platform
http://www.biodiversity.be
Université Libre de Bruxelles
Campus de la Plaine CP 257
Bâtiment NO, Bureau 4 N4 115C (Niveau 4)
Boulevard du Triomphe, entrée ULB 2
B-1050 Bruxelles
office: [EMAIL PROTECTED]
home: [EMAIL PROTECTED]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to