[Zope3-dev] Re: Z3 schemas and validation?
Garrett Smith
garrett at mojave-corp.com
Mon Sep 8 17:42:59 EDT 2003
Anthony Baxter wrote:
> I'm trying to use the zope3 zope.schemas package outside zope3,
> but I've hit a pretty basic problem - how do I actually validate
> the object conforms to the schema? The README.txt in the schema
> package refers to a validateMapping function that no longer exists.
>
> Is it a matter of something like (assuming the objects from
> src/zope/schema/README.txt)
>
> for name in IContact.names():
> IContact.get(name).validate(someone.__dict__[name])
>
> Shouldn't this functionality be provided in some way, that's
> less ugly for the programmer?
I.e. something like this?
from zope.schema import validateObject
object = Foo() # implements schema IFoo
object.bar = u'aaa'
object.baz = 111
try:
validateObject(object, IFoo)
except ????:
...
I'm not sure what sort of exception would be raised if one or more
fields raised a ValidationError. I would assume the caller would want to
have a list of errors, rather than just the first error raised. (What
would be more Pythonic here -- a sequence of errors, chained errors, or
a new exception class?)
Alternatively (though personally I don't like it):
errors = []
validateObject(object, IFoo, errors)
-- Garrett
More information about the Zope3-dev
mailing list