[Zope3-dev] Add function for schema validation in zope.schema

Wichert Akkerman wichert at wiggy.net
Mon Aug 20 09:22:35 EDT 2007


Previously Christian Theune wrote:
> Hi,
> 
> Am Montag, den 20.08.2007, 08:59 -0400 schrieb Fred Drake:
> > On 8/20/07, Christian Zagrodnick <cz at gocept.com> wrote:
> > > I think we should add a function to validate a given schema on a given
> > > class. This should include constraints and invariants:
> > 
> > I do presume you mean object, rather than class, as your example implies.
> > 
> > > validateSchema(IMySchema, myobject)  [or alike]
> > 
> > +1
> > 
> > > I'm not sure about return values or exceptions. There are different use cases:
> > >
> > > 1. Is the object valid? -> True/False
> > > 2. What's wrong with the object -> {attribute/field-name: what's
> > > wrong}, what's wrong with invariants.
> > 
> > There should probably be a specific exception that's defined for this
> > purpose, and it could directly support the mapping interface to allow
> > detailed information to be extracted.  I suspect a common use would be
> > to simply verify the object and raise the exception in the invalid
> > case, denying whatever operation was being attempted.
> > 
> > This also suggests that there's no interesting return value; either
> > the exception is raised or it isn't.  Code that wants to introspect
> > the exception can catch that and act accordingly.
> 
> >From my latest experience and research of when to use exceptions and
> when to use return values I'd say let's not use an exception.
> 
> The report of "which fields are wrong" is the normal result of this
> function. Invalid data is not an uncommon output, rather, it's the sole
> purpose of this function. An exception should be raised if the
> validation could not be performed.
> 
> The result could be a structure that lists all errors. Eventually a
> result that equals False could be used to signal no errors, e.g. an
> empty dict or an empty list.

That would be confusing though: I would expect the result of a method
that checks validaty to return something that evaluates to True if
everything is valid. Code like this just messes up my brain:

  if not zope.schema.validate(obj, IMySchema):
	  print "Everything validates correctly!"

to me that is very non-intuitive and looks like the if condition is
incorrect.

Wichert.

-- 
Wichert Akkerman <wichert at wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.


More information about the Zope3-dev mailing list