Warlock

2.1.0 · active · verified Thu Apr 16

Warlock is a Python object model designed for creating self-validating objects based on JSON Schema and JSON Patch. It ensures data integrity by automatically validating object mutations against a defined schema. The current version is 2.1.0, and releases occur infrequently, often with significant changes between major versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a JSON Schema, create a Warlock model using `warlock.model_factory`, instantiate an object, modify its properties with automatic validation, and generate a JSON Patch document to track changes.

import warlock

# Define your JSON schema
schema = {
    'name': 'Country',
    'properties': {
        'name': {'type': 'string'},
        'abbreviation': {'type': 'string'},
        'population': {'type': 'integer'}
    },
    'additionalProperties': False
}

# Create a Warlock model factory from the schema
Country = warlock.model_factory(schema)

# Create an object using your model
sweden = Country(name='Sweden', abbreviation='SE', population=9453000)

print(f"Country Name: {sweden.name}")
print(f"Population: {sweden.population}")

# Update a property, which is validated against the schema
sweden.population = 10416000
print(f"New Population: {sweden.population}")

# Generate a JSON Patch document to track changes
sweden.population = 9453000 # Reset for patch example
sweden.name = 'Swedish Kingdom'
patch_document = sweden.patch
print(f"Generated JSON Patch: {patch_document}")

view raw JSON →