Registry / serialization / pykwalify

pykwalify

JSON →
library1.8.0pypypi✓ verified 26d ago

pykwalify is a Python library and CLI tool for validating JSON and YAML data against a schema, based on the Kwalify schema specification. The current version is 1.8.0, with releases occurring periodically to add features, fix bugs, and update dependencies, though major development seems to have slowed since late 2020.

pip install pykwalify
INSTALL
IMPORT
SIG · PYKWALIFY
P
pykwalify
serializationpythonv1.8.0
Install
2.9s avg
Import
122ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.124s · 21.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.120s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Core
from pykwalify.core import Core
import pykwalify.core
The primary validation class 'Core' is typically imported directly from the pykwalify.core submodule for convenience.
SchemaError
from pykwalify.errors import SchemaError
ValidationError
from pykwalify.errors import ValidationError

This quickstart demonstrates how to define a schema and validate YAML data using pykwalify. It uses `io.StringIO` to simulate file input, making the example self-contained. It also shows how to catch `ValidationError` exceptions for invalid data.

import io from pykwalify.core import Core, ValidationError # Define your schema in YAML schema_yaml = """ type: map mapping: name: type: str required: true age: type: int range: {min: 0, max: 120} required: false """ # Define your data in YAML data_yaml = """ name: Alice age: 30 """ # Create Core instance with StringIO objects for schema and data c = Core(source_data=io.StringIO(data_yaml), schema_data=io.StringIO(schema_yaml)) try: c.validate() print("Validation successful!") except ValidationError as e: print(f"Validation failed: {e}") # Example of invalid data invalid_data_yaml = """ name: Bob age: 150 """ c_invalid = Core(source_data=io.StringIO(invalid_data_yaml), schema_data=io.StringIO(schema_yaml)) try: c_invalid.validate() except ValidationError as e: print(f"Invalid data validation failed as expected: {e}")
pykwalify --version
Debug
Known issues
breakingPython 2.7 and 3.5 support has been dropped. pykwalify v1.8.0 and later require Python 3.6 or newer.
fix
Upgrade your Python environment to 3.6 or later.
affects: >=1.8.0
breakingSupport for the `PyYAML` parser has been completely removed in favor of `ruamel.yaml`. Attempts to use `PyYAML` will fail.
fix
Ensure `ruamel.yaml` (minimum 0.16.0) is installed and used. No code change is typically needed as `pykwalify` defaults to `ruamel.yaml` since v1.8.0.
affects: >=1.8.0
gotcha`True` and `False` are no longer considered valid integers. This change affects schemas where boolean values might have previously passed integer validation.
fix
Update schemas to use `type: bool` for boolean values or ensure data correctly uses `type: int` for numbers.
affects: >=1.5.2
gotchaThe default YAML parser was changed to `ruamel.yaml` from `PyYAML` (which was deprecated in 1.5.2 and removed in 1.8.0). Ensure `ruamel.yaml>=0.16.0` and `python-dateutil>=2.8.0` are installed to avoid dependency conflicts or parser issues.
fix
Install the required versions: `pip install 'ruamel.yaml>=0.16.0' 'python-dateutil>=2.8.0'`.
affects: >=1.8.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pykwalify'
The 'pykwalify' library is not installed in the Python environment or is not accessible in the current PYTHONPATH.
fix
Install the library using pip: `pip install pykwalify`
NotMappingError: error code ...
The data being validated does not match the expected type 'map' (dictionary) as defined in the schema for a given node. This error can also appear as 'NotSequenceError' if a sequence (list) was expected.
fix
Adjust the data to conform to the schema's type definition; for 'NotMappingError', ensure a dictionary is provided where `type: map` is specified in the schema, and for 'NotSequenceError', provide a list where `type: seq` is specified.
AttributeError: 'generator' object has no attribute 'get'
This error occurs when `yaml.safe_load_all()` is used to load a YAML file containing a single document, as it returns a generator, but `pykwalify.core.Core` expects a dictionary or list directly for its `schema_data` or `source_data` parameters.
fix
Use `yaml.safe_load()` to load a single YAML document into a dictionary or list, which can then be passed to `pykwalify.core.Core`.
AttributeError: 'int' object has no attribute 'startswith'
A non-string value (e.g., an integer) is present in the data where the schema specifies a rule like `regex` or `pattern`, which attempts to call string methods like `startswith` on the incompatible type.
fix
Ensure that data values conform to the expected string type when using string-specific validation rules like `regex` or `pattern` in the schema. Convert non-string data to strings if appropriate for the schema definition.
Upgrade
Version history
1.8.0latest on PyPI · released Dec 30, 2020
Audit
Dependencies
ruamel.yamlrequiredDefault and preferred YAML parser; minimum version 0.16.0 required since v1.8.0.
python-dateutilrequiredRequired for date/datetime validations; minimum version 2.8.0 required since v1.8.0.
Agent activity
10 hits · last 30 days
node
8
Resources
pykwalify — pip install pykwalify · libregistry