Zope Schema

8.1 · active · verified Mon Apr 13

zope.schema is a Python library that extends zope.interface to provide detailed descriptions of object attributes, known as schemas. It enables defining data models, including field types, constraints, and validation methods, independent of specific storage or form libraries. The current version is 8.1, and it's actively maintained by the Zope Foundation with a release cadence tied to features and breaking changes, typically alongside the broader Zope ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a schema using `zope.interface.Interface` and various `zope.schema` field types, then how to implement that schema in a Python class and perform basic validation.

import zope.interface
import zope.schema

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

@zope.interface.implementer(IBookmark)
class Bookmark(object):
    title = None
    url = None

bm = Bookmark()

# Example of validation (a common use case)
try:
    IBookmark['title'].validate('My Awesome Bookmark')
    IBookmark['url'].validate('http://example.com/bookmark')
    print("Validation successful!")
except zope.schema.ValidationError as e:
    print(f"Validation failed: {e}")

view raw JSON →