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.
pip install warlockVerified import paths — ran on the pinned version, not inferred.
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.
If your project requires JSON Schema Draft 4, explicitly add `jsonschema<3` to your project's dependencies alongside Warlock. Otherwise, ensure you are using a compatible `jsonschema` version (v3 or later).
Always ensure that values assigned to Warlock object properties match the type and constraints defined in the JSON Schema. Catch `warlock.core.InvalidOperation` for robust error handling.
Review your schema to ensure all expected properties are defined, or set `'additionalProperties': True` in your schema if you intend to allow arbitrary properties. Catch `warlock.core.InvalidOperation` if this behavior is expected.
Ensure the assigned value's type matches the `type` specified in the JSON schema for that property. For example, assign a string for a `string` type property: `obj.name = 'New Name'`.
If the property is intended to exist, add it to the `properties` section of your JSON schema. If arbitrary properties should be allowed, set `'additionalProperties': True` in your schema.
Install the library using pip: `pip install warlock`. If using a virtual environment, ensure it is activated.