[Zope3-dev] Re: Z3 schemas and validation?
Jeffrey P Shell
jeffrey at cuemedia.com
Mon Sep 8 17:40:12 EDT 2003
On Monday, September 8, 2003, at 03:42 PM, Garrett Smith wrote:
> 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?)
Formulator distinguishes between 'validate' and 'validate_all'. The
'validate_all' methods raise a 'FormValidationError', which is a list
of individual 'ValidationError' objects (stored in the
FormValidationError's instance attribute 'errors').
'validate' raises ValidationError at the first failure (I believe).
I like the 'validate_all' approach personally. But I appreciate there
are both options.
More information about the Zope3-dev
mailing list