{"id":7857,"library":"warlock","title":"Warlock","description":"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.","status":"active","version":"2.1.0","language":"en","source_language":"en","source_url":"https://github.com/bcwaldon/warlock","tags":["JSON schema","JSON patch","model validation","data validation","object modeling"],"install":[{"cmd":"pip install warlock","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for JSON Schema validation.","package":"jsonschema","optional":false},{"reason":"Required for JSON Patch functionality.","package":"jsonpatch","optional":false},{"reason":"Warlock 2.x requires Python 3.9 or newer.","package":"Python","optional":false}],"imports":[{"note":"The primary library import is straightforward.","symbol":"warlock","correct":"import warlock"},{"note":"Models are created using the `model_factory` function from the top-level `warlock` module.","symbol":"model_factory","correct":"import warlock\nCountry = warlock.model_factory(schema)"}],"quickstart":{"code":"import warlock\n\n# Define your JSON schema\nschema = {\n    'name': 'Country',\n    'properties': {\n        'name': {'type': 'string'},\n        'abbreviation': {'type': 'string'},\n        'population': {'type': 'integer'}\n    },\n    'additionalProperties': False\n}\n\n# Create a Warlock model factory from the schema\nCountry = warlock.model_factory(schema)\n\n# Create an object using your model\nsweden = Country(name='Sweden', abbreviation='SE', population=9453000)\n\nprint(f\"Country Name: {sweden.name}\")\nprint(f\"Population: {sweden.population}\")\n\n# Update a property, which is validated against the schema\nsweden.population = 10416000\nprint(f\"New Population: {sweden.population}\")\n\n# Generate a JSON Patch document to track changes\nsweden.population = 9453000 # Reset for patch example\nsweden.name = 'Swedish Kingdom'\npatch_document = sweden.patch\nprint(f\"Generated JSON Patch: {patch_document}\")","lang":"python","description":"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."},"warnings":[{"fix":"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).","message":"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`.","severity":"breaking","affected_versions":"1.3.1 - 2.x"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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'`.","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.","error":"warlock.core.InvalidOperation: Unable to set 'name' to '5'"},{"fix":"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.","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.","error":"warlock.core.InvalidOperation: Unable to set 'overlord' to 'Bears'"},{"fix":"Install the library using pip: `pip install warlock`. If using a virtual environment, ensure it is activated.","cause":"The `warlock` library is not installed in the current Python environment or the environment's path is not correctly configured.","error":"ModuleNotFoundError: No module named 'warlock'"}]}