Core Schema
coreschema (version 0.0.4) is a Python library for defining and validating data structures, often used in conjunction with Core API. It provides a basic set of schema primitives and a validation mechanism. The library was last released in February 2017 and appears to be in an abandoned or unmaintained state, with its GitHub repository showing no activity for seven years and other projects citing its deprecation.
Warnings
- breaking The `coreschema` library and its related `coreapi` project are considered abandoned and deprecated. Django REST Framework, which previously used CoreAPI, has phased it out in favor of OpenAPI starting from version 3.9. New projects should avoid using `coreschema`.
- gotcha `coreschema` was last released in 2017 (version 0.0.4) with a 'Development Status :: 3 - Alpha' classifier, indicating it was never considered stable. It was primarily built with Python 2 compatibility in mind, which may lead to unexpected behavior or compatibility issues with modern Python 3 environments.
- gotcha The library lacks active development, meaning there will be no new features, bug fixes, or security patches. Its functionality is very basic compared to modern schema validation tools.
Install
-
pip install coreschema
Imports
- Schema
from coreschema import Schema
- String
from coreschema import String
- Number
from coreschema import Number
- Object
from coreschema import Object
- Array
from coreschema import Array
- Union
from coreschema import Union
Quickstart
from coreschema import String, Number, Object
from collections import namedtuple
# Coreschema's internal Error structure for demonstration
Error = namedtuple('Error', ['text', 'index'])
# Define a simple schema for a user
user_schema = Object(
properties={
'name': String(title='User Name', description='The name of the user.'),
'age': Number(minimum=0, maximum=150, description='User\'s age, 0-150.'),
'city': String(default='Unknown', description='City of residence.')
},
required=['name', 'age']
)
# Example data
valid_data = {'name': 'Alice', 'age': 30}
valid_data_with_city = {'name': 'Bob', 'age': 45, 'city': 'New York'}
invalid_data_age = {'name': 'Charlie', 'age': 200} # age > 150
invalid_data_missing = {'age': 25, 'city': 'London'} # missing 'name'
print("--- Validating Data ---")
# Validate valid data
errors = user_schema.validate(valid_data)
if not errors:
print(f"'{valid_data}' is valid.")
else:
print(f"'{valid_data}' errors: {errors}")
# Validate valid data with optional field
errors = user_schema.validate(valid_data_with_city)
if not errors:
print(f"'{valid_data_with_city}' is valid.")
else:
print(f"'{valid_data_with_city}' errors: {errors}")
# Validate invalid age
errors = user_schema.validate(invalid_data_age)
if not errors:
print(f"'{invalid_data_age}' is valid.")
else:
print(f"'{invalid_data_age}' errors: {errors}")
# Validate missing required field
errors = user_schema.validate(invalid_data_missing)
if not errors:
print(f"'{invalid_data_missing}' is valid.")
else:
print(f"'{invalid_data_missing}' errors: {errors}")