Text File

Zope 3 Schemas


Schemas extend the notion of interfaces to detailed descriptions of Attributes (but not methods). Every schema is an interface and specifies the public fields of an object. A field roughly corresponds to an attribute of a python object. But a Field provides space for at least a title and a description. It can also constrain its value and provide a validation method. Besides you can optionally specify characteristics such as its value being read-only or not required.

Zope 3 schemas were born when Jim Fulton and Martijn Faassen thought about Formulator for Zope 3 and PropertySets while at the Zope 3 sprint at the Zope BBQ in Berlin. They realized that if you strip all view logic from forms then you have something similar to interfaces. And thus schemas were born.

Simple Usage

Let's have a look at a simple example. First we write an interface as usual, but instead of describing the attributes of the interface with Attribute instances, we now use schema fields:

>>> import zope.interface
>>> import zope.schema
>>> class IBookmark(zope.interface.Interface):
...     title = zope.schema.TextLine(
...         title=u'Title',
...         description=u'The title of the bookmark',
...         required=True)
...     url = zope.schema.URI(
...         title=u'Bookmark URL',
...         description=u'URL of the Bookmark',
...         required=True)

Now we create a class that implements this interface and create an instance of it:

>>> class Bookmark(object):
...     zope.interface.implements(IBookmark)
...     title = None
...     url = None
>>> bm = Bookmark()

We would now like to only add validated values to the class. This can be done by first validating and then setting the value on the object. The first step is to define some data:

>>> title = u'Zope 3 Website'
>>> url = 'http://dev.zope.org/Zope3'

Now we, get the fields from the interface:

>>> title_field = IBookmark.get('title')
>>> url_field = IBookmark.get('url')

Next we have to bind these fields to the context, so that instance-specific information can be used for validation:

>>> title_bound = title_field.bind(bm)
>>> url_bound = url_field.bind(bm)

Now that the fields are bound, we can finally validate the data:

>>> title_bound.validate(title)
>>> url_bound.validate(url)

If the validation is successful, None is returned. If a validation error occurs a ValidationError will be raised; for example:

>>> url_bound.validate(u'http://zope.org/foo')
Traceback (most recent call last):
WrongType: (u'http://zope.org/foo', <type 'str'>)
>>> url_bound.validate('foo.bar')
Traceback (most recent call last):
InvalidURI: foo.bar

Now that the data has been successfully validated, we can set it on the object:

>>> title_bound.set(bm, title)
>>> url_bound.set(bm, url)

That's it. You still might think this is a lot of work to validate and set a value for an object. Note, however, that it is very easy to write helper functions that automate these tasks. If correctly designed, you will never have to worry explicitely about validation again, since the system takes care of it automatically.

What is a schema, how does it compare to an interface?

A schema is an extended interface which defines fields. You can validate that the attributes of an object conform to their fields defined on the schema. With plain interfaces you can only validate that methods conform to their interface specification.

So interfaces and schemas refer to different aspects of an object (respectively its code and state).

A schema starts out like an interface but defines certain fields to which an object's attributes must conform. Let's look at a stripped down example from the programmer's tutorial:

>>> import re
>>> class IContact(zope.interface.Interface):
...     """Provides access to basic contact information."""
...     first = zope.schema.TextLine(title=u"First name")
...     last = zope.schema.TextLine(title=u"Last name")
...     email = zope.schema.TextLine(title=u"Electronic mail address")
...     address = zope.schema.Text(title=u"Postal address")
...     postalCode = zope.schema.TextLine(
...         title=u"Postal code",
...         constraint=re.compile("\d{5,5}(-\d{4,4})?$").match)

TextLine is a field and expresses that an attribute is a single line of Unicode text. Text expresses an arbitrary Unicode ("text") object. The most interesting part is the last attribute specification. It constrains the postalCode attribute to only have values that are US postal codes.

Now we want a class that adheres to the IContact schema:

>>> class Contact(object):
...     zope.interface.implements(IContact)
...     def __init__(self, first, last, email, address, pc):
...         self.first = first
...         self.last = last
...         self.email = email
...         self.address = address
...         self.postalCode = pc

Now you can see if an instance of Contact actually implements the schema:

>>> someone = Contact(u'Tim', u'Roberts', u'tim@roberts', u'',
...                   u'12032-3492')
>>> for field in zope.schema.getFields(IContact).values():
...     bound = field.bind(someone)
...     bound.validate(bound.get(someone))

Data Modeling Concepts

The zope.schema package provides a core set of field types, including single- and multi-line text fields, binary data fields, integers, floating-point numbers, and date/time values.

Selection issues; field type can specify:

Fields and Widgets

Widgets are components that display field values and, in the case of writable fields, allow the user to edit those values.