Schematics
Schematics is a Python library for defining, validating, and transforming data structures. It allows combining types into structures, validating them, and transforming data shapes based on simple descriptions, similar to ORM type systems but without a database layer. The current stable version is 2.1.1. While there isn't a strict, regular release cadence, the library has seen active development and maintenance, offering an intuitive API and comprehensive features for modern Python applications.
Warnings
- breaking While older documentation mentions support for Python 2.7 and Python 3.3-3.7, the latest `schematics` 2.1.1 explicitly declares `requires_python: >=3.6` on PyPI. Attempting to install or run this version on older Python versions (e.g., Python 2.x, 3.3, 3.4, 3.5) will result in installation failures or runtime errors due to dropped compatibility.
- gotcha The official documentation for Schematics (e.g., on Read the Docs) explicitly states that it is 'currently somewhat out of date.' This can lead to discrepancies between the documented behavior, available features, or best practices and the actual state of the 2.1.1 release.
- gotcha When running on Python 3.10 and newer, `schematics` may emit `DeprecationWarning` messages, typically related to deprecated imports from `collections.abc` (e.g., `collections.Iterable`). While these are currently warnings, they indicate usage of Python APIs that are slated for removal in future Python versions (e.g., Python 3.12+), which could eventually lead to breaking errors.
- gotcha For handling validation failures, `schematics.exceptions.DataError` is the general exception to catch for all validation-related issues from models. While some older examples or specific scenarios might refer to `schematics.exceptions.ModelValidationError`, `ModelValidationError` is a subclass of `DataError`. Using `DataError` ensures you catch all relevant validation exceptions consistently.
Install
-
pip install schematics
Imports
- Model
from schematics.models import Model
- StringType
from schematics.types import StringType
- URLType
from schematics.types import URLType
- DecimalType
from schematics.types import DecimalType
- DateTimeType
from schematics.types import DateTimeType
- DataError
from schematics.exceptions import DataError
Quickstart
import datetime
from schematics.models import Model
from schematics.types import StringType, DecimalType, DateTimeType
from schematics.exceptions import DataError
class WeatherReport(Model):
city = StringType(required=True)
temperature = DecimalType(required=True)
taken_at = DateTimeType(default=datetime.datetime.now)
# Create a valid instance
report_data = {'city': 'NYC', 'temperature': 80.5}
t1 = WeatherReport(report_data)
t1.validate()
print(f"Validated report: {t1.to_primitive()}\n")
# Demonstrate validation failure with invalid type
t_fail_type = WeatherReport({'city': 'LA', 'temperature': 'not-a-number'})
try:
t_fail_type.validate()
except DataError as e:
print(f"Validation failed (invalid type): {e.messages}")
# Example with missing required field
t_missing_field = WeatherReport({'temperature': 75.0})
try:
t_missing_field.validate()
except DataError as e:
print(f"Validation failed (missing field): {e.messages}")