Warlock
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
-
warlock.core.InvalidOperation: Unable to set 'name' to '5'
cause An attempt was made to assign a value (e.g., an integer) to a property that is defined as a different type (e.g., string) in the JSON schema.fixEnsure 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'`. -
warlock.core.InvalidOperation: Unable to set 'overlord' to 'Bears'
cause This error typically occurs when the schema for the Warlock model includes `'additionalProperties': False`, preventing the creation of properties not explicitly defined in the schema.fixIf 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. -
ModuleNotFoundError: No module named 'warlock'
cause The `warlock` library is not installed in the current Python environment or the environment's path is not correctly configured.fixInstall the library using pip: `pip install warlock`. If using a virtual environment, ensure it is activated.
Warnings
- breaking Warlock 1.3.1 and later versions (including 2.x) upgraded their `jsonschema` dependency to be compatible with `jsonschema` v3+, supporting JSON Schema Draft 7. If your project relies on validation against Draft 4 of JSON Schema, you must explicitly pin your `jsonschema` dependency to `<3`.
- gotcha Attempting to assign a value to an object property that does not conform to the schema's type or constraints (e.g., assigning an `int` to a `string` property) will raise a `warlock.core.InvalidOperation` error.
- gotcha If your JSON schema includes `'additionalProperties': False`, attempting to set any property on a Warlock object that is not explicitly defined in the schema will raise a `warlock.core.InvalidOperation` error.
Install
-
pip install warlock
Imports
- warlock
import warlock
- model_factory
import warlock Country = warlock.model_factory(schema)
Quickstart
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}")